Skip to content

Commit 88b18c0

Browse files
authored
Merge pull request #1177 from iceljc/features/refine-py-interpreter
refine running py code script
2 parents 4404e21 + c20ab2e commit 88b18c0

File tree

23 files changed

+262
-70
lines changed

23 files changed

+262
-70
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ public static class BuiltInAgentId
5151
/// Translates user-defined natural language rules into programmatic code
5252
/// </summary>
5353
public const string RulesInterpreter = "201e49a2-40b3-4ccd-b8cc-2476565a1b40";
54+
55+
/// <summary>
56+
/// Generate code script
57+
/// </summary>
58+
public const string AIProgrammer = "c2a2faf6-b8b5-47fe-807b-f4714cf25dd4";
59+
60+
/// <summary>
61+
/// Handle files
62+
/// </summary>
63+
public const string FileAssistant = "f1e09a73-9efe-46ce-ba02-b3aaf96d97e0";
5464
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
using System.Threading;
2+
13
namespace BotSharp.Abstraction.CodeInterpreter.Models;
24

35
public class CodeInterpretOptions
46
{
7+
public string? ScriptName { get; set; }
58
public IEnumerable<KeyValue>? Arguments { get; set; }
9+
public bool UseMutex { get; set; }
10+
public CancellationToken? CancellationToken { get; set; }
611
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14+
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-task_wait.json" />
1415
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-crontab-schedule_task.json" />
1516
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-crontab-schedule_task.fn.liquid" />
1617
</ItemGroup>

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,16 @@
9696
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.simulator.liquid" />
9797
<None Remove="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.simulator.liquid" />
9898
<None Remove="data\plugins\config.json" />
99-
100-
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-file-select_file_instruction.liquid" />
10199
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_weather.json" />
102100
<None Remove="data\agents\01e2fc5c-2c89-4ec7-8470-7688608b496c\functions\get_fun_events.json" />
103101

104102
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-web-intelligent_search.json" />
105103
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-web-intelligent_search.fn.liquid" />
104+
105+
<None Remove="data\agents\c2a2faf6-b8b5-47fe-807b-f4714cf25dd4\agent.json" />
106+
<None Remove="data\agents\c2a2faf6-b8b5-47fe-807b-f4714cf25dd4\instructions\instruction.liquid" />
107+
<None Remove="data\agents\f1e09a73-9efe-46ce-ba02-b3aaf96d97e0\agent.json" />
108+
<None Remove="data\agents\f1e09a73-9efe-46ce-ba02-b3aaf96d97e0\instructions\instruction.liquid" />
106109
</ItemGroup>
107110

108111
<ItemGroup>
@@ -172,6 +175,9 @@
172175
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-routing-fallback_to_router.fn.liquid">
173176
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
174177
</Content>
178+
<Content Include="data\agents\c2a2faf6-b8b5-47fe-807b-f4714cf25dd4\instructions\instruction.liquid">
179+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
180+
</Content>
175181
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\agent.json">
176182
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
177183
</Content>
@@ -193,9 +199,6 @@
193199
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\instructions\instruction.liquid">
194200
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
195201
</Content>
196-
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\util-file-select_file_instruction.liquid">
197-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
198-
</Content>
199202
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.simulator.liquid">
200203
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
201204
</Content>
@@ -208,6 +211,18 @@
208211
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\util-instruct-execute_template.json">
209212
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
210213
</Content>
214+
<Content Include="data\agents\c2a2faf6-b8b5-47fe-807b-f4714cf25dd4\agent.json">
215+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
216+
</Content>
217+
<Content Include="data\agents\c2a2faf6-b8b5-47fe-807b-f4714cf25dd4\instructions\instruction.liquid">
218+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
219+
</Content>
220+
<Content Include="data\agents\f1e09a73-9efe-46ce-ba02-b3aaf96d97e0\agent.json">
221+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
222+
</Content>
223+
<Content Include="data\agents\f1e09a73-9efe-46ce-ba02-b3aaf96d97e0\instructions\instruction.liquid">
224+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
225+
</Content>
211226
<Content Include="data\plugins\config.json">
212227
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
213228
</Content>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using BotSharp.Abstraction.CodeInterpreter.Models;
2+
3+
namespace BotSharp.Core.CodeInterpreter;
4+
5+
public class CodeScriptExecutor
6+
{
7+
private readonly ILogger<CodeScriptExecutor> _logger;
8+
private readonly SemaphoreSlim _semLock = new(initialCount: 1, maxCount: 1);
9+
10+
public CodeScriptExecutor(
11+
ILogger<CodeScriptExecutor> logger)
12+
{
13+
_logger = logger;
14+
}
15+
16+
public async Task<T> Execute<T>(Func<Task<T>> func, CancellationToken cancellationToken = default)
17+
{
18+
await _semLock.WaitAsync(cancellationToken);
19+
20+
try
21+
{
22+
return await func();
23+
}
24+
catch (Exception ex)
25+
{
26+
_logger.LogError(ex, $"Error in {nameof(CodeScriptExecutor)}.");
27+
return default(T);
28+
}
29+
finally
30+
{
31+
_semLock.Release();
32+
}
33+
}
34+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using BotSharp.Abstraction.Plugins.Models;
99
using BotSharp.Abstraction.Settings;
1010
using BotSharp.Abstraction.Templating;
11+
using BotSharp.Core.CodeInterpreter;
1112
using BotSharp.Core.Instructs;
1213
using BotSharp.Core.MessageHub;
1314
using BotSharp.Core.MessageHub.Observers;
@@ -70,6 +71,7 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
7071
services.AddScoped<ITokenStatistics, TokenStatistics>();
7172

7273
services.AddScoped<IAgentUtilityHook, WebSearchUtilityHook>();
74+
services.AddSingleton<CodeScriptExecutor>();
7375
}
7476

7577
public bool AttachMenu(List<PluginMenuDef> menu)

src/Infrastructure/BotSharp.Core/Files/Services/Instruct/FileInstructService.SelectFile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ private async Task<IEnumerable<MessageFileModel>> SelectFiles(IEnumerable<Messag
114114
return new NameDesc(text, desc);
115115
}).ToList();
116116

