Skip to content

Commit 0af83be

Browse files
author
Jicheng Lu
committed
minor change
1 parent ead4588 commit 0af83be

File tree

6 files changed

+39
-44
lines changed

6 files changed

+39
-44
lines changed

src/Infrastructure/BotSharp.Abstraction/Coding/Options/CodeGenerationOptions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ public class CodeGenerationOptions : LlmConfigBase
1717
public string? TemplateName { get; set; }
1818

1919
/// <summary>
20-
/// The programming language
20+
/// Programming language
2121
/// </summary>
22-
[JsonPropertyName("language")]
23-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
24-
public string? Language { get; set; } = "python";
22+
[JsonPropertyName("programming_language")]
23+
public string? ProgrammingLanguage { get; set; }
2524

2625
/// <summary>
2726
/// Data that can be used to fill in the prompt

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ namespace BotSharp.Abstraction.Rules;
22

33
public interface IRuleEngine
44
{
5+
/// <summary>
6+
/// Trigger the rule that is subscribed by agents.
7+
/// </summary>
8+
/// <param name="trigger"></param>
9+
/// <param name="text"></param>
10+
/// <param name="options"></param>
11+
/// <returns></returns>
12+
/// <exception cref="NotImplementedException"></exception>
513
Task<IEnumerable<string>> Trigger(IRuleTrigger trigger, string text, RuleTriggerOptions? options = null)
614
=> throw new NotImplementedException();
715
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public class RuleTriggerOptions
1515
public string? CodeScriptName { get; set; }
1616

1717
/// <summary>
18-
/// Json arguments
18+
/// Argument name as an input key to the code script
1919
/// </summary>
20-
public JsonDocument? Arguments { get; set; }
20+
public string? ArgsName { get; set; }
2121

2222
/// <summary>
23-
/// Agent where the code script is stored
23+
/// Json arguments as an input value to the code script
2424
/// </summary>
25-
public string? AgentId { get; set; }
25+
public JsonDocument? Arguments { get; set; }
2626

2727
/// <summary>
2828
/// States

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

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ public async Task<IEnumerable<string>> Trigger(IRuleTrigger trigger, string text
3838
}
3939
});
4040

41-
var isTriggered = true;
42-
43-
// Code trigger
44-
if (options != null)
45-
{
46-
isTriggered = await TriggerCodeScript(trigger.Name, options);
47-
}
48-
49-
if (!isTriggered)
50-
{
51-
return newConversationIds;
52-
}
53-
5441
// Trigger agents
5542
var filteredAgents = agents.Items.Where(x => x.Rules.Exists(r => r.TriggerName == trigger.Name && !x.Disabled)).ToList();
5643
foreach (var agent in filteredAgents)
5744
{
45+
var isTriggered = true;
46+
47+
// Code trigger
48+
if (options != null)
49+
{
50+
isTriggered = await TriggerCodeScript(agent.Id, trigger.Name, options);
51+
}
52+
53+
if (!isTriggered)
54+
{
55+
continue;
56+
}
57+
5858
var convService = _services.GetRequiredService<IConversationService>();
5959
var conv = await convService.NewConversation(new Conversation
6060
{
@@ -90,9 +90,8 @@ await convService.SendMessage(agent.Id,
9090
}
9191

9292
#region Private methods
93-
private async Task<bool> TriggerCodeScript(string triggerName, RuleTriggerOptions options)
93+
private async Task<bool> TriggerCodeScript(string agentId, string triggerName, RuleTriggerOptions options)
9494
{
95-
var agentId = options.AgentId;
9695
if (string.IsNullOrWhiteSpace(agentId))
9796
{
9897
return false;
@@ -123,7 +122,7 @@ private async Task<bool> TriggerCodeScript(string triggerName, RuleTriggerOption
123122
var response = await processor.RunAsync(codeScript, options: new()
124123
{
125124
ScriptName = scriptName,
126-
Arguments = BuildArguments(options.Arguments, options.States)
125+
Arguments = BuildArguments(options.ArgsName, options.Arguments)
127126
});
128127

129128
if (response == null || !response.Success)
@@ -155,26 +154,14 @@ private async Task<bool> TriggerCodeScript(string triggerName, RuleTriggerOption
155154
}
156155
}
157156

158-
private IEnumerable<KeyValue> BuildArguments(JsonDocument? args, IEnumerable<MessageState>? states)
157+
private IEnumerable<KeyValue> BuildArguments(string? argName, JsonDocument? args)
159158
{
160-
var dict = new Dictionary<string, string>();
161-
if (!states.IsNullOrEmpty())
162-
{
163-
foreach (var state in states)
164-
{
165-
if (state.Value != null)
166-
{
167-
dict[state.Key] = state.Value.ConvertToString();
168-
}
169-
}
170-
}
171-
159+
var keyValues = new List<KeyValue>();
172160
if (args != null)
173161
{
174-
dict["trigger_args"] = args.RootElement.GetRawText();
162+
keyValues.Add(new KeyValue(argName ?? "rule_args", args.RootElement.GetRawText()));
175163
}
176-
177-
return dict.Select(x => new KeyValue(x.Key, x.Value));
164+
return keyValues;
178165
}
179166
#endregion
180167
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/Agent/AgentController.Coding.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public async Task<CodeGenerationResult> GenerateAgentCodeScript([FromRoute] stri
6161
var states = request.Options?.Data?.ToList();
6262
var state = _services.GetRequiredService<IConversationStateService>();
6363
states?.ForEach(x => state.SetState(x.Key, x.Value, source: StateSource.External));
64-
state.SetState("programming_language", request.Options?.Language, source: StateSource.External);
64+
state.SetState("code_processor", request.Options?.Processor, source: StateSource.External);
65+
state.SetState("programming_language", request.Options?.ProgrammingLanguage, source: StateSource.External);
6566

6667
var result = await _agentService.GenerateCodeScript(agentId, request.Text, request?.Options);
6768
return result;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public async Task<CodeGenerationResult> GenerateCodeScriptAsync(string text, Cod
4141

4242
var agentId = options?.AgentId;
4343
var templateName = options?.TemplateName;
44-
45-
var agentService = _services.GetRequiredService<IAgentService>();
44+
4645
if (!string.IsNullOrEmpty(agentId))
4746
{
47+
var agentService = _services.GetRequiredService<IAgentService>();
4848
agent = await agentService.GetAgent(agentId);
4949
}
5050

@@ -83,7 +83,7 @@ public async Task<CodeGenerationResult> GenerateCodeScriptAsync(string text, Cod
8383
{
8484
Success = true,
8585
Content = response.Content,
86-
Language = options?.Language ?? "python"
86+
Language = options?.ProgrammingLanguage ?? "python"
8787
};
8888
}
8989

0 commit comments

Comments
 (0)