Skip to content

Commit ad27596

Browse files
authored
Merge pull request #761 from iceljc/features/merge-origin-agent
Features/merge origin agent
2 parents 2b1fc99 + 247afc4 commit ad27596

File tree

75 files changed

+623
-1058
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+623
-1058
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
using BotSharp.Abstraction.Agents.Settings;
2+
using BotSharp.Abstraction.Conversations;
23
using BotSharp.Abstraction.Functions.Models;
4+
using BotSharp.Abstraction.Repositories;
5+
using BotSharp.Abstraction.Routing;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using System.Data;
38

49
namespace BotSharp.Abstraction.Agents;
510

@@ -49,7 +54,71 @@ public virtual bool OnSamplesLoaded(List<string> samples)
4954
return true;
5055
}
5156

52-
public virtual void OnAgentLoaded(Agent agent)
57+
public virtual void OnAgentLoaded(Agent agent)
5358
{
5459
}
60+
61+
public virtual void OnAgentUtilityLoaded(Agent agent)
62+
{
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+
agent.Functions.AddRange(functions);
75+
foreach (var prompt in templates)
76+
{
77+
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
78+
}
79+
}
80+
81+
private (IEnumerable<FunctionDef>, IEnumerable<string>) GetUtilityContent(Agent agent)
82+
{
83+
var db = _services.GetRequiredService<IBotSharpRepository>();
84+
var (functionNames, templateNames) = GetUniqueContent(agent.Utilities);
85+
86+
if (agent.MergeUtility)
87+
{
88+
var routing = _services.GetRequiredService<IRoutingContext>();
89+
var entryAgentId = routing.EntryAgentId;
90+
if (!string.IsNullOrEmpty(entryAgentId))
91+
{
92+
var entryAgent = db.GetAgent(entryAgentId);
93+
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
94+
functionNames = functionNames.Concat(fns).Distinct().ToList();
95+
templateNames = templateNames.Concat(tps).Distinct().ToList();
96+
}
97+
}
98+
99+
var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
100+
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
101+
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.Select(x => x.Content)?.ToList() ?? [];
102+
return (functions, templates);
103+
}
104+
105+
private (IEnumerable<string>, IEnumerable<string>) GetUniqueContent(IEnumerable<AgentUtility>? utilities)
106+
{
107+
if (utilities.IsNullOrEmpty())
108+
{
109+
return ([], []);
110+
}
111+
112+
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
113+
var functionNames = utilities.SelectMany(x => x.Functions)
114+
.Where(x => !string.IsNullOrEmpty(x.Name))
115+
.Select(x => x.Name)
116+
.Distinct().ToList();
117+
var templateNames = utilities.SelectMany(x => x.Templates)
118+
.Where(x => !string.IsNullOrEmpty(x.Name))
119+
.Select(x => x.Name)
120+
.Distinct().ToList();
121+
122+
return (functionNames, templateNames);
123+
}
55124
}

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public interface IAgentHook
2525

2626
bool OnSamplesLoaded(List<string> samples);
2727

28+
void OnAgentUtilityLoaded(Agent agent);
29+
2830
/// <summary>
2931
/// Triggered when agent is loaded completely.
3032
/// </summary>

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ public interface IAgentService
5959

6060
PluginDef GetPlugin(string agentId);
6161

62-
IEnumerable<string> GetAgentUtilities();
62+
IEnumerable<AgentUtility> GetAgentUtilityOptions();
6363
}

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentUtilityHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ namespace BotSharp.Abstraction.Agents;
22

33
public interface IAgentUtilityHook
44
{
5-
void AddUtilities(List<string> utilities);
5+
void AddUtilities(List<AgentUtility> utilities);
66
}

src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@ public class Agent
8787
/// <summary>
8888
/// Profile by channel
8989
/// </summary>
90-
public List<string> Profiles { get; set; }
91-
= new List<string>();
90+
public List<string> Profiles { get; set; } = new();
91+
92+
/// <summary>
93+
/// Merge utilities from entry agent
94+
/// </summary>
95+
public bool MergeUtility { get; set; }
9296

9397
/// <summary>
9498
/// Agent utilities
9599
/// </summary>
96-
public List<string> Utilities { get; set; } = new();
100+
public List<AgentUtility> Utilities { get; set; } = new();
97101

98102
/// <summary>
99103
/// Inherit from agent
@@ -173,9 +177,9 @@ public Agent SetSamples(List<string> samples)
173177
return this;
174178
}
175179

176-
public Agent SetUtilities(List<string> utilities)
180+
public Agent SetUtilities(List<AgentUtility> utilities)
177181
{
178-
Utilities = utilities ?? new List<string>();
182+
Utilities = utilities ?? new List<AgentUtility>();
179183
return this;
180184
}
181185

@@ -215,6 +219,12 @@ public Agent SetDisabled(bool disabled)
215219
return this;
216220
}
217221

222+
public Agent SetMergeUtility(bool merge)
223+
{
224+
MergeUtility = merge;
225+
return this;
226+
}
227+
218228
public Agent SetAgentType(string type)
219229
{
220230
Type = type;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
namespace BotSharp.Abstraction.Agents.Models;
2+
3+
public class AgentUtility
4+
{
5+
public string Name { get; set; }
6+
public bool Disabled { get; set; }
7+
public IEnumerable<UtilityFunction> Functions { get; set; } = [];
8+
public IEnumerable<UtilityTemplate> Templates { get; set; } = [];
9+
10+
public AgentUtility()
11+
{
12+
13+
}
14+
15+
public AgentUtility(
16+
string name,
17+
IEnumerable<UtilityFunction>? functions = null,
18+
IEnumerable<UtilityTemplate>? templates = null)
19+
{
20+
Name = name;
21+
Functions = functions ?? [];
22+
Templates = templates ?? [];
23+
}
24+
}
25+
26+
27+
public class UtilityFunction : UtilityBase
28+
{
29+
public UtilityFunction()
30+
{
31+
32+
}
33+
34+
public UtilityFunction(string name)
35+
{
36+
Name = name;
37+
}
38+
}
39+
40+
public class UtilityTemplate : UtilityBase
41+
{
42+
public UtilityTemplate()
43+
{
44+
45+
}
46+
47+
public UtilityTemplate(string name)
48+
{
49+
Name = name;
50+
}
51+
}
52+
53+
public class UtilityBase
54+
{
55+
public string Name { get; set; }
56+
}

src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public interface IRoutingContext
66
string FirstGoalAgentId();
77
bool ContainsAgentId(string agentId);
88
string OriginAgentId { get; }
9+
string EntryAgentId { get; }
910
string ConversationId { get; }
1011
string MessageId { get; }
1112
void SetMessageId(string conversationId, string messageId);

src/Infrastructure/BotSharp.Abstraction/Templating/ITemplateRender.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ namespace BotSharp.Abstraction.Templating;
33
public interface ITemplateRender
44
{
55
string Render(string template, Dictionary<string, object> dict);
6+
void Register(Type type);
67
}

src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserAction.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace BotSharp.Abstraction.Users.Enums;
22

3+
/// <summary>
4+
/// User actions on agent level
5+
/// </summary>
36
public static class UserAction
47
{
58
public const string Edit = "edit";

src/Infrastructure/BotSharp.Abstraction/Users/Enums/UserPermission.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace BotSharp.Abstraction.Users.Enums;
22

3+
/// <summary>
4+
/// User permission
5+
/// </summary>
36
public static class UserPermission
47
{
58
public const string CreateAgent = "create-agent";

0 commit comments

Comments
 (0)