Skip to content

A programming framework for agentic AI

Use a root json element instead of dict (#5430)

Jack Gerrits362d6a4e6b38
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/AgentProxy.cs b/dotnet/src/Microsoft.AutoGen/Contracts/AgentProxy.cs
index d37d6284b..a3700adc7 100644
--- a/dotnet/src/Microsoft.AutoGen/Contracts/AgentProxy.cs
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/AgentProxy.cs
@@ -57,7 +57,7 @@ public class AgentProxy(AgentId agentId, IAgentRuntime runtime)
/// </summary>
/// <param name="state">A dictionary representing the state of the agent. Must be JSON serializable.</param>
/// <returns>A task representing the asynchronous operation.</returns>
- public ValueTask LoadStateAsync(IDictionary<string, JsonElement> state)
+ public ValueTask LoadStateAsync(JsonElement state)
{
return this.runtime.LoadAgentStateAsync(this.Id, state);
}
@@ -66,7 +66,7 @@ public class AgentProxy(AgentId agentId, IAgentRuntime runtime)
/// Saves the state of the agent. The result must be JSON serializable.
/// </summary>
/// <returns>A task representing the asynchronous operation, returning a dictionary containing the saved state.</returns>
- public ValueTask<IDictionary<string, JsonElement>> SaveStateAsync()
+ public ValueTask<JsonElement> SaveStateAsync()
{
return this.runtime.SaveAgentStateAsync(this.Id);
}
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.cs
index c4b2e998f..59bc3180f 100644
--- a/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.cs
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/IAgentRuntime.cs
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// IAgentRuntime.cs
-using StateDict = System.Collections.Generic.IDictionary<string, System.Text.Json.JsonElement>;
+using System.Text.Json;
namespace Microsoft.AutoGen.Contracts;
@@ -70,7 +70,7 @@ public interface IAgentRuntime : ISaveState<IAgentRuntime>
/// </summary>
/// <param name="agentId">The ID of the agent whose state is being saved.</param>
/// <returns>A task representing the asynchronous operation, returning a dictionary of the saved state.</returns>
- public ValueTask<StateDict> SaveAgentStateAsync(AgentId agentId/*, CancellationToken? cancellationToken = default*/);
+ public ValueTask<JsonElement> SaveAgentStateAsync(AgentId agentId/*, CancellationToken? cancellationToken = default*/);
/// <summary>
/// Loads the saved state into an agent.
@@ -78,7 +78,7 @@ public interface IAgentRuntime : ISaveState<IAgentRuntime>
/// <param name="agentId">The ID of the agent whose state is being restored.</param>
/// <param name="state">The state dictionary to restore.</param>
/// <returns>A task representing the asynchronous operation.</returns>
- public ValueTask LoadAgentStateAsync(AgentId agentId, StateDict state/*, CancellationToken? cancellationToken = default*/);
+ public ValueTask LoadAgentStateAsync(AgentId agentId, JsonElement state/*, CancellationToken? cancellationToken = default*/);
/// <summary>
/// Retrieves metadata for an agent.
diff --git a/dotnet/src/Microsoft.AutoGen/Contracts/ISaveState.cs b/dotnet/src/Microsoft.AutoGen/Contracts/ISaveState.cs
index 4f98f1fc4..3a4c55606 100644
--- a/dotnet/src/Microsoft.AutoGen/Contracts/ISaveState.cs
+++ b/dotnet/src/Microsoft.AutoGen/Contracts/ISaveState.cs
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// ISaveState.cs
-using StateDict = System.Collections.Generic.IDictionary<string, System.Text.Json.JsonElement>;
+using System.Text.Json;
namespace Microsoft.AutoGen.Contracts;
@@ -20,7 +20,7 @@ public interface ISaveState<T>
/// containing the saved state. The structure of the state is implementation-defined
/// but must be JSON serializable.
/// </returns>
- public ValueTask<StateDict> SaveStateAsync();
+ public ValueTask<JsonElement> SaveStateAsync();
/// <summary>
/// Loads a previously saved state into the object.
@@ -30,6 +30,6 @@ public interface ISaveState<T>
/// is implementation-defined but must be JSON serializable.
/// </param>
/// <returns>A task representing the asynchronous operation.</returns>
- public ValueTask LoadStateAsync(StateDict state);
+ public ValueTask LoadStateAsync(JsonElement state);
}
diff --git a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs
index 461148843..f3dc619fd 100644
--- a/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs
@@ -320,13 +320,13 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi
public ValueTask<Contracts.AgentId> GetAgentAsync(string agent, string key = "default", bool lazy = true)
=> this.GetAgentAsync(new Contracts.AgentId(agent, key), lazy);
- public async ValueTask<IDictionary<string, JsonElement>> SaveAgentStateAsync(Contracts.AgentId agentId)
+ public async ValueTask<JsonElement> SaveAgentStateAsync(Contracts.AgentId agentId)
{
IHostableAgent agent = await this._agentsContainer.EnsureAgentAsync(agentId);
return await agent.SaveStateAsync();
}
- public async ValueTask LoadAgentStateAsync(Contracts.AgentId agentId, IDictionary<string, JsonElement> state)
+ public async ValueTask LoadAgentStateAsync(Contracts.AgentId agentId, JsonElement state)
{
IHostableAgent agent = await this._agentsContainer.EnsureAgentAsync(agentId);
await agent.LoadStateAsync(state);
@@ -376,31 +376,28 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi
return ValueTask.FromResult(new AgentProxy(agentId, this));
}
- public async ValueTask LoadStateAsync(IDictionary<string, JsonElement> state)
+ public async ValueTask LoadStateAsync(JsonElement state)
{
HashSet<AgentType> registeredTypes = this._agentsContainer.RegisteredAgentTypes;
- foreach (var agentIdStr in state.Keys)
+ foreach (var agentIdStr in state.EnumerateObject())
{
- Contracts.AgentId agentId = Contracts.AgentId.FromStr(agentIdStr);
+ Contracts.AgentId agentId = Contracts.AgentId.FromStr(agentIdStr.Name);
- if (state[agentIdStr].ValueKind != JsonValueKind.Object)
+ if (agentIdStr.Value.ValueKind != JsonValueKind.Object)
{
throw new Exception($"Agent state for {agentId} is not a valid JSON object.");
}
- var agentState = JsonSerializer.Deserialize<IDictionary<string, JsonElement>>(state[agentIdStr].GetRawText())
- ?? throw new Exception($"Failed to deserialize state for {agentId}.");
-
if (registeredTypes.Contains(agentId.Type))
{
IHostableAgent agent = await this._agentsContainer.EnsureAgentAsync(agentId);
- await agent.LoadStateAsync(agentState);
+ await agent.LoadStateAsync(agentIdStr.Value);
}
}
}
- public async ValueTask<IDictionary<string, JsonElement>> SaveStateAsync()
+ public async ValueTask<JsonElement> SaveStateAsync()
{
Dictionary<string, JsonElement> state = new();
foreach (var agent in this._agentsContainer.LiveAgents)
@@ -408,7 +405,7 @@ public sealed class GrpcAgentRuntime : IHostedService, IAgentRuntime, IMessageSi
var agentState = await agent.SaveStateAsync();
state[agent.Id.ToString()] = JsonSerializer.SerializeToElement(agentState);
}
- return state;
+ return JsonSerializer.SerializeToElement(state);
}
public async ValueTask OnMessageAsync(Message message, CancellationToken cancellation = default)
diff --git a/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs b/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs
index a3899280f..457c2cc6a 100644
--- a/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core/BaseAgent.cs
@@ -93,11 +93,11 @@ public abstract class BaseAgent : IAgent, IHostableAgent
return null;
}
- public virtual ValueTask<IDictionary<string, JsonElement>> SaveStateAsync()
+ public virtual ValueTask<JsonElement> SaveStateAsync()
{
- return ValueTask.FromResult<IDictionary<string, JsonElement>>(new Dictionary<string, JsonElement>());
+ return ValueTask.FromResult(JsonDocument.Parse("{}").RootElement);
}
- public virtual ValueTask LoadStateAsync(IDictionary<string, JsonElement> state)
+ public virtual ValueTask LoadStateAsync(JsonElement state)
{
return ValueTask.CompletedTask;
}
diff --git a/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs b/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs
index 9acf96e64..fdd390d2d 100644
--- a/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core/InProcessRuntime.cs
@@ -153,13 +153,13 @@ public sealed class InProcessRuntime : IAgentRuntime, IHostedService
return agent.Metadata;
}
- public async ValueTask LoadAgentStateAsync(AgentId agentId, IDictionary<string, JsonElement> state)
+ public async ValueTask LoadAgentStateAsync(AgentId agentId, JsonElement state)
{
IHostableAgent agent = await this.EnsureAgentAsync(agentId);
await agent.LoadStateAsync(state);
}
- public async ValueTask<IDictionary<string, JsonElement>> SaveAgentStateAsync(AgentId agentId)
+ public async ValueTask<JsonElement> SaveAgentStateAsync(AgentId agentId)
{
IHostableAgent agent = await this.EnsureAgentAsync(agentId);
return await agent.SaveStateAsync();
@@ -188,38 +188,34 @@ public sealed class InProcessRuntime : IAgentRuntime, IHostedService
return ValueTask.CompletedTask;
}
- public async ValueTask LoadStateAsync(IDictionary<string, JsonElement> state)
+ public async ValueTask LoadStateAsync(JsonElement state)
{
- foreach (var agentIdStr in state.Keys)
+ foreach (var agentIdStr in state.EnumerateObject())
{
- AgentId agentId = AgentId.FromStr(agentIdStr);
+ AgentId agentId = AgentId.FromStr(agentIdStr.Name);
- if (state[agentIdStr].ValueKind != JsonValueKind.Object)
+ if (agentIdStr.Value.ValueKind != JsonValueKind.Object)
{
throw new Exception($"Agent state for {agentId} is not a valid JSON object.");
}
- // Deserialize before using
- var agentState = JsonSerializer.Deserialize<IDictionary<string, JsonElement>>(state[agentIdStr].GetRawText())
- ?? throw new Exception($"Failed to deserialize state for {agentId}.");
-
if (this.agentFactories.ContainsKey(agentId.Type))
{
IHostableAgent agent = await this.EnsureAgentAsync(agentId);
- await agent.LoadStateAsync(agentState);
+ await agent.LoadStateAsync(agentIdStr.Value);
}
}
}
- public async ValueTask<IDictionary<string, JsonElement>> SaveStateAsync()
+ public async ValueTask<JsonElement> SaveStateAsync()
{
Dictionary<string, JsonElement> state = new();
foreach (var agentId in this.agentInstances.Keys)
{
var agentState = await this.agentInstances[agentId].SaveStateAsync();
- state[agentId.ToString()] = JsonSerializer.SerializeToElement(agentState);
+ state[agentId.ToString()] = agentState;
}
- return state;
+ return JsonSerializer.SerializeToElement(state);
}
public ValueTask<AgentType> RegisterAgentFactoryAsync<TAgent>(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<TAgent>> factoryFunc) where TAgent : IHostableAgent
diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/InProcessRuntimeTests.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/InProcessRuntimeTests.cs
index 174f8b781..6a42bb568 100644
--- a/dotnet/test/Microsoft.AutoGen.Core.Tests/InProcessRuntimeTests.cs
+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/InProcessRuntimeTests.cs
@@ -107,11 +107,11 @@ public class InProcessRuntimeTests()
// Save the state
var savedState = await runtime.SaveStateAsync();
- // Ensure saved state contains the agent's state
- savedState.Should().ContainKey(agentId.ToString());
+ // Ensure calling TryGetPropertyValue with the agent's key returns the agent's state
+ savedState.TryGetProperty(agentId.ToString(), out var agentState).Should().BeTrue("Agent state should be saved");
// Ensure the agent's state is stored as a valid JSON object
- savedState[agentId.ToString()].ValueKind.Should().Be(JsonValueKind.Object, "Agent state should be stored as a JSON object");
+ agentState.ValueKind.Should().Be(JsonValueKind.Object, "Agent state should be stored as a JSON object");
// Serialize and Deserialize the state to simulate persistence
string json = JsonSerializer.Serialize(savedState);
@@ -134,7 +134,7 @@ public class InProcessRuntimeTests()
newRuntime.agentInstances.Count.Should().Be(0, "Agent should be registered in the new runtime");
// Load the state into the new runtime and show that agent is now instantiated
- await newRuntime.LoadStateAsync(deserializedState);
+ await newRuntime.LoadStateAsync(savedState);
newRuntime.agentInstances.Count.Should().Be(1, "Agent should be registered in the new runtime");
newRuntime.agentInstances.Should().ContainKey(agentId, "Agent should be loaded into the new runtime");
}
diff --git a/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs b/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
index ed87a7105..6c1951946 100644
--- a/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
+++ b/dotnet/test/Microsoft.AutoGen.Core.Tests/TestAgent.cs
@@ -83,25 +83,15 @@ public class SubscribedSaveLoadAgent : TestAgent
{
}
- public override ValueTask<IDictionary<string, JsonElement>> SaveStateAsync()
+ public override ValueTask<JsonElement> SaveStateAsync()
{
- var jsonSafeDictionary = _receivedMessages.ToDictionary(
- kvp => kvp.Key,
- kvp => JsonSerializer.SerializeToElement(kvp.Value) // Convert each object to JsonElement
- );
-
- return ValueTask.FromResult<IDictionary<string, JsonElement>>(jsonSafeDictionary);
+ var jsonDoc = JsonSerializer.SerializeToElement(_receivedMessages);
+ return ValueTask.FromResult(jsonDoc);
}
- public override ValueTask LoadStateAsync(IDictionary<string, JsonElement> state)
+ public override ValueTask LoadStateAsync(JsonElement state)
{
- _receivedMessages.Clear();
-
- foreach (var kvp in state)
- {
- _receivedMessages[kvp.Key] = kvp.Value.Deserialize<object>() ?? throw new Exception($"Failed to deserialize key: {kvp.Key}");
- }
-
+ _receivedMessages = JsonSerializer.Deserialize<Dictionary<string, object>>(state) ?? throw new InvalidOperationException("Failed to deserialize state");
return ValueTask.CompletedTask;
}
}