Skip to content

A programming framework for agentic AI

feat: Implement mechanism to abstract over IHandle implementations

Jacob Alberd9e001397589
diff --git a/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs b/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs
index 65e96bee7..e101e1cde 100644
--- a/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs
@@ -8,6 +8,7 @@ using Microsoft.AutoGen.Contracts;
using Microsoft.Extensions.Logging;
namespace Microsoft.AutoGen.Core;
+
/// <summary>
/// Represents the base class for an agent in the AutoGen system.
/// </summary>
diff --git a/dotnet/src/Microsoft.AutoGen/Core/HandlerInvoker.cs b/dotnet/src/Microsoft.AutoGen/Core/HandlerInvoker.cs
new file mode 100644
index 000000000..e60d5f6bf
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Core/HandlerInvoker.cs
@@ -0,0 +1,78 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// HandlerInvoker.cs
+
+using System.Diagnostics;
+using System.Reflection;
+using Microsoft.AutoGen.Contracts;
+
+namespace Microsoft.AutoGen.Core;
+
+public class HandlerInvoker
+{
+ private static async ValueTask<object?> TypeEraseAwait<T>(ValueTask<T> vt)
+ {
+ return await vt;
+ }
+
+ public HandlerInvoker(MethodInfo methodInfo, object? target = null)
+ {
+ // TODO: Check that the MethodInfo params check out?
+
+ Func<object?, MessageContext, object?> invocation;
+ if (target != null)
+ {
+ invocation = (object? message, MessageContext messageContext) => methodInfo.Invoke(target, new object?[] { message, messageContext });
+ }
+ else if (methodInfo.IsStatic)
+ {
+ invocation = (object? message, MessageContext messageContext) => methodInfo.Invoke(null, new object?[] { message, messageContext });
+ }
+ else
+ {
+ throw new InvalidOperationException("Target must be provided for non-static methods");
+ }
+
+ Func<object?, MessageContext, ValueTask<object?>> getResultAsync;
+ if (methodInfo.ReturnType.IsAssignableFrom(typeof(ValueTask)))
+ {
+ getResultAsync = async
+ (object? message, MessageContext messageContext) =>
+ {
+ await (ValueTask)invocation(message, messageContext)!;
+ return null;
+ };
+ }
+ else if (
+ methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(ValueTask<>)
+ )
+ {
+ getResultAsync = async
+ (object? message, MessageContext messageContext) =>
+ {
+ object valueTask = invocation(message, messageContext)!;
+
+ object? typelessValueTask = typeof(HandlerInvoker)
+ .GetMethod(nameof(TypeEraseAwait), BindingFlags.NonPublic | BindingFlags.Static)!
+ .MakeGenericMethod(methodInfo.ReturnType.GetGenericArguments()[0])
+ .Invoke(null, new object[] { valueTask });
+
+ Debug.Assert(typelessValueTask is ValueTask<object?>);
+
+ return await (ValueTask<object?>)typelessValueTask;
+ };
+ }
+ else
+ {
+ throw new InvalidOperationException($"Method {methodInfo.Name} must return a ValueTask or ValueTask<T>");
+ }
+
+ this.Invocation = getResultAsync;
+ }
+
+ private Func<object?, MessageContext, ValueTask<object?>> Invocation { get; }
+
+ public ValueTask<object?> InvokeAsync(object? obj, MessageContext messageContext)
+ {
+ return this.Invocation(obj, messageContext);
+ }
+}
diff --git a/dotnet/src/Microsoft.AutoGen/Core/ReflectionHelper.cs b/dotnet/src/Microsoft.AutoGen/Core/ReflectionHelper.cs
index 395162719..17a3844c2 100644
--- a/dotnet/src/Microsoft.AutoGen/Core/ReflectionHelper.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core/ReflectionHelper.cs
@@ -19,6 +19,7 @@ public sealed class ReflectionHelper
}
type = type.BaseType;
}
+
return false;
}
}
diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/HandlerInvokerTest.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/HandlerInvokerTest.cs
new file mode 100644
index 000000000..7387366b5
--- /dev/null
+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/HandlerInvokerTest.cs
@@ -0,0 +1,57 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// HandlerInvokerTest.cs
+
+using FluentAssertions;
+using Microsoft.AutoGen.Contracts;
+using Xunit;
+
+namespace Microsoft.AutoGen.Core.Tests;
+
+public class HandlerInvokerTest()
+{
+ public List<(string, MessageContext)> PublishlikeInvocations = new List<(string, MessageContext)>();
+
+ public ValueTask PublishlikeAsync(string message, MessageContext messageContext)
+ {
+ this.PublishlikeInvocations.Add((message, messageContext));
+ return ValueTask.CompletedTask;
+ }
+
+ public List<(string, MessageContext)> SendlikeInvocations = new List<(string, MessageContext)>();
+
+ public ValueTask<int> SendlikeAsync(string message, MessageContext messageContext)
+ {
+ this.SendlikeInvocations.Add((message, messageContext));
+ return ValueTask.FromResult(this.SendlikeInvocations.Count);
+ }
+
+ [Fact]
+ public async Task Test_InvokingPublishlike_Succeeds()
+ {
+ MessageContext messageContext = new MessageContext(Guid.NewGuid().ToString(), CancellationToken.None);
+
+ var methodInfo = typeof(HandlerInvokerTest).GetMethod(nameof(PublishlikeAsync))!;
+ var invoker = new HandlerInvoker(methodInfo, this);
+
+ object? result = await invoker.InvokeAsync("Hello, world!", messageContext);
+
+ this.PublishlikeInvocations.Should().HaveCount(1);
+ this.PublishlikeInvocations[0].Item1.Should().Be("Hello, world!");
+ result.Should().BeNull();
+ }
+
+ [Fact]
+ public async Task Test_InvokingSendlike_Succeeds()
+ {
+ MessageContext messageContext = new MessageContext(Guid.NewGuid().ToString(), CancellationToken.None);
+
+ var methodInfo = typeof(HandlerInvokerTest).GetMethod(nameof(SendlikeAsync))!;
+ var invoker = new HandlerInvoker(methodInfo, this);
+
+ object? result = await invoker.InvokeAsync("Hello, world!", messageContext);
+
+ this.SendlikeInvocations.Should().HaveCount(1);
+ this.SendlikeInvocations[0].Item1.Should().Be("Hello, world!");
+ result.Should().Be(1);
+ }
+}