forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA2AAgentHook.cs
More file actions
88 lines (78 loc) · 3.2 KB
/
A2AAgentHook.cs
File metadata and controls
88 lines (78 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Agents.Settings;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Core.A2A.Services;
using BotSharp.Core.A2A.Settings;
using System.Text.Json;
namespace BotSharp.Core.A2A.Hooks;
public class A2AAgentHook : AgentHookBase
{
public override string SelfId => string.Empty;
private readonly A2ASettings _a2aSettings;
private readonly IA2AService _a2aService;
public A2AAgentHook(IServiceProvider services, IA2AService a2aService, A2ASettings a2aSettings, AgentSettings agentSettings)
: base(services, agentSettings)
{
_a2aService = a2aService;
_a2aSettings = a2aSettings;
}
public override bool OnAgentLoading(ref string id)
{
var agentId = id;
var remoteConfig = _a2aSettings.Agents?.FirstOrDefault(x => x.Id == agentId);
if (remoteConfig != null)
{
return true;
}
return base.OnAgentLoading(ref id);
}
public override void OnAgentLoaded(Agent agent)
{
// Check if this is an A2A remote agent
if (agent.Type != AgentType.A2ARemote)
{
return;
}
var remoteConfig = _a2aSettings.Agents?.FirstOrDefault(x => x.Id == agent.Id);
if (remoteConfig != null)
{
var agentCard = _a2aService.GetCapabilitiesAsync(remoteConfig.Endpoint).GetAwaiter().GetResult();
if (agentCard != null)
{
agent.Name = agentCard.Name;
agent.Description = agentCard.Description;
agent.Instruction = $"You are a proxy interface for an external intelligent service named '{agentCard.Name}'. " +
$"Your ONLY goal is to forward the user's request verbatim to the external service. " +
$"You must use the function 'delegate_to_a2a' to communicate with it. " +
$"Do not attempt to answer the question yourself.";
var properties = new Dictionary<string, object>
{
{
"user_query",
new
{
type = "string",
description = "The exact user request or task description to be forwarded."
}
}
};
var propertiesJson = JsonSerializer.Serialize(properties);
var propertiesDocument = JsonDocument.Parse(propertiesJson);
agent.Functions.Add(new FunctionDef
{
Name = "delegate_to_a2a",
Description = $"Delegates the task to the external {remoteConfig.Name} via A2A protocol.",
Parameters = new FunctionParametersDef()
{
Type = "object",
Properties = propertiesDocument,
Required = new List<string> { "user_query" }
}
});
}
}
base.OnAgentLoaded(agent);
}
}