Skip to content

Commit d758e4b

Browse files
authored
Merge pull request #783 from iceljc/master
add common agent hook
2 parents c57ca81 + 9bbea3b commit d758e4b

File tree

3 files changed

+85
-73
lines changed

3 files changed

+85
-73
lines changed
Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
using BotSharp.Abstraction.Agents.Settings;
2-
using BotSharp.Abstraction.Conversations;
32
using BotSharp.Abstraction.Functions.Models;
4-
using BotSharp.Abstraction.Repositories;
5-
using BotSharp.Abstraction.Routing;
6-
using Microsoft.Extensions.DependencyInjection;
7-
using System.Data;
83

94
namespace BotSharp.Abstraction.Agents;
105

@@ -60,73 +55,5 @@ public virtual void OnAgentLoaded(Agent agent)
6055

6156
public virtual void OnAgentUtilityLoaded(Agent agent)
6257
{
63-
if (agent.Type == AgentType.Routing) return;
64-
65-
var conv = _services.GetRequiredService<IConversationService>();
66-
var isConvMode = conv.IsConversationMode();
67-
if (!isConvMode) return;
68-
69-
agent.Functions ??= [];
70-
agent.Utilities ??= [];
71-
72-
var (functions, templates) = GetUtilityContent(agent);
73-
74-
foreach (var fn in functions)
75-
{
76-
if (!agent.Functions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
77-
{
78-
agent.Functions.Add(fn);
79-
}
80-
}
81-
82-
foreach (var prompt in templates)
83-
{
84-
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
85-
}
86-
}
87-
88-
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
89-
{
90-
var db = _services.GetRequiredService<IBotSharpRepository>();
91-
var (functionNames, templateNames) = GetUniqueContent(agent.Utilities);
92-
93-
if (agent.MergeUtility)
94-
{
95-
var routing = _services.GetRequiredService<IRoutingContext>();
96-
var entryAgentId = routing.EntryAgentId;
97-
if (!string.IsNullOrEmpty(entryAgentId))
98-
{
99-
var entryAgent = db.GetAgent(entryAgentId);
100-
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
101-
functionNames = functionNames.Concat(fns).Distinct().ToList();
102-
templateNames = templateNames.Concat(tps).Distinct().ToList();
103-
}
104-
}
105-
106-
var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
107-
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
108-
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.Select(x => x.Content)?.ToList() ?? [];
109-
return (functions, templates);
110-
}
111-
112-
private (IEnumerable<string>, IEnumerable<string>) GetUniqueContent(IEnumerable<AgentUtility>? utilities)
113-
{
114-
if (utilities.IsNullOrEmpty())
115-
{
116-
return ([], []);
117-
}
118-
119-
var prefix = "util-";
120-
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
121-
var functionNames = utilities.SelectMany(x => x.Functions)
122-
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
123-
.Select(x => x.Name)
124-
.Distinct().ToList();
125-
var templateNames = utilities.SelectMany(x => x.Templates)
126-
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
127-
.Select(x => x.Name)
128-
.Distinct().ToList();
129-
130-
return (functionNames, templateNames);
13158
}
13259
}

src/Infrastructure/BotSharp.Core/Agents/AgentPlugin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using BotSharp.Abstraction.Settings;
44
using BotSharp.Abstraction.Templating;
55
using BotSharp.Abstraction.Users.Enums;
6+
using BotSharp.Core.Agents.Hooks;
67
using Microsoft.Extensions.Configuration;
78

89
namespace BotSharp.Core.Agents;
@@ -30,6 +31,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
3031
{
3132
services.AddScoped<ILlmProviderService, LlmProviderService>();
3233
services.AddScoped<IAgentService, AgentService>();
34+
services.AddScoped<IAgentHook, CommonAgentHook>();
3335

3436
services.AddScoped(provider =>
3537
{
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
namespace BotSharp.Core.Agents.Hooks;
2+
3+
public class CommonAgentHook : AgentHookBase
4+
{
5+
public override string SelfId => string.Empty;
6+
7+
public CommonAgentHook(IServiceProvider services, AgentSettings settings)
8+
: base(services, settings)
9+
{
10+
}
11+
12+
public override void OnAgentUtilityLoaded(Agent agent)
13+
{
14+
if (agent.Type == AgentType.Routing) return;
15+
16+
var conv = _services.GetRequiredService<IConversationService>();
17+
var isConvMode = conv.IsConversationMode();
18+
if (!isConvMode) return;
19+
20+
agent.Functions ??= [];
21+
agent.Utilities ??= [];
22+
23+
var (functions, templates) = GetUtilityContent(agent);
24+
25+
foreach (var fn in functions)
26+
{
27+
if (!agent.Functions.Any(x => x.Name.Equals(fn.Name, StringComparison.OrdinalIgnoreCase)))
28+
{
29+
agent.Functions.Add(fn);
30+
}
31+
}
32+
33+
foreach (var prompt in templates)
34+
{
35+
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
36+
}
37+
}
38+
39+
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
40+
{
41+
var db = _services.GetRequiredService<IBotSharpRepository>();
42+
var (functionNames, templateNames) = GetUniqueContent(agent.Utilities);
43+
44+
if (agent.MergeUtility)
45+
{
46+
var routing = _services.GetRequiredService<IRoutingContext>();
47+
var entryAgentId = routing.EntryAgentId;
48+
if (!string.IsNullOrEmpty(entryAgentId))
49+
{
50+
var entryAgent = db.GetAgent(entryAgentId);
51+
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
52+
functionNames = functionNames.Concat(fns).Distinct().ToList();
53+
templateNames = templateNames.Concat(tps).Distinct().ToList();
54+
}
55+
}
56+
57+
var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
58+
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
59+
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.Select(x => x.Content)?.ToList() ?? [];
60+
return (functions, templates);
61+
}
62+
63+
private (IEnumerable<string>, IEnumerable<string>) GetUniqueContent(IEnumerable<AgentUtility>? utilities)
64+
{
65+
if (utilities.IsNullOrEmpty())
66+
{
67+
return ([], []);
68+
}
69+
70+
var prefix = "util-";
71+
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
72+
var functionNames = utilities.SelectMany(x => x.Functions)
73+
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
74+
.Select(x => x.Name)
75+
.Distinct().ToList();
76+
var templateNames = utilities.SelectMany(x => x.Templates)
77+
.Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(prefix))
78+
.Select(x => x.Name)
79+
.Distinct().ToList();
80+
81+
return (functionNames, templateNames);
82+
}
83+
}

0 commit comments

Comments
 (0)