Skip to content

Commit 9d698fc

Browse files
committed
Support rendering submenu.
1 parent 6286393 commit 9d698fc

9 files changed

Lines changed: 70 additions & 27 deletions

File tree

‎src/Infrastructure/BotSharp.Abstraction/Plugins/IBotSharpPlugin.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ public interface IBotSharpPlugin
2525

2626
void RegisterDI(IServiceCollection services, IConfiguration config);
2727

28-
PluginMenuDef[] GetMenus() => new PluginMenuDef[0];
28+
bool AttachMenu(List<PluginMenuDef> menu) => true;
2929
}

‎src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginDef.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ public class PluginDef
66
public string Name { get; set; }
77
public string Description { get; set; }
88
public string Assembly { get; set; }
9+
[JsonPropertyName("is_core")]
10+
public bool IsCore => Assembly == "BotSharp.Core";
11+
912
[JsonPropertyName("icon_url")]
1013
public string? IconUrl { get; set; }
1114

@@ -17,6 +20,4 @@ public class PluginDef
1720
public IBotSharpPlugin Module { get; set; }
1821

1922
public bool Enabled { get; set; }
20-
21-
public PluginMenuDef[] Menus { get; set; }
2223
}

‎src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginMenuDef.cs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,36 @@ namespace BotSharp.Abstraction.Plugins.Models;
22

33
public class PluginMenuDef
44
{
5+
[JsonIgnore]
6+
public string Id { get; set; }
7+
58
public string Label { get; set; }
9+
10+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
611
public string? Icon { get; set; }
12+
13+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
714
public string? Link { get; set; }
8-
public bool IsHeader { get; set; }
15+
16+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
17+
public bool? IsHeader { get; set; }
18+
19+
[JsonIgnore]
920
public int Weight { get; set; }
1021

11-
public PluginMenuDef(string lable, string? link = null, string? icon = null, int weight = 0, bool isHeader = false)
22+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
23+
public List<PluginMenuDef>? SubMenu { get; set; }
24+
25+
public PluginMenuDef(string lable, string? link = null, string? icon = null, int weight = 0)
1226
{
1327
Label = lable;
1428
Link = link;
1529
Icon = icon;
1630
Weight = weight;
17-
IsHeader = isHeader;
31+
}
32+
33+
public override string ToString()
34+
{
35+
return $"{Label} {Link} {Weight}";
1836
}
1937
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.MLTasks;
2+
using BotSharp.Abstraction.Plugins.Models;
23
using BotSharp.Abstraction.Settings;
34
using Microsoft.Extensions.Configuration;
45

@@ -26,4 +27,20 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2627
return settingService.Bind<AgentSettings>("Agent");
2728
});
2829
}
30+
31+
public bool AttachMenu(List<PluginMenuDef> menu)
32+
{
33+
var section = menu.First(x => x.Label == "Apps");
34+
menu.Add(new PluginMenuDef("Agent", icon: "bx bx-bot", weight: section.Weight + 1)
35+
{
36+
SubMenu = new List<PluginMenuDef>
37+
{
38+
new PluginMenuDef("Router", link: "/page/agent/router"), // icon: "bx bx-map-pin"
39+
new PluginMenuDef("Evaluator", link: "/page/agent/evaluator"), // icon: "bx bx-task"
40+
new PluginMenuDef("Agents", link: "/page/agent"), // icon: "bx bx-bot"
41+
}
42+
});
43+
44+
return true;
45+
}
2946
}

‎src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BotSharp.Abstraction.Instructs;
22
using BotSharp.Abstraction.Messaging;
33
using BotSharp.Abstraction.Planning;
4+
using BotSharp.Abstraction.Plugins.Models;
45
using BotSharp.Abstraction.Settings;
56
using BotSharp.Abstraction.Templating;
67
using BotSharp.Core.Instructs;
@@ -45,4 +46,11 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
4546
services.AddScoped<IInstructService, InstructService>();
4647
services.AddScoped<ITokenStatistics, TokenStatistics>();
4748
}
49+
50+
public bool AttachMenu(List<PluginMenuDef> menu)
51+
{
52+
var section = menu.First(x => x.Label == "Apps");
53+
menu.Add(new PluginMenuDef("Conversation", link: "/page/conversation", icon: "bx bx-conversation", weight: section.Weight + 5));
54+
return true;
55+
}
4856
}

