You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

AgentClient.cs 1.8 kB

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Diagnostics;
  2. using Agents;
  3. using Microsoft.AutoGen.Agents.Worker.Client;
  4. using AgentId = Microsoft.AutoGen.Agents.Worker.Client.AgentId;
  5. namespace Greeter.AgentWorker;
  6. public sealed class AgentClient(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator, EventTypes typeRegistry) : AgentBase(new ClientContext(logger, runtime, distributedContextPropagator), typeRegistry)
  7. {
  8. public async ValueTask PublishEventAsync(CloudEvent @event) => await PublishEvent(@event);
  9. public async ValueTask<RpcResponse> SendRequestAsync(AgentId target, string method, Dictionary<string, string> parameters) => await RequestAsync(target, method, parameters);
  10. private sealed class ClientContext(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator) : IAgentContext
  11. {
  12. public AgentId AgentId { get; } = new AgentId("client", Guid.NewGuid().ToString());
  13. public AgentBase? AgentInstance { get; set; }
  14. public ILogger Logger { get; } = logger;
  15. public DistributedContextPropagator DistributedContextPropagator { get; } = distributedContextPropagator;
  16. public async ValueTask PublishEventAsync(CloudEvent @event)
  17. {
  18. await runtime.PublishEvent(@event).ConfigureAwait(false);
  19. }
  20. public async ValueTask SendRequestAsync(AgentBase agent, RpcRequest request)
  21. {
  22. await runtime.SendRequest(AgentInstance!, request).ConfigureAwait(false);
  23. }
  24. public async ValueTask SendResponseAsync(RpcRequest request, RpcResponse response)
  25. {
  26. await runtime.SendResponse(response).ConfigureAwait(false);
  27. }
  28. }
  29. }