Skip to content

Commit f4a1765

Browse files
author
jason
committed
Merge remote-tracking branch 'origin/master' into jason_dev
2 parents ed27520 + c387ea0 commit f4a1765

File tree

115 files changed

+932
-305
lines changed

Some content is hidden

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

115 files changed

+932
-305
lines changed

docs/architecture/agent-utility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ public class HttpHandlerPlugin : IBotSharpPlugin
419419
}
420420
```
421421

422-
[Fig 4.1.2](#register-assembly) demonstrates the utility assembly registration in “appsettings.json”. It is important to note that we are required to add the project reference to the Startup project, e.g., WebStarter. Moreover, we are required to add any new custom agent utility in the “Plugin” folder instead of the “BotSharp” folder.
422+
[Fig 4.1.1](#register-assembly) demonstrates the utility assembly registration in “appsettings.json”. It is important to note that we are required to add the project reference to the Startup project, e.g., WebStarter. Moreover, we are required to add any new custom agent utility in the “Plugin” folder instead of the “BotSharp” folder.
423423

424424
<div style="text-align: center;" id="register-assembly">
425425
<img src="assets/agent-utility/register-assembly.png" />
Lines changed: 0 additions & 72 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,72 +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-
utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? [];
120-
var functionNames = utilities.SelectMany(x => x.Functions)
121-
.Where(x => !string.IsNullOrEmpty(x.Name))
122-
.Select(x => x.Name)
123-
.Distinct().ToList();
124-
var templateNames = utilities.SelectMany(x => x.Templates)
125-
.Where(x => !string.IsNullOrEmpty(x.Name))
126-
.Select(x => x.Name)
127-
.Distinct().ToList();
128-
129-
return (functionNames, templateNames);
13058
}
13159
}

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,4 @@ public class BuiltInAgentId
5656
/// Translates user-defined natural language rules into programmatic code
5757
/// </summary>
5858
public const string RuleEncoder = "6acfb93c-3412-402e-9ba5-c5d3cd8f0161";
59-
60-
/// <summary>
61-
/// Schedule job
62-
/// </summary>
63-
public const string Crontab = "c2o139da-a62a-4355-8605-fdf0ffaca58e";
6459
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public AgentUtility(
2121
Functions = functions ?? [];
2222
Templates = templates ?? [];
2323
}
24+
25+
public override string ToString()
26+
{
27+
return Name;
28+
}
2429
}
2530

2631

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
using BotSharp.Abstraction.Functions.Models;
33
using BotSharp.Abstraction.Messaging;
44
using BotSharp.Abstraction.Messaging.Models.RichContent;
5+
using System.Diagnostics;
56

67
namespace BotSharp.Abstraction.Conversations.Models;
78

9+
[DebuggerStepThrough]
810
public class RoleDialogModel : ITrackableMessage
911
{
1012
/// <summary>

src/Infrastructure/BotSharp.Abstraction/Users/Models/UserActivationModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ public class UserActivationModel
44
{
55
public string UserName { get; set; }
66
public string VerificationCode { get; set; }
7+
public string RegionCode { get; set; } = "CN";
78
}

src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<None Remove="data\agents\c2o139da-a62a-4355-8605-fdf0ffaca58e\agent.json" />
10+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json" />
11+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-crontab-schedule_task.fn.liquid" />
1112
</ItemGroup>
1213

1314
<ItemGroup>
14-
<Content Include="data\agents\c2o139da-a62a-4355-8605-fdf0ffaca58e\agent.json">
15+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
</Content>
18+
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-crontab-schedule_task.fn.liquid">
1519
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1620
</Content>
1721
</ItemGroup>

src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using BotSharp.Core.Crontab.Hooks;
18+
1719
namespace BotSharp.Core.Crontab;
1820

1921
/// <summary>
@@ -27,13 +29,9 @@ public class CrontabPlugin : IBotSharpPlugin
2729
public string Description => "Crontab plugin is a time-based job scheduler in agent framework. The cron system is used to trigger AI Agent to do specific task periodically.";
2830
public string IconUrl => "https://icon-library.com/images/stop-watch-icon/stop-watch-icon-10.jpg";
2931

30-
public string[] AgentIds =
31-
[
32-
BuiltInAgentId.Crontab
33-
];
34-
3532
public void RegisterDI(IServiceCollection services, IConfiguration config)
3633
{
34+
services.AddScoped<IAgentUtilityHook, CrontabUtilityHook>();
3735
services.AddScoped<ICrontabService, CrontabService>();
3836
services.AddHostedService<CrontabWatcher>();
3937
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Core.Crontab.Enum;
2+
3+
public class UtilityName
4+
{
5+
public const string ScheduleTask = "crontab.schedule-task";
6+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using BotSharp.Abstraction.Routing;
2+
using BotSharp.Abstraction.Users;
3+
using BotSharp.Core.Crontab.Hooks;
4+
5+
namespace BotSharp.Core.Crontab.Functions;
6+
7+
public class ScheduleTaskFn : IFunctionCallback
8+
{
9+
public string Name => $"{CrontabUtilityHook.PREFIX}schedule_task";
10+
private readonly IServiceProvider _services;
11+
12+
public ScheduleTaskFn(IServiceProvider services)
13+
{
14+
_services = services;
15+
}
16+
17+
public async Task<bool> Execute(RoleDialogModel message)
18+
{
19+
var args = JsonSerializer.Deserialize<ScheduleTaskArgs>(message.FunctionArgs);
20+
21+
var routing = _services.GetRequiredService<IRoutingContext>();
22+
var user = _services.GetRequiredService<IUserIdentity>();
23+
var crontabItem = new CrontabItem
24+
{
25+
Topic = args.Topic,
26+
Description = args.Description,
27+
Cron = args.Cron,
28+
Script = args.Script,
29+
Language = args.Language,
30+
UserId = user.Id,
31+
AgentId = routing.EntryAgentId,
32+
ConversationId = routing.ConversationId
33+
};
34+
35+
return true;
36+
}
37+
}

0 commit comments

Comments
 (0)