Skip to content

A programming framework for agentic AI

add python equiv in core

Jack Gerrits0497592e8a2d
diff --git a/dotnet/src/Microsoft.AutoGen/Core/PythonEquiv/IHandle.cs b/dotnet/src/Microsoft.AutoGen/Core/PythonEquiv/IHandle.cs
new file mode 100644
index 000000000..c86e73dea
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Core/PythonEquiv/IHandle.cs
@@ -0,0 +1,30 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// IHandle.cs
+
+using Google.Protobuf;
+
+namespace Microsoft.AutoGen.Core.Python;
+
+/// <summary>
+/// Defines a handler interface for processing items of type <typeparamref name="T"/>.
+/// </summary>
+/// <typeparam name="T">The type of item to be handled, which must implement <see cref="IMessage"/>.</typeparam>
+public interface IHandle<in T> where T : IMessage
+{
+ /// <summary>
+ /// Handles the specified item asynchronously.
+ /// </summary>
+ /// <param name="item">The item to be handled.</param>
+ /// <returns>A task that represents the asynchronous operation.</returns>
+ Task Handle(T item, CancellationToken cancellationToken = default);
+}
+
+public interface IHandle<in InT, OutT> where InT : IMessage where OutT : IMessage
+{
+ /// <summary>
+ /// Handles the specified item asynchronously.
+ /// </summary>
+ /// <param name="item">The item to be handled.</param>
+ /// <returns>A task that represents the asynchronous operation.</returns>
+ Task<OutT> Handle(InT item, CancellationToken cancellationToken = default);
+}
diff --git a/dotnet/src/Microsoft.AutoGen/Core/PythonEquiv/IHandleExtensions.cs b/dotnet/src/Microsoft.AutoGen/Core/PythonEquiv/IHandleExtensions.cs
new file mode 100644
index 000000000..5053e5965
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Core/PythonEquiv/IHandleExtensions.cs
@@ -0,0 +1,38 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// IHandleExtensions.cs
+
+using System.Reflection;
+
+namespace Microsoft.AutoGen.Core.Python;
+
+/// <summary>
+/// Provides extension methods for types implementing the IHandle interface.
+/// </summary>
+public static class IHandleExtensions
+{
+ /// <summary>
+ /// Gets all the handler methods from the interfaces implemented by the specified type.
+ /// </summary>
+ /// <param name="type">The type to get the handler methods from.</param>
+ /// <returns>An array of MethodInfo objects representing the handler methods.</returns>
+ public static MethodInfo[] GetHandlers(this Type type)
+ {
+ var handlers = type.GetInterfaces().Where(i => i.IsGenericType && (i.GetGenericTypeDefinition() == typeof(IHandle<>) || i.GetGenericTypeDefinition() == typeof(IHandle<,>)));
+ return handlers.SelectMany(h => h.GetMethods().Where(m => m.Name == "Handle")).ToArray();
+ }
+
+ /// <summary>
+ /// Gets a lookup table of handler methods from the interfaces implemented by the specified type.
+ /// </summary>
+ /// <param name="type">The type to get the handler methods from.</param>
+ /// <returns>A dictionary where the key is the generic type and the value is the MethodInfo of the handler method.</returns>
+ public static Dictionary<Type, MethodInfo> GetHandlersLookupTable(this Type type)
+ {
+ var handlers = type.GetHandlers();
+ return handlers.ToDictionary(h =>
+ {
+ var generic = h.DeclaringType!.GetGenericArguments();
+ return generic[0];
+ });
+ }
+}