Skip to content

A programming framework for agentic AI

feat: Python-equivalent Interfaces

Jacob Alber0100201dd411
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentId.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentId.cs
new file mode 100644
index 000000000..23d56eee9
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentId.cs
@@ -0,0 +1,50 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+[DebuggerDisplay($"AgentId(type=\"{nameof(Type)}\", key=\"{nameof(Key)}\")")]
+public struct AgentId
+{
+ public string Type;
+ public string Key;
+
+ public AgentId(string type, string key)
+ {
+ Type = type;
+ Key = key;
+ }
+
+ public AgentId((string Type, string Key) kvPair) : this(kvPair.Type, kvPair.Key)
+ {
+ }
+
+ public AgentId(AgentType type, string key) : this(type.Name, key)
+ {
+ }
+
+ public static AgentId FromStr(string maybeAgentId) => new AgentId(maybeAgentId.ToKVPair(nameof(Type), nameof(Key)));
+
+ public override string ToString() => $"{Type}/{Key}";
+
+ public override bool Equals([NotNullWhen(true)] object? obj)
+ {
+ if (obj is AgentId other)
+ {
+ return Type == other.Type && Key == other.Key;
+ }
+
+ return false;
+ }
+
+ public override int GetHashCode()
+ {
+ return HashCode.Combine(Type, Key);
+ }
+
+ public static explicit operator AgentId(string id) => FromStr(id);
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentMetadata.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentMetadata.cs
new file mode 100644
index 000000000..e4e396aa1
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentMetadata.cs
@@ -0,0 +1,12 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public struct AgentMetadata(string type, string key, string description)
+{
+ public string Type { get; set; } = type;
+ public string Key { get; set; } = key;
+ public string Description { get; set; } = description;
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentProxy.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentProxy.cs
new file mode 100644
index 000000000..8c11214bf
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentProxy.cs
@@ -0,0 +1,33 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// AgentProxy.cs
+
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public class AgentProxy(AgentId agentId, IAgentRuntime runtime) //: IAgent
+{
+ private IAgentRuntime runtime = runtime;
+ public AgentId Id = agentId;
+
+ private T ExecuteAndUnwrap<T>(Func<IAgentRuntime, ValueTask<T>> delegate_)
+ {
+ return delegate_(this.runtime).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();
+ }
+
+ public AgentMetadata Metadata => this.ExecuteAndUnwrap(runtime => runtime.GetAgentMetadataAsync(this.Id));
+
+ public ValueTask LoadStateAsync(IDictionary<string, object> state)
+ {
+ throw new NotImplementedException();
+ }
+
+ public ValueTask<object> OnMessageAsync(object message, MessageContext messageContext)
+ {
+ throw new NotImplementedException();
+ }
+
+ public ValueTask<IDictionary<string, object>> SaveStateAsync()
+ {
+ throw new NotImplementedException();
+ }
+}
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentRuntimeExtensions.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentRuntimeExtensions.cs
new file mode 100644
index 000000000..1461d9b70
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentRuntimeExtensions.cs
@@ -0,0 +1,29 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public static class AgentRuntimeExtensions
+{
+ internal static ValueTask<TAgent> ActivateAgentAsync<TAgent>(IServiceProvider serviceProvider, params object[] additionalArguments) where TAgent : IHostableAgent
+ {
+ try
+ {
+ var agent = (TAgent)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TAgent), additionalArguments);
+ return ValueTask.FromResult(agent);
+ }
+ catch (Exception e)
+ {
+ return ValueTask.FromException<TAgent>(e);
+ }
+ }
+
+ public static ValueTask<AgentType> RegisterAgentTypeAsync<TAgent>(this IAgentRuntime runtime, AgentType type, IServiceProvider serviceProvider, params object[] additionalArguments) where TAgent : IHostableAgent
+ {
+ Func<ValueTask<TAgent>> factory = () => ActivateAgentAsync<TAgent>(serviceProvider, additionalArguments);
+ return runtime.RegisterAgentFactoryAsync(type, factory);
+ }
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentType.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentType.cs
new file mode 100644
index 000000000..d40e5ef14
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/AgentType.cs
@@ -0,0 +1,15 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public struct AgentType
+{
+ public required string Name;
+
+ public static explicit operator AgentType(Type type) => new AgentType { Name = type.Name };
+
+ public static implicit operator AgentType(string type) => new AgentType { Name = type };
+ public static implicit operator string(AgentType type) => type.Name;
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/IAgent.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/IAgent.cs
new file mode 100644
index 000000000..36fd4a36c
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/IAgent.cs
@@ -0,0 +1,18 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// IAgent.cs
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public interface IAgent : ISaveState<IAgent>
+{
+ public AgentId Id { get; }
+ public AgentMetadata Metadata { get; }
+
+ public ValueTask<object> OnMessageAsync(object message, MessageContext messageContext); // TODO: How do we express this properly in .NET?
+}
+
+public interface IHostableAgent : IAgent
+{
+ public ValueTask CloseAsync();
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/IAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/IAgentRuntime.cs
new file mode 100644
index 000000000..d819cc4af
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/IAgentRuntime.cs
@@ -0,0 +1,34 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+using StateDict = System.Collections.Generic.IDictionary<string, object>;
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public interface IAgentRuntime : ISaveState<IAgentRuntime>
+{
+ public ValueTask<object> SendMessage(object message, AgentId recepient, AgentId? sender = null, string? messageId = null, CancellationToken? cancellationToken = default);
+ public ValueTask<object> PublishMessage(object message, TopicId topic, AgentId? sender = null, string? messageId = null, CancellationToken? cancellationToken = default);
+
+ public ValueTask<AgentId> GetAgentAsync(AgentId agentId, string key = "default", bool lazy = true/*, CancellationToken? = default*/);
+ public ValueTask<AgentId> GetAgentAsync(AgentType agentType, string key = "default", bool lazy = true/*, CancellationToken? = default*/);
+ public ValueTask<AgentId> GetAgentAsync(string agent, string key = "default", bool lazy = true/*, CancellationToken? = default*/);
+
+ public ValueTask<StateDict> SaveAgentStateAsync(/*CancellationToken? cancellationToken = default*/);
+ public ValueTask<StateDict> LoadAgentStateAsync(StateDict state/*, CancellationToken? cancellationToken = default*/);
+
+ public ValueTask<AgentMetadata> GetAgentMetadataAsync(AgentId agentId/*, CancellationToken? cancellationToken = default*/);
+
+ public ValueTask AddSubscriptionAsync(ISubscriptionDefinition subscription/*, CancellationToken? cancellationToken = default*/);
+ public ValueTask RemoveSubscriptionAsync(string subscriptionId/*, CancellationToken? cancellationToken = default*/);
+
+ public ValueTask<AgentType> RegisterAgentFactoryAsync<TAgent>(AgentType type, Func<ValueTask<TAgent>> factoryFunc) where TAgent : IHostableAgent;
+
+ // TODO:
+ public ValueTask<TAgent> TryGetUnderlyingAgentInstanceAsync<TAgent>(AgentId agentId) where TAgent : IHostableAgent;
+ public void AddMessageSerializer(params object[] serializers);
+
+ // Extras
+ public ValueTask<IAgent> TryGetAgentProxyAsync(AgentId agentId);
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/ISaveState.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/ISaveState.cs
new file mode 100644
index 000000000..172bfb364
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/ISaveState.cs
@@ -0,0 +1,13 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+using StateDict = System.Collections.Generic.IDictionary<string, object>;
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public interface ISaveState<T>
+{
+ public ValueTask<StateDict> SaveStateAsync();
+ public ValueTask LoadStateAsync(StateDict state);
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/ISubscriptionDefinition.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/ISubscriptionDefinition.cs
new file mode 100644
index 000000000..4bdf6a8b6
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/ISubscriptionDefinition.cs
@@ -0,0 +1,20 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+using System.Diagnostics.CodeAnalysis;
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public interface ISubscriptionDefinition
+{
+ public string Id { get; }
+
+ public bool Equals([NotNullWhen(true)] object? obj) => obj is ISubscriptionDefinition other && Equals(other);
+ public bool Equals(ISubscriptionDefinition? other) => Id == other?.Id;
+
+ public int GetHashCode() => Id.GetHashCode();
+
+ public bool Matches(TopicId topic);
+ public AgentId MapToAgent(TopicId topic);
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/KVStringParseHelper.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/KVStringParseHelper.cs
new file mode 100644
index 000000000..5d3615066
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/KVStringParseHelper.cs
@@ -0,0 +1,24 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+using System.Text.RegularExpressions;
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public static class KVStringParseHelper
+{
+ private const string KVPairPattern = @"^(?<key>\w+)/(?<value>\w+)$";
+ private static readonly Regex KVPairRegex = new Regex(KVPairPattern, RegexOptions.Compiled);
+
+ public static (string, string) ToKVPair(this string kvString, string keyName, string valueName)
+ {
+ var match = KVPairRegex.Match(kvString);
+ if (match.Success)
+ {
+ return (match.Groups["key"].Value, match.Groups["value"].Value);
+ }
+
+ throw new FormatException($"Invalid key-value pair format: {kvString}; expecting \"{{{keyName}}}/{{{valueName}}}\"");
+ }
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/MessageContext.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/MessageContext.cs
new file mode 100644
index 000000000..a1e5d1204
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/MessageContext.cs
@@ -0,0 +1,16 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public class MessageContext(string messageId, CancellationToken cancellationToken)
+{
+ public string MessageId { get; set; } = messageId;
+ public CancellationToken CancellationToken { get; set; } = cancellationToken;
+
+ public AgentId? Sender { get; set; }
+ public TopicId? Topic { get; set; }
+
+ public bool IsRpc { get; set; }
+}
+
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/TopicId.cs b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/TopicId.cs
new file mode 100644
index 000000000..89fbd0942
--- /dev/null
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/PythonEquiv/TopicId.cs
@@ -0,0 +1,44 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// PythonInterfaces.cs
+
+using System.Diagnostics.CodeAnalysis;
+
+namespace Microsoft.AutoGen.Contracts.Python;
+
+public struct TopicId
+{
+ public string Type { get; }
+ public string Source { get; }
+
+ public TopicId(string type, string source)
+ {
+ Type = type;
+ Source = source;
+ }
+
+ public TopicId((string Type, string Source) kvPair) : this(kvPair.Type, kvPair.Source)
+ {
+ }
+
+ public static TopicId FromStr(string maybeTopicId) => new TopicId(maybeTopicId.ToKVPair(nameof(Type), nameof(Source)));
+
+ public override string ToString() => $"{Type}/{Source}";
+
+ public override bool Equals([NotNullWhen(true)] object? obj)
+ {
+ if (obj is TopicId other)
+ {
+ return Type == other.Type && Source == other.Source;
+ }
+
+ return false;
+ }
+
+ public override int GetHashCode()
+ {
+ return HashCode.Combine(Type, Source);
+ }
+
+ public static explicit operator TopicId(string id) => FromStr(id);
+}
+