microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix/msal-agentic-cache

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/samples/A2ABot/A2A/A2AClient.cs

56lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System.Text.Json;
5using A2A;
6
7namespace A2ABot.A2A;
8
9// Outbound A2A. Resolves the peer's AgentCard once (so callers can read
10// its live description) and ships HandoffMessage payloads as DataParts.
11// Knows nothing about Teams, LLMs, or proactive messaging.
12internal sealed class A2AClient(IHttpClientFactory factory, Config config)
13{
14 private readonly SemaphoreSlim _initLock = new(1, 1);
15 private volatile CachedPeer? _cached;
16
17 private sealed record CachedPeer(AgentCard Card, global::A2A.A2AClient Client);
18
19 public async Task<AgentCard> GetPeerCardAsync(CancellationToken ct)
20 {
21 if (_cached is not null) return _cached.Card;
22
23 await _initLock.WaitAsync(ct);
24 try
25 {
26 if (_cached is not null) return _cached.Card;
27
28 HttpClient http = factory.CreateClient("a2a");
29 A2ACardResolver resolver = new(new Uri(config.PeerUrl), http);
30 AgentCard card = await resolver.GetAgentCardAsync(ct);
31
32 global::A2A.A2AClient client = new(new Uri(card.SupportedInterfaces[0].Url), http);
33 _cached = new CachedPeer(card, client);
34 return card;
35 }
36 finally
37 {
38 _initLock.Release();
39 }
40 }
41
42 public async Task SendHandoffAsync(HandoffMessage payload, CancellationToken ct)
43 {
44 await GetPeerCardAsync(ct);
45
46 await _cached!.Client.SendMessageAsync(new SendMessageRequest
47 {
48 Message = new Message
49 {
50 MessageId = Guid.NewGuid().ToString("N"),
51 Role = Role.User,
52 Parts = [Part.FromData(JsonSerializer.SerializeToElement(payload))],
53 }
54 }, ct);
55 }
56}
57