.NET: Make serialize methods sync and rename one to match others. (#946)

* Make serialize methods sync and rename one to match others.

* Remove unnecessary async postfixes.

* Remove nullability of ChatMessageStore.Serialize return type, since the default JsonElement already represents an undefined json element.

* Fix unit test
This commit is contained in:
westey
2025-09-29 09:32:01 +00:00
committed by GitHub
parent 7c5b553c7e
commit bf5931932e
21 changed files with 64 additions and 86 deletions
@@ -30,7 +30,7 @@ AgentThread thread = agent.GetNewThread();
Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", thread));
// Serialize the thread state to a JsonElement, so it can be stored for later use.
JsonElement serializedThread = await thread.SerializeAsync();
JsonElement serializedThread = thread.Serialize();
// Save the serialized thread to a temporary file (for demonstration purposes).
string tempFilePath = Path.GetTempFileName();
@@ -56,7 +56,7 @@ Console.WriteLine(await agent.RunAsync("Tell me a joke about a pirate.", thread)
// Serialize the thread state, so it can be stored for later use.
// Since the chat history is stored in the vector store, the serialized thread
// only contains the guid that the messages are stored under in the vector store.
JsonElement serializedThread = await thread.SerializeAsync();
JsonElement serializedThread = thread.Serialize();
Console.WriteLine("\n--- Serialized thread ---\n");
Console.WriteLine(JsonSerializer.Serialize(serializedThread, new JsonSerializerOptions { WriteIndented = true }));
@@ -131,9 +131,9 @@ namespace SampleApp
return messages;
}
public override ValueTask<JsonElement?> SerializeStateAsync(JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default) =>
public override JsonElement Serialize(JsonSerializerOptions? jsonSerializerOptions = null) =>
// We have to serialize the thread id, so that on deserialization we can retrieve the messages using the same thread id.
new(JsonSerializer.SerializeToElement(this.ThreadDbKey));
JsonSerializer.SerializeToElement(this.ThreadDbKey);
/// <summary>
/// The data structure used to store chat history items in the vector store.
@@ -52,7 +52,7 @@ Console.WriteLine(await agent.RunAsync("My name is Ruaidhrí", thread));
Console.WriteLine(await agent.RunAsync("I am 20 years old", thread));
// We can serialize the thread. The serialized state will include the state of the memory component.
var threadElement = await thread.SerializeAsync();
var threadElement = thread.Serialize();
Console.WriteLine("\n>> Use deserialized thread with previously created memories\n");
@@ -148,9 +148,9 @@ namespace SampleApp
});
}
public override ValueTask<JsonElement?> SerializeAsync(JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
public override JsonElement Serialize(JsonSerializerOptions? jsonSerializerOptions = null)
{
return new ValueTask<JsonElement?>(JsonSerializer.SerializeToElement(this.UserInfo, jsonSerializerOptions));
return JsonSerializer.SerializeToElement(this.UserInfo, jsonSerializerOptions);
}
}