Skip to content

Commit

Permalink
a little more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rysweet committed Nov 6, 2024
1 parent 6ce4494 commit 81a79d5
Show file tree
Hide file tree
Showing 24 changed files with 38 additions and 39 deletions.
2 changes: 1 addition & 1 deletion dotnet/samples/Hello/HelloAIAgents/HelloAIAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Hello;
[TopicSubscription("HelloAgents")]
public class HelloAIAgent(
IAgentContext context,
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry,
IChatClient client) : HelloAgent(
context,
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/Hello/HelloAIAgents/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Hello
{
[TopicSubscription("HelloAgents")]
public class HelloAgent(
IAgentContext context,
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : ConsoleAgent(
context,
typeRegistry),
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/Hello/HelloAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Hello
{
[TopicSubscription("HelloAgents")]
public class HelloAgent(
IAgentContext context,
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : AgentBase(
context,
typeRegistry),
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/Hello/HelloAgentState/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Hello
{
[TopicSubscription("HelloAgents")]
public class HelloAgent(
IAgentContext context,
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry) : ConsoleAgent(
context,
typeRegistry),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace DevTeam.Agents;

[TopicSubscription("devteam")]
public class Dev(IAgentContext context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<Dev> logger)
public class Dev(IAgentRuntime context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<Dev> logger)
: SKAiAgent<DeveloperState>(context, memory, kernel, typeRegistry), IDevelopApps,
IHandle<CodeGenerationRequested>,
IHandle<CodeChainClosed>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace DevTeam.Agents;

[TopicSubscription("devteam")]
public class DeveloperLead(IAgentContext context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<DeveloperLead> logger)
public class DeveloperLead(IAgentRuntime context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<DeveloperLead> logger)
: SKAiAgent<DeveloperLeadState>(context, memory, kernel, typeRegistry), ILeadDevelopers,
IHandle<DevPlanRequested>,
IHandle<DevPlanChainClosed>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace DevTeam.Agents;

[TopicSubscription("devteam")]
public class ProductManager(IAgentContext context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<ProductManager> logger)
public class ProductManager(IAgentRuntime context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, ILogger<ProductManager> logger)
: SKAiAgent<ProductManagerState>(context, memory, kernel, typeRegistry), IManageProducts,
IHandle<ReadmeChainClosed>,
IHandle<ReadmeRequested>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.SemanticKernel.Memory;
namespace Microsoft.AI.DevTeam;

public class AzureGenie(IAgentContext context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, IManageAzure azureService)
public class AzureGenie(IAgentRuntime context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, IManageAzure azureService)
: SKAiAgent<object>(context, memory, kernel, typeRegistry),
IHandle<ReadmeCreated>,
IHandle<CodeCreated>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/dev-team/DevTeam.Backend/Agents/Hubber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Microsoft.AI.DevTeam;

public class Hubber(IAgentContext context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, IManageGithub ghService)
public class Hubber(IAgentRuntime context, Kernel kernel, ISemanticTextMemory memory, [FromKeyedServices("EventTypes")] EventTypes typeRegistry, IManageGithub ghService)
: SKAiAgent<object>(context, memory, kernel, typeRegistry),
IHandle<NewAsk>,
IHandle<ReadmeGenerated>,
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Microsoft.AutoGen/Abstractions/IAgentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IAgentBase
{
// Properties
AgentId AgentId { get; }
IAgentContext Context { get; }
IAgentRuntime Context { get; }

// Methods
Task CallHandler(CloudEvent item);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// IAgentContext.cs
// IAgentRuntime.cs

using System.Diagnostics;

namespace Microsoft.AutoGen.Abstractions;

public interface IAgentContext
public interface IAgentRuntime
{
AgentId AgentId { get; }
IAgentBase? AgentInstance { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions dotnet/src/Microsoft.AutoGen/Agents/AgentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public abstract class AgentBase : IAgentBase, IHandle
private readonly Dictionary<string, TaskCompletionSource<RpcResponse>> _pendingRequests = [];

private readonly Channel<object> _mailbox = Channel.CreateUnbounded<object>();
private readonly IAgentContext _context;
private readonly IAgentRuntime _context;
public string Route { get; set; } = "base";

protected internal ILogger<AgentBase> _logger;
public IAgentContext Context => _context;
public IAgentRuntime Context => _context;
protected readonly EventTypes EventTypes;

protected AgentBase(
IAgentContext context,
IAgentRuntime context,
EventTypes eventTypes,
ILogger<AgentBase>? logger = null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// AgentContext.cs
// AgentRuntime.cs

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

namespace Microsoft.AutoGen.Agents;

internal sealed class AgentContext(AgentId agentId, IAgentWorker runtime, ILogger<AgentBase> logger, DistributedContextPropagator distributedContextPropagator) : IAgentContext
internal sealed class AgentRuntime(AgentId agentId, IAgentWorker worker, ILogger<AgentBase> logger, DistributedContextPropagator distributedContextPropagator) : IAgentRuntime
{
private readonly IAgentWorker _runtime = runtime;
private readonly IAgentWorker worker = worker;

public AgentId AgentId { get; } = agentId;
public ILogger Logger { get; } = logger;
Expand Down Expand Up @@ -39,23 +39,23 @@ public void Update(Activity? activity, CloudEvent cloudEvent)
public async ValueTask SendResponseAsync(RpcRequest request, RpcResponse response, CancellationToken cancellationToken = default)
{
response.RequestId = request.RequestId;
await _runtime.SendResponseAsync(response, cancellationToken);
await worker.SendResponseAsync(response, cancellationToken);
}
public async ValueTask SendRequestAsync(IAgentBase agent, RpcRequest request, CancellationToken cancellationToken = default)
{
await _runtime.SendRequestAsync(agent, request, cancellationToken).ConfigureAwait(false);
await worker.SendRequestAsync(agent, request, cancellationToken).ConfigureAwait(false);
}
public async ValueTask PublishEventAsync(CloudEvent @event, CancellationToken cancellationToken = default)
{
await _runtime.PublishEventAsync(@event, cancellationToken).ConfigureAwait(false);
await worker.PublishEventAsync(@event, cancellationToken).ConfigureAwait(false);
}
public async ValueTask StoreAsync(AgentState value, CancellationToken cancellationToken = default)
{
await _runtime.StoreAsync(value, cancellationToken).ConfigureAwait(false);
await worker.StoreAsync(value, cancellationToken).ConfigureAwait(false);
}
public ValueTask<AgentState> ReadAsync(AgentId agentId, CancellationToken cancellationToken = default)
{
return _runtime.ReadAsync(agentId, cancellationToken);
return worker.ReadAsync(agentId, cancellationToken);
}

public IDictionary<string, string> ExtractMetadata(IDictionary<string, string> metadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.AutoGen.Agents;
{
protected IChatClient ChatClient { get; }
public InferenceAgent(
IAgentContext context,
IAgentRuntime context,
EventTypes typeRegistry, IChatClient client
) : base(context, typeRegistry)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.AutoGen.Agents;
protected Kernel _kernel;
private readonly ISemanticTextMemory _memory;

public SKAiAgent(IAgentContext context, ISemanticTextMemory memory, Kernel kernel, EventTypes typeRegistry) : base(context, typeRegistry)
public SKAiAgent(IAgentRuntime context, ISemanticTextMemory memory, Kernel kernel, EventTypes typeRegistry) : base(context, typeRegistry)
{
_state = new();
_memory = memory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class ConsoleAgent : IOAgent,
{

// instead of the primary constructor above, make a constructr here that still calls the base constructor
public ConsoleAgent(IAgentContext context, [FromKeyedServices("EventTypes")] EventTypes typeRegistry) : base(context, typeRegistry)
public ConsoleAgent(IAgentRuntime context, [FromKeyedServices("EventTypes")] EventTypes typeRegistry) : base(context, typeRegistry)
{
_route = "console";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.AutoGen.Agents;

[TopicSubscription("FileIO")]
public abstract class FileAgent(
IAgentContext context,
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry,
string inputPath = "input.txt",
string outputPath = "output.txt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.AutoGen.Agents;
public abstract class IOAgent : AgentBase
{
public string _route = "base";
protected IOAgent(IAgentContext context, EventTypes eventTypes) : base(context, eventTypes)
protected IOAgent(IAgentRuntime context, EventTypes eventTypes) : base(context, eventTypes)
{
}
public virtual async Task Handle(Input item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class WebAPIAgent : IOAgent,
private readonly string _url = "/agents/webio";

public WebAPIAgent(
IAgentContext context,
IAgentRuntime context,
[FromKeyedServices("EventTypes")] EventTypes typeRegistry,
ILogger<WebAPIAgent> logger,
string url = "/agents/webio") : base(
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Microsoft.AutoGen/Agents/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
namespace Microsoft.AutoGen.Agents;
public sealed class Client(IAgentWorker runtime, DistributedContextPropagator distributedContextPropagator,
[FromKeyedServices("EventTypes")] EventTypes eventTypes, ILogger<AgentBase> logger)
: AgentBase(new AgentContext(new AgentId("client", Guid.NewGuid().ToString()), runtime, logger, distributedContextPropagator), eventTypes)
: AgentBase(new AgentRuntime(new AgentId("client", Guid.NewGuid().ToString()), runtime, logger, distributedContextPropagator), eventTypes)
{
}
5 changes: 2 additions & 3 deletions dotnet/src/Microsoft.AutoGen/Agents/Services/AgentWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class AgentWorker :
private readonly ConcurrentDictionary<string, List<IConnection>> _supportedAgentTypes = [];
private readonly ConcurrentDictionary<string, Type> _agentTypes = new();
private readonly ConcurrentDictionary<(string Type, string Key), IAgentBase> _agents = new();
private readonly ConcurrentDictionary<string, (IAgentBase Agent, string OriginalRequestId)> _pendingRequests = new();
private readonly ILogger<AgentWorker> _logger;
private readonly InMemoryQueue<CloudEvent> _eventsQueue = new();
private readonly InMemoryQueue<Message> _messageQueue = new();
Expand Down Expand Up @@ -58,6 +57,7 @@ public AgentWorker(
_gatewayRegistry = _clusterClient.GetGrain<IAgentRegistry>(0);
}
public InMemoryQueue<Message> GetMessageQueue() => _messageQueue;
public InMemoryQueue<CloudEvent> GetEventQueue() => _eventsQueue;
public async ValueTask PublishEventAsync(CloudEvent evt, CancellationToken cancellationToken = default)
{
await this.WriteAsync(evt,cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -155,7 +155,6 @@ public async Task RunReadPump()
}
}


public async Task StartAsync(CancellationToken cancellationToken)
{
StartCore();
Expand Down Expand Up @@ -264,7 +263,7 @@ private IAgentBase GetOrActivateAgent(AgentId agentId)
{
if (_agentTypes.TryGetValue(agentId.Type, out var agentType))
{
var context = new AgentContext(agentId, this, _serviceProvider.GetRequiredService<ILogger<AgentBase>>(), _distributedContextPropagator);
var context = new AgentRuntime(agentId, this, _serviceProvider.GetRequiredService<ILogger<AgentBase>>(), _distributedContextPropagator);
agent = (AgentBase)ActivatorUtilities.CreateInstance(_serviceProvider, agentType, context);
_agents.TryAdd((agentId.Type, agentId.Key), agent);
}
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/Microsoft.AutoGen/Agents/Services/Gateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void DispatchResponse(InMemoryQueue<Message> connection, RpcResponse res
private async ValueTask RegisterAgentTypeAsync(AgentWorker connection, RegisterAgentTypeRequest msg)
{
connection.AddSupportedType(msg.Type);
_supportedAgentTypes.GetOrAdd(msg.Type, _ => []).Add(connection);
_supportedAgentTypes.GetOrAdd(msg.Type, _ => []).Add(connection.GetEventQueue());

await _gatewayRegistry.RegisterAgentType(msg.Type, _reference);
}
Expand Down Expand Up @@ -148,7 +148,7 @@ internal async Task OnReceivedMessageAsync(AgentWorker connection, Message messa
await DispatchEventAsync(message.CloudEvent);
break;
case Message.MessageOneofCase.RegisterAgentTypeRequest:
await RegisterAgentTypeAsync(connection, message.RegisterAgentTypeRequest);
await RegisterAgentTypeAsync(connection, message.RegisterAgentTypeRequest).ConfigureAwait(false);
break;
default:
throw new InvalidOperationException($"Unknown message type for message '{message}'.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void Dispose()
_outboundMessagesChannel.Writer.TryComplete();
_channel?.Dispose();
}
private async Task RunReadPump()
private new async Task RunReadPump()
{
var channel = GetChannel();
while (!_shutdownCts.Token.IsCancellationRequested)
Expand Down Expand Up @@ -169,7 +169,7 @@ private IAgentBase GetOrActivateAgent(AgentId agentId)
{
if (_agentTypes.TryGetValue(agentId.Type, out var agentType))
{
var context = new AgentContext(agentId, this, _serviceProvider.GetRequiredService<ILogger<AgentBase>>(), _distributedContextPropagator);
var context = new AgentRuntime(agentId, this, _serviceProvider.GetRequiredService<ILogger<AgentBase>>(), _distributedContextPropagator);
agent = (AgentBase)ActivatorUtilities.CreateInstance(_serviceProvider, agentType, context);
_agents.TryAdd((agentId.Type, agentId.Key), agent);
}
Expand Down
4 changes: 2 additions & 2 deletions dotnet/test/Microsoft.AutoGen.Agents.Tests/AgentBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AgentBaseTests
[Fact]
public async Task ItInvokeRightHandlerTestAsync()
{
var mockContext = new Mock<IAgentContext>();
var mockContext = new Mock<IAgentRuntime>();
var agent = new TestAgent(mockContext.Object, new EventTypes(TypeRegistry.Empty, [], []), new Logger<AgentBase>(new LoggerFactory()));

await agent.HandleObject("hello world");
Expand All @@ -31,7 +31,7 @@ public async Task ItInvokeRightHandlerTestAsync()
/// </summary>
public class TestAgent : AgentBase, IHandle<string>, IHandle<int>
{
public TestAgent(IAgentContext context, EventTypes eventTypes, Logger<AgentBase> logger) : base(context, eventTypes, logger)
public TestAgent(IAgentRuntime context, EventTypes eventTypes, Logger<AgentBase> logger) : base(context, eventTypes, logger)
{
}

Expand Down

0 comments on commit 81a79d5

Please sign in to comment.