Skip to content

Commit 61cad7c

Browse files
authored
Merge pull request #1209 from iceljc/bugfix/fix-code-exec-timeout
Bugfix/fix code exec timeout
2 parents d089401 + d911cfc commit 61cad7c

File tree

13 files changed

+244
-33
lines changed

13 files changed

+244
-33
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public AgentCodeScript() : base()
1111

1212
public override string ToString()
1313
{
14-
return $"{CodePath}";
14+
return base.ToString();
1515
}
1616
}
1717

@@ -26,4 +26,9 @@ public class AgentCodeScriptBase
2626
public string ScriptType { get; set; } = null!;
2727

2828
public string CodePath => $"{ScriptType}/{Name}";
29+
30+
public override string ToString()
31+
{
32+
return $"{CodePath}";
33+
}
2934
}

src/Infrastructure/BotSharp.Abstraction/Agents/Settings/AgentSettings.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ public class AgentSettings
1010
/// <summary>
1111
/// This is the default LLM config for agent
1212
/// </summary>
13-
public AgentLlmConfig LlmConfig { get; set; }
14-
= new AgentLlmConfig();
13+
public AgentLlmConfig LlmConfig { get; set; } = new();
1514
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public class CodeInterpretOptions
77
public string? ScriptName { get; set; }
88
public IEnumerable<KeyValue>? Arguments { get; set; }
99
public bool UseMutex { get; set; }
10+
public bool UseProcess { get; set; }
1011
public CancellationToken? CancellationToken { get; set; }
1112
}

src/Infrastructure/BotSharp.Abstraction/Coding/Responses/CodeInterpretResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ namespace BotSharp.Abstraction.Coding.Responses;
33
public class CodeInterpretResponse : ResponseBase
44
{
55
public string Result { get; set; } = string.Empty;
6+
7+
public override string ToString()
8+
{
9+
return Result ?? ErrorMsg ?? $"Success: {Success}";
10+
}
611
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace BotSharp.Abstraction.Coding.Settings;
2+
3+
public class CodingSettings
4+
{
5+
public CodeScriptGenerationSettings CodeGeneration { get; set; } = new();
6+
7+
public CodeScriptExecutionSettings CodeExecution { get; set; } = new();
8+
}
9+
10+
public class CodeScriptGenerationSettings
11+
{
12+
/// <summary>
13+
/// Llm provider to generate code script
14+
/// </summary>
15+
public string? Provider { get; set; }
16+
17+
/// <summary>
18+
/// Llm model to generate code script
19+
/// </summary>
20+
public string? Model { get; set; }
21+
}
22+
23+
public class CodeScriptExecutionSettings
24+
{
25+
public int MaxConcurrency { get; set; } = 1;
26+
}

src/Infrastructure/BotSharp.Abstraction/Using.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
global using BotSharp.Abstraction.Knowledges.Models;
2222
global using BotSharp.Abstraction.Crontab.Models;
2323
global using BotSharp.Abstraction.MCP.Models;
24-
global using BotSharp.Abstraction.Settings;
24+
global using BotSharp.Abstraction.Settings;
25+
global using BotSharp.Abstraction.Coding.Settings;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using BotSharp.Abstraction.Templating;
66
using BotSharp.Abstraction.Users.Enums;
77
using BotSharp.Core.Agents.Hooks;
8-
using BotSharp.Core.Coding;
98
using Microsoft.Extensions.Configuration;
109

1110
namespace BotSharp.Core.Agents;
@@ -49,8 +48,6 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
4948
render.RegisterType(typeof(AgentSettings));
5049
return settingService.Bind<AgentSettings>("Agent");
5150
});
52-
53-
services.AddSingleton<CodeScriptExecutor>();
5451
}
5552

5653
public bool AttachMenu(List<PluginMenuDef> menu)

src/Infrastructure/BotSharp.Core/Coding/CodeScriptExecutor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ namespace BotSharp.Core.Coding;
22

33
public class CodeScriptExecutor
44
{
5+
private readonly CodingSettings _settings;
56
private readonly ILogger<CodeScriptExecutor> _logger;
67
private readonly SemaphoreSlim _semLock = new(initialCount: 1, maxCount: 1);
78

89
public CodeScriptExecutor(
10+
CodingSettings settings,
911
ILogger<CodeScriptExecutor> logger)
1012
{
13+
_settings = settings;
1114
_logger = logger;
15+
16+
var maxConcurrency = settings.CodeExecution?.MaxConcurrency > 0 ? settings.CodeExecution.MaxConcurrency : 1;
17+
_semLock = new(initialCount: maxConcurrency, maxCount: maxConcurrency);
1218
}
1319

1420
public async Task<T> ExecuteAsync<T>(Func<Task<T>> func, CancellationToken cancellationToken = default)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.Extensions.Configuration;
2+
3+
namespace BotSharp.Core.Coding;
4+
5+
public class CodingPlugin : IBotSharpPlugin
6+
{
7+
public string Id => "31bc334b-9462-4191-beac-cb4a139b78c1";
8+
public string Name => "Coding";
9+
public string Description => "Handling execution and generation of code scripts";
10+
11+
public void RegisterDI(IServiceCollection services, IConfiguration config)
12+
{
13+
var coding = new CodingSettings();
14+
config.Bind("Coding", coding);
15+
services.AddSingleton(provider => coding);
16+
17+
services.AddSingleton<CodeScriptExecutor>();
18+
}
19+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Coding;
2+
using BotSharp.Abstraction.Coding.Responses;
23
using BotSharp.Abstraction.Files.Options;
34
using BotSharp.Abstraction.Files.Proccessors;
45
using BotSharp.Abstraction.Instructs;
@@ -253,17 +254,24 @@ await hook.OnResponseGenerated(new InstructResponseModel
253254
}
254255

255256
// Run code script
257+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
256258
var codeResponse = await codeProcessor.RunAsync(context.CodeScript, options: new()
257259
{
258260
ScriptName = scriptName,
259-
Arguments = context.Arguments
261+
Arguments = context.Arguments,
262+
CancellationToken = cts.Token
260263
});
261264

265+
if (codeResponse == null || !codeResponse.Success)
266+
{
267+
return response;
268+
}
269+
262270
response = new InstructResult
263271
{
264272
MessageId = message.MessageId,
265273
Template = scriptName,
266-
Text = codeResponse?.Result ?? codeResponse?.ErrorMsg
274+
Text = codeResponse.Result
267275
};
268276

269277
if (context?.Arguments != null)

0 commit comments

Comments
 (0)