1+ using A2A ;
12using BotSharp . Abstraction . Agents ;
23using BotSharp . Abstraction . Agents . Enums ;
34using BotSharp . Abstraction . Agents . Models ;
45using BotSharp . Abstraction . Agents . Settings ;
56using BotSharp . Abstraction . Functions . Models ;
67using BotSharp . Core . A2A . Services ;
78using BotSharp . Core . A2A . Settings ;
9+ using Microsoft . Extensions . Logging ;
810using System . Text . Json ;
911
1012namespace BotSharp . Core . A2A . Hooks ;
@@ -15,12 +17,14 @@ public class A2AAgentHook : AgentHookBase
1517
1618 private readonly A2ASettings _a2aSettings ;
1719 private readonly IA2AService _a2aService ;
20+ private readonly ILogger < A2AAgentHook > _logger ;
1821
19- public A2AAgentHook ( IServiceProvider services , IA2AService a2aService , A2ASettings a2aSettings , AgentSettings agentSettings )
22+ public A2AAgentHook ( IServiceProvider services , IA2AService a2aService , A2ASettings a2aSettings , AgentSettings agentSettings , ILogger < A2AAgentHook > logger )
2023 : base ( services , agentSettings )
2124 {
2225 _a2aService = a2aService ;
2326 _a2aSettings = a2aSettings ;
27+ _logger = logger ;
2428 }
2529
2630 public override async Task < string ? > OnAgentLoading ( string id )
@@ -45,7 +49,16 @@ public override async Task OnAgentLoaded(Agent agent)
4549 var remoteConfig = _a2aSettings . Agents ? . FirstOrDefault ( x => x . Id == agent . Id ) ;
4650 if ( remoteConfig != null )
4751 {
48- var agentCard = await _a2aService . GetCapabilitiesAsync ( remoteConfig . Endpoint ) ;
52+ AgentCard ? agentCard = null ;
53+ try
54+ {
55+ agentCard = await _a2aService . GetCapabilitiesAsync ( remoteConfig . Endpoint ) ;
56+ }
57+ catch ( Exception ex )
58+ {
59+ _logger . LogWarning ( ex , "Failed to resolve A2A agent card for endpoint {AgentEndpoint}. Using configured metadata." , remoteConfig . Endpoint ) ;
60+ }
61+
4962 if ( agentCard != null )
5063 {
5164 agent . Name = agentCard . Name ;
@@ -54,34 +67,34 @@ public override async Task OnAgentLoaded(Agent agent)
5467 $ "Your ONLY goal is to forward the user's request verbatim to the external service. " +
5568 $ "You must use the function 'delegate_to_a2a' to communicate with it. " +
5669 $ "Do not attempt to answer the question yourself.";
70+ }
5771
58- var properties = new Dictionary < string , object >
72+ var properties = new Dictionary < string , object >
73+ {
5974 {
75+ "user_query" ,
76+ new
6077 {
61- "user_query" ,
62- new
63- {
64- type = "string" ,
65- description = "The exact user request or task description to be forwarded."
66- }
78+ type = "string" ,
79+ description = "The exact user request or task description to be forwarded."
6780 }
68- } ;
81+ }
82+ } ;
6983
70- var propertiesJson = JsonSerializer . Serialize ( properties ) ;
71- var propertiesDocument = JsonDocument . Parse ( propertiesJson ) ;
84+ var propertiesJson = JsonSerializer . Serialize ( properties ) ;
85+ var propertiesDocument = JsonDocument . Parse ( propertiesJson ) ;
7286
73- agent . Functions . Add ( new FunctionDef
87+ agent . Functions . Add ( new FunctionDef
88+ {
89+ Name = "delegate_to_a2a" ,
90+ Description = $ "Delegates the task to the external { remoteConfig . Name } via A2A protocol.",
91+ Parameters = new FunctionParametersDef ( )
7492 {
75- Name = "delegate_to_a2a" ,
76- Description = $ "Delegates the task to the external { remoteConfig . Name } via A2A protocol.",
77- Parameters = new FunctionParametersDef ( )
78- {
79- Type = "object" ,
80- Properties = propertiesDocument ,
81- Required = new List < string > { "user_query" }
82- }
83- } ) ;
84- }
93+ Type = "object" ,
94+ Properties = propertiesDocument ,
95+ Required = new List < string > { "user_query" }
96+ }
97+ } ) ;
8598 }
8699 await base . OnAgentLoaded ( agent ) ;
87100 }
0 commit comments