Ryan Sweet 7d7fc8a912
.NET cleanup and refactor (#558)
Moves some shared code from samples into core.
complete/cleanup the rename to Microsoft.AutoGen
adds new projects in AutoGen.Extensions
2024-09-18 11:57:51 -07:00

39 lines
1.8 KiB
C#

using System.Diagnostics;
using Agents;
using Microsoft.AutoGen.Agents.Worker.Client;
using AgentId = Microsoft.AutoGen.Agents.Worker.Client.AgentId;
namespace DevTeam.Backend;
// TODO: Extract this to be part of the Client
public sealed class AgentClient(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator,
[FromKeyedServices("EventTypes")] EventTypes eventTypes)
: AgentBase(new ClientContext(logger, runtime, distributedContextPropagator), eventTypes )
{
public async ValueTask PublishEventAsync(CloudEvent evt) => await PublishEvent(evt);
public async ValueTask<RpcResponse> SendRequestAsync(AgentId target, string method, Dictionary<string, string> parameters) => await RequestAsync(target, method, parameters);
private sealed class ClientContext(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator) : IAgentContext
{
public AgentId AgentId { get; } = new AgentId("client", Guid.NewGuid().ToString());
public AgentBase? AgentInstance { get; set; }
public ILogger Logger { get; } = logger;
public DistributedContextPropagator DistributedContextPropagator { get; } = distributedContextPropagator;
public async ValueTask PublishEventAsync(CloudEvent @event)
{
await runtime.PublishEvent(@event).ConfigureAwait(false);
}
public async ValueTask SendRequestAsync(AgentBase agent, RpcRequest request)
{
await runtime.SendRequest(AgentInstance!, request).ConfigureAwait(false);
}
public async ValueTask SendResponseAsync(RpcRequest request, RpcResponse response)
{
await runtime.SendResponse(response).ConfigureAwait(false);
}
}
}