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

1234567891011121314151617181920212223242526272829303132333435363738
  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 DevTeam.Backend;
  6. // TODO: Extract this to be part of the Client
  7. public sealed class AgentClient(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator,
  8. [FromKeyedServices("EventTypes")] EventTypes eventTypes)
  9. : AgentBase(new ClientContext(logger, runtime, distributedContextPropagator), eventTypes )
  10. {
  11. public async ValueTask PublishEventAsync(CloudEvent evt) => await PublishEvent(evt);
  12. public async ValueTask<RpcResponse> SendRequestAsync(AgentId target, string method, Dictionary<string, string> parameters) => await RequestAsync(target, method, parameters);
  13. private sealed class ClientContext(ILogger<AgentClient> logger, AgentWorkerRuntime runtime, DistributedContextPropagator distributedContextPropagator) : IAgentContext
  14. {
  15. public AgentId AgentId { get; } = new AgentId("client", Guid.NewGuid().ToString());
  16. public AgentBase? AgentInstance { get; set; }
  17. public ILogger Logger { get; } = logger;
  18. public DistributedContextPropagator DistributedContextPropagator { get; } = distributedContextPropagator;
  19. public async ValueTask PublishEventAsync(CloudEvent @event)
  20. {
  21. await runtime.PublishEvent(@event).ConfigureAwait(false);
  22. }
  23. public async ValueTask SendRequestAsync(AgentBase agent, RpcRequest request)
  24. {
  25. await runtime.SendRequest(AgentInstance!, request).ConfigureAwait(false);
  26. }
  27. public async ValueTask SendResponseAsync(RpcRequest request, RpcResponse response)
  28. {
  29. await runtime.SendResponse(response).ConfigureAwait(false);
  30. }
  31. }
  32. }