mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
7dee184ae4
* Organize the .Net samples * Organize the .Net samples * Merge latest from main * Update sample to also include function calling telemetry (#577) * Move package installation instructions to user-guide (#572) * Move package installation instructions to user-guide * Update user-documentation-dotnet/getting-started/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update docs/docs-templates/getting-started/README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * .NET: Add SK-AF Migration Samples for Responses API. (#575) * Responses wip * Adding OpenAI Responses Migration samples * Address all samples and code for Azure and OpenAI Responses Migration code * Update dotnet/samples/SemanticKernelMigration/OpenAIResponses/Step02_ReasoningModel/Program.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Organize the .Net samples * Organize the .Net samples * Merge latest from main * Use Agent rather than AIAgent * Rename agents getting started samples * Use singular Agent --------- Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
128 lines
5.3 KiB
C#
128 lines
5.3 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System.Text.Json;
|
|
|
|
namespace Microsoft.Extensions.AI.Agents.Runtime.Samples;
|
|
|
|
/// <summary>
|
|
/// Example demonstrating how to use the InMemoryActorStateStorage.
|
|
/// </summary>
|
|
public static class InMemoryActorStateStorageExample
|
|
{
|
|
/// <summary>
|
|
/// Demonstrates the basic usage of InMemoryActorStateStorage.
|
|
/// </summary>
|
|
/// <returns>A task representing the asynchronous operation.</returns>
|
|
public static async Task RunAsync()
|
|
{
|
|
// Create an in-memory actor state storage
|
|
var storage = new InMemoryActorStateStorage();
|
|
|
|
// Create an actor ID
|
|
var actorId = new ActorId("ExampleActor", "instance1");
|
|
|
|
Console.WriteLine("=== InMemoryActorStateStorage Example ===");
|
|
Console.WriteLine();
|
|
|
|
// 1. Write some initial state
|
|
Console.WriteLine("1. Writing initial state...");
|
|
var initialOperations = new List<ActorStateWriteOperation>
|
|
{
|
|
new SetValueOperation("name", JsonSerializer.SerializeToElement("John Doe")),
|
|
new SetValueOperation("age", JsonSerializer.SerializeToElement(30)),
|
|
new SetValueOperation("city", JsonSerializer.SerializeToElement("Seattle"))
|
|
};
|
|
|
|
var writeResult = await storage.WriteStateAsync(actorId, initialOperations, "0").ConfigureAwait(false);
|
|
Console.WriteLine($" Write successful: {writeResult.Success}");
|
|
Console.WriteLine($" New ETag: {writeResult.ETag}");
|
|
Console.WriteLine($" Actor count: {storage.ActorCount}");
|
|
Console.WriteLine($" Key count for actor: {storage.GetKeyCount(actorId)}");
|
|
Console.WriteLine();
|
|
|
|
// 2. Read the state back
|
|
Console.WriteLine("2. Reading state back...");
|
|
var readOperations = new List<ActorStateReadOperation>
|
|
{
|
|
new GetValueOperation("name"),
|
|
new GetValueOperation("age"),
|
|
new GetValueOperation("city"),
|
|
new GetValueOperation("nonexistent"), // This won't exist
|
|
new ListKeysOperation(continuationToken: null) // List all keys
|
|
};
|
|
|
|
var readResult = await storage.ReadStateAsync(actorId, readOperations).ConfigureAwait(false);
|
|
Console.WriteLine($" Current ETag: {readResult.ETag}");
|
|
Console.WriteLine(" Results:");
|
|
|
|
foreach (var result in readResult.Results)
|
|
{
|
|
switch (result)
|
|
{
|
|
case GetValueResult getValue:
|
|
Console.WriteLine($" - Get value: {getValue.Value?.ToString() ?? "null"}");
|
|
break;
|
|
|
|
case ListKeysResult listKeys:
|
|
Console.WriteLine($" - Keys: [{string.Join(", ", listKeys.Keys)}]");
|
|
break;
|
|
}
|
|
}
|
|
Console.WriteLine();
|
|
|
|
// 3. Update some values
|
|
Console.WriteLine("3. Updating state...");
|
|
var updateOperations = new List<ActorStateWriteOperation>
|
|
{
|
|
new SetValueOperation("age", JsonSerializer.SerializeToElement(31)), // Update age
|
|
new SetValueOperation("email", JsonSerializer.SerializeToElement("john@example.com")), // Add email
|
|
new RemoveKeyOperation("city") // Remove city
|
|
};
|
|
|
|
var updateResult = await storage.WriteStateAsync(actorId, updateOperations, writeResult.ETag).ConfigureAwait(false);
|
|
Console.WriteLine($" Update successful: {updateResult.Success}");
|
|
Console.WriteLine($" New ETag: {updateResult.ETag}");
|
|
Console.WriteLine($" Key count for actor: {storage.GetKeyCount(actorId)}");
|
|
Console.WriteLine();
|
|
|
|
// 4. Try to update with wrong ETag (should fail)
|
|
Console.WriteLine("4. Trying to update with wrong ETag...");
|
|
var failingOperations = new List<ActorStateWriteOperation>
|
|
{
|
|
new SetValueOperation("shouldFail", JsonSerializer.SerializeToElement("this should fail"))
|
|
};
|
|
|
|
var failResult = await storage.WriteStateAsync(actorId, failingOperations, "wrong-etag").ConfigureAwait(false);
|
|
Console.WriteLine($" Update successful: {failResult.Success}");
|
|
Console.WriteLine($" Current ETag: {failResult.ETag}");
|
|
Console.WriteLine();
|
|
|
|
// 5. Read final state
|
|
Console.WriteLine("5. Reading final state...");
|
|
var finalReadOperations = new List<ActorStateReadOperation>
|
|
{
|
|
new ListKeysOperation(continuationToken: null)
|
|
};
|
|
|
|
var finalReadResult = await storage.ReadStateAsync(actorId, finalReadOperations).ConfigureAwait(false);
|
|
var finalKeys = finalReadResult.Results.OfType<ListKeysResult>().First();
|
|
Console.WriteLine($" Final keys: [{string.Join(", ", finalKeys.Keys)}]");
|
|
|
|
// 6. Read each value
|
|
foreach (var key in finalKeys.Keys)
|
|
{
|
|
var valueReadOperations = new List<ActorStateReadOperation>
|
|
{
|
|
new GetValueOperation(key)
|
|
};
|
|
|
|
var valueReadResult = await storage.ReadStateAsync(actorId, valueReadOperations).ConfigureAwait(false);
|
|
var getValue = valueReadResult.Results.OfType<GetValueResult>().First();
|
|
Console.WriteLine($" - {key}: {getValue.Value}");
|
|
}
|
|
|
|
Console.WriteLine();
|
|
Console.WriteLine("=== Example Complete ===");
|
|
}
|
|
}
|