‎src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ public void Load(Action<Assembly> loaded)
5757
Description = module.Description,
5858
Assembly = assemblyName,
5959
IconUrl = module.IconUrl,
60-
AgentIds = module.AgentIds,
61-
Menus = module.GetMenus(),
60+
AgentIds = module.AgentIds
6261
});
6362
Console.Write($"Loaded plugin ");
6463
Console.Write(name, Color.Green);
@@ -85,7 +84,7 @@ public List<PluginDef> GetPlugins(IServiceProvider services)
8584
var config = db.GetPluginConfig();
8685
foreach (var plugin in _plugins)
8786
{
88-
plugin.Enabled = config.EnabledPlugins.Contains(plugin.Id);
87+
plugin.Enabled = plugin.IsCore || config.EnabledPlugins.Contains(plugin.Id);
8988
}
9089
return _plugins;
9190
}

‎src/Infrastructure/BotSharp.Core/data/plugins/config.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"058094a7-4ad3-4284-b94d-ac1373cf63d8",
55
"65185362-392c-44fd-a023-95a198824436",
66
"5b18ca9b-82ed-494b-b121-e3499661edb0",
7-
"6e52d42d-1e23-406b-8599-36af36c83209"
7+
"6e52d42d-1e23-406b-8599-36af36c83209"
88
]
99
}

‎src/Infrastructure/BotSharp.OpenAPI/Controllers/PluginController.cs‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,32 @@ public List<PluginDef> GetPlugins()
2626
[HttpGet("/plugin/menu")]
2727
public List<PluginMenuDef> GetPluginMenu()
2828
{
29-
var menus = new List<PluginMenuDef>
29+
var menu = new List<PluginMenuDef>
3030
{
3131
new PluginMenuDef("Dashboard", link: "/page/dashboard", icon: "bx bx-home-circle", weight: 1),
32-
new PluginMenuDef("Agent", isHeader: true, weight: 5),
33-
new PluginMenuDef("Router", link: "/page/agent/router", icon: "bx bx-map-pin", weight: 6),
34-
// new PluginMenuDef("Evaluator", link: "/page/agent/evaluator", icon: "bx bx-task", weight: 7),
35-
new PluginMenuDef("Agents", link: "/page/agent", icon: "bx bx-bot", weight: 8),
36-
new PluginMenuDef("Conversation", isHeader: true, weight: 15),
37-
new PluginMenuDef("Conversations", link: "/page/conversation", icon: "bx bx-conversation", weight: 16),
38-
new PluginMenuDef("System", isHeader: true, weight: 30),
32+
new PluginMenuDef("Apps", weight: 5)
33+
{
34+
IsHeader = true,
35+
},
36+
new PluginMenuDef("System", weight: 30)
37+
{
38+
IsHeader = true
39+
},
3940
new PluginMenuDef("Plugins", link: "/page/plugin", icon: "bx bx-plug", weight: 31),
4041
new PluginMenuDef("Settings", link: "/page/setting", icon: "bx bx-cog", weight: 32),
4142
};
43+
4244
var loader = _services.GetRequiredService<PluginLoader>();
4345
foreach (var plugin in loader.GetPlugins(_services))
4446
{
4547
if (!plugin.Enabled)
4648
{
4749
continue;
4850
}
49-
menus.AddRange(plugin.Menus);
51+
plugin.Module.AttachMenu(menu);
5052
}
51-
menus = menus.OrderBy(x => x.Weight).ToList();
52-
return menus;
53+
menu = menu.OrderBy(x => x.Weight).ToList();
54+
return menu;
5355
}
5456

5557
[HttpPost("/plugin/{id}/enable")]

‎src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2727
services.AddSingleton<IPdf2TextConverter, PigPdf2TextConverter>();
2828
}
2929

30-
public PluginMenuDef[] GetMenus()
30+
public bool AttachMenu(List<PluginMenuDef> menu)
3131
{
32-
return new PluginMenuDef[]
33-
{
34-
new PluginMenuDef("RAG", isHeader: true, weight: 20),
35-
new PluginMenuDef("Knowledge Base", link: "/page/knowledge-base", icon: "bx bx-book-open", weight: 21),
36-
};
32+
var section = menu.First(x => x.Label == "Apps");
33+
menu.Add(new PluginMenuDef("Knowledge Base", link: "/page/knowledge-base", icon: "bx bx-book-open", weight: section.Weight + 1));
34+
return true;
3735
}
3836
}

0 commit comments

Comments
 (0)