117-
var agentId = !string.IsNullOrWhiteSpace(options.AgentId) ? options.AgentId : BuiltInAgentId.UtilityAssistant;
118-
var template = !string.IsNullOrWhiteSpace(options.Template) ? options.Template : "util-file-select_file_instruction";
117+
var agentId = !string.IsNullOrWhiteSpace(options.AgentId) ? options.AgentId : BuiltInAgentId.FileAssistant;
118+
var template = !string.IsNullOrWhiteSpace(options.Template) ? options.Template : "select_chat_file_instruction";
119119
var prompt = db.GetAgentTemplate(agentId, template);
120120

121121
var data = new Dictionary<string, object>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
155155
var db = _services.GetRequiredService<IBotSharpRepository>();
156156
var hooks = _services.GetHooks<IInstructHook>(agent.Id);
157157

158-
var codeProvider = codeOptions?.CodeInterpretProvider.IfNullOrEmptyAs("botsharp-py-interpreter");
158+
var codeProvider = codeOptions?.CodeInterpretProvider ?? "botsharp-py-interpreter";
159159
var codeInterpreter = _services.GetServices<ICodeInterpretService>()
160160
.FirstOrDefault(x => x.Provider.IsEqualTo(codeProvider));
161161

@@ -228,6 +228,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
228228
// Run code script
229229
var result = await codeInterpreter.RunCode(context.CodeScript, options: new()
230230
{
231+
ScriptName = scriptName,
231232
Arguments = context.Arguments
232233
});
233234

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import argparse
2+
import json
23

34
def main():
45
parser = argparse.ArgumentParser(description="Receive named arguments")
56
parser.add_argument("--first_name", required=True, help="The first name")
67
parser.add_argument("--last_name", required=True, help="The last name")
78

8-
args = parser.parse_args()
9-
print(f"Hello, {args.first_name} {args.last_name}!")
9+
args, _ = parser.parse_known_args()
10+
obj = {
11+
"first_name": args.first_name,
12+
"last_name":args.last_name
13+
}
14+
print(f"{json.dumps(obj)}")
1015

1116
if __name__ == "__main__":
1217
main()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"id": "c2a2faf6-b8b5-47fe-807b-f4714cf25dd4",
3+
"name": "AI programmer",
4+
"description": "AI programmer is designed to generate code scripts.",
5+
"type": "task",
6+
"createdDateTime": "2025-10-07T10:39:32Z",
7+
"updatedDateTime": "2025-10-07T14:39:32Z",
8+
"iconUrl": "/images/logo.png",
9+
"disabled": false,
10+
"isPublic": true,
11+
"llmConfig": {
12+
"is_inherit": false,
13+
"provider": "openai",
14+
"model": "gpt-5",
15+
"max_recursion_depth": 3,
16+
"reasoning_effort_level": "minimal"
17+
}
18+
}

0 commit comments

Comments
 (0)