Skip to content

Commit 3ea642d

Browse files
committed
refine
1 parent 920fcc6 commit 3ea642d

File tree

8 files changed

+39
-22
lines changed

8 files changed

+39
-22
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace BotSharp.Abstraction.Coding.Constants;
2+
3+
public static class BuiltInCodeProcessor
4+
{
5+
public const string PyInterpreter = "botsharp-py-interpreter";
6+
}

src/Infrastructure/BotSharp.Abstraction/Rules/IRuleTrigger.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ public interface IRuleTrigger
1212

1313
string EntityId { get; set; }
1414

15+
/// <summary>
16+
/// The default arguments as input to code trigger (display purpose)
17+
/// </summary>
1518
JsonDocument OutputArgs => JsonDocument.Parse("{}");
1619
}

src/Infrastructure/BotSharp.Abstraction/Rules/Options/RuleTriggerOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class RuleTriggerOptions
77
/// <summary>
88
/// Code processor provider
99
/// </summary>
10-
public string? CodeProcessor { get; set; } = "botsharp-py-interpreter";
10+
public string? CodeProcessor { get; set; }
1111

1212
/// <summary>
1313
/// Code script name

src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Coding;
2+
using BotSharp.Abstraction.Coding.Constants;
23
using BotSharp.Abstraction.Conversations;
34
using BotSharp.Abstraction.Models;
45
using BotSharp.Abstraction.Repositories.Filters;
@@ -39,23 +40,19 @@ public async Task<IEnumerable<string>> Trigger(IRuleTrigger trigger, string text
3940

4041
var isTriggered = true;
4142

42-
// Apply code trigger
43-
if (options != null
44-
&& !string.IsNullOrWhiteSpace(options.CodeProcessor)
45-
&& !string.IsNullOrWhiteSpace(options.AgentId))
43+
// Code trigger
44+
if (options != null)
4645
{
47-
var scriptName = options.CodeScriptName ?? $"{trigger.Name}_cron.py";
48-
isTriggered = await TriggerCodeScript(options.AgentId, scriptName, options.CodeProcessor, trigger.Name, options.Arguments, options.States);
46+
isTriggered = await TriggerCodeScript(trigger.Name, options);
4947
}
5048

5149
if (!isTriggered)
5250
{
5351
return newConversationIds;
5452
}
5553

56-
var filteredAgents = agents.Items.Where(x => x.Rules.Exists(r => r.TriggerName == trigger.Name && !x.Disabled)).ToList();
57-
5854
// Trigger agents
55+
var filteredAgents = agents.Items.Where(x => x.Rules.Exists(r => r.TriggerName == trigger.Name && !x.Disabled)).ToList();
5956
foreach (var agent in filteredAgents)
6057
{
6158
var convService = _services.GetRequiredService<IConversationService>();
@@ -93,32 +90,40 @@ await convService.SendMessage(agent.Id,
9390
}
9491

9592
#region Private methods
96-
private async Task<bool> TriggerCodeScript(string agentId, string scriptName, string codeProcessor, string triggerName, JsonDocument? args = null, IEnumerable<MessageState>? states = null)
93+
private async Task<bool> TriggerCodeScript(string triggerName, RuleTriggerOptions options)
9794
{
98-
var processor = _services.GetServices<ICodeProcessor>().FirstOrDefault(x => x.Provider.IsEqualTo(codeProcessor));
95+
var agentId = options.AgentId;
96+
if (string.IsNullOrWhiteSpace(agentId))
97+
{
98+
return false;
99+
}
100+
101+
var provider = options.CodeProcessor ?? BuiltInCodeProcessor.PyInterpreter;
102+
var processor = _services.GetServices<ICodeProcessor>().FirstOrDefault(x => x.Provider.IsEqualTo(provider));
99103
if (processor == null)
100104
{
101-
_logger.LogWarning($"Unable to find code processor: {codeProcessor}.");
105+
_logger.LogWarning($"Unable to find code processor: {provider}.");
102106
return false;
103107
}
104108

105109
var agentService = _services.GetRequiredService<IAgentService>();
110+
var scriptName = options.CodeScriptName ?? $"{triggerName}_rule.py";
106111
var codeScript = await agentService.GetAgentCodeScript(agentId, scriptName, scriptType: AgentCodeScriptType.Src);
107112

113+
var msg = $"rule trigger ({triggerName}) code script ({scriptName}) in agent ({agentId}) => args: {options.Arguments?.RootElement.GetRawText()}.";
114+
108115
if (string.IsNullOrWhiteSpace(codeScript))
109116
{
110-
_logger.LogWarning($"Unable to find code script ({scriptName}) in agent ({agentId}).");
117+
_logger.LogWarning($"Unable to find {msg}.");
111118
return false;
112119
}
113120

114-
var msg = $"rule trigger ({triggerName}) code script ({scriptName}) in agent ({agentId}) => args: {args?.RootElement.GetRawText()}.";
115-
116121
try
117122
{
118123
var response = await processor.RunAsync(codeScript, options: new()
119124
{
120125
ScriptName = scriptName,
121-
Arguments = BuildArguments(args, states)
126+
Arguments = BuildArguments(options.Arguments, options.States)
122127
});
123128

124129
if (response == null || !response.Success)
@@ -140,7 +145,7 @@ private async Task<bool> TriggerCodeScript(string agentId, string scriptName, st
140145
result = false;
141146
}
142147

143-
_logger.Log(logLevel, $"Code result ({response.Result}) from {msg}");
148+
_logger.Log(logLevel, $"Code script execution result ({response.Result}) from {msg}");
144149
return result;
145150
}
146151
catch (Exception ex)
@@ -166,7 +171,7 @@ private IEnumerable<KeyValue> BuildArguments(JsonDocument? args, IEnumerable<Mes
166171

167172
if (args != null)
168173
{
169-
dict["trigger_input"] = args.RootElement.GetRawText();
174+
dict["trigger_args"] = args.RootElement.GetRawText();
170175
}
171176

172177
return dict.Select(x => new KeyValue(x.Key, x.Value));

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Coding.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Agents.Options;
22
using BotSharp.Abstraction.Coding;
3+
using BotSharp.Abstraction.Coding.Constants;
34
using BotSharp.Abstraction.Coding.Options;
45

56
namespace BotSharp.Core.Agents.Services;
@@ -76,7 +77,7 @@ public async Task<CodeGenerationResult> GenerateCodeScript(string agentId, strin
7677
};
7778
}
7879

79-
var processor = options?.Processor ?? "botsharp-py-interpreter";
80+
var processor = options?.Processor ?? BuiltInCodeProcessor.PyInterpreter;
8081
var codeProcessor = _services.GetServices<ICodeProcessor>().FirstOrDefault(x => x.Provider.IsEqualTo(processor));
8182
if (codeProcessor == null)
8283
{

src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Coding;
2+
using BotSharp.Abstraction.Coding.Constants;
23
using BotSharp.Abstraction.Files.Options;
34
using BotSharp.Abstraction.Files.Proccessors;
45
using BotSharp.Abstraction.Instructs;
@@ -179,7 +180,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
179180
var state = _services.GetRequiredService<IConversationStateService>();
180181
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
181182

182-
var codeProvider = codeOptions?.Processor ?? "botsharp-py-interpreter";
183+
var codeProvider = codeOptions?.Processor ?? BuiltInCodeProcessor.PyInterpreter;
183184
var codeProcessor = _services.GetServices<ICodeProcessor>()
184185
.FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider));
185186

src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Abstraction.Coding.Models;
21
using Microsoft.Extensions.Logging;
32
using Python.Runtime;
43
using System.Threading;
@@ -22,7 +21,7 @@ public PyCodeInterpreter(
2221
_executor = executor;
2322
}
2423

25-
public string Provider => "botsharp-py-interpreter";
24+
public string Provider => BuiltInCodeProcessor.PyInterpreter;
2625

2726
public async Task<CodeInterpretResponse> RunAsync(string codeScript, CodeInterpretOptions? options = null)
2827
{

src/Plugins/BotSharp.Plugin.PythonInterpreter/Using.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
global using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
2222
global using BotSharp.Abstraction.Routing;
2323
global using BotSharp.Abstraction.Coding;
24+
global using BotSharp.Abstraction.Coding.Constants;
25+
global using BotSharp.Abstraction.Coding.Models;
2426
global using BotSharp.Abstraction.Coding.Options;
2527
global using BotSharp.Abstraction.Coding.Responses;
2628
global using BotSharp.Core.Coding;

0 commit comments

Comments
 (0)