Skip to content

Commit 51239bc

Browse files
committed
refine tokens
1 parent 61cad7c commit 51239bc

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ public class CodeInterpretOptions
66
{
77
public string? ScriptName { get; set; }
88
public IEnumerable<KeyValue>? Arguments { get; set; }
9-
public bool UseMutex { get; set; }
9+
public bool UseLock { get; set; }
1010
public bool UseProcess { get; set; }
11-
public CancellationToken? CancellationToken { get; set; }
11+
public CancellationToken? OperationToken { get; set; }
12+
public CancellationToken? LockToken { get; set; }
1213
}

src/Infrastructure/BotSharp.Abstraction/Instructs/Options/CodeInstructOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public class CodeInstructOptions
2929
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3030
[JsonPropertyName("arguments")]
3131
public List<KeyValue>? Arguments { get; set; }
32+
33+
/// <summary>
34+
/// Timeout in seconds
35+
/// </summary>
36+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
37+
[JsonPropertyName("timeout_seconds")]
38+
public int? TimeoutSeconds { get; set; }
3239
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,13 @@ await hook.OnResponseGenerated(new InstructResponseModel
254254
}
255255

256256
// Run code script
257-
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
257+
var seconds = codeOptions?.TimeoutSeconds > 0 ? codeOptions.TimeoutSeconds.Value : 3;
258+
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(seconds));
258259
var codeResponse = await codeProcessor.RunAsync(context.CodeScript, options: new()
259260
{
260261
ScriptName = scriptName,
261262
Arguments = context.Arguments,
262-
CancellationToken = cts.Token
263+
OperationToken = cts.Token
263264
});
264265

265266
if (codeResponse == null || !codeResponse.Success)

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public PyCodeInterpreter(
3232

3333
public async Task<CodeInterpretResponse> RunAsync(string codeScript, CodeInterpretOptions? options = null)
3434
{
35-
if (options?.UseMutex == true)
35+
if (options?.UseLock == true)
3636
{
3737
return await _executor.ExecuteAsync(async () =>
3838
{
3939
return await InnerRunCode(codeScript, options);
40-
}, cancellationToken: options?.CancellationToken ?? CancellationToken.None);
40+
}, cancellationToken: options?.LockToken ?? CancellationToken.None);
4141
}
4242

4343
return await InnerRunCode(codeScript, options);
@@ -138,13 +138,11 @@ private async Task<CodeInterpretResponse> CoreRunScript(string codeScript, CodeI
138138
{
139139
_logger.LogWarning($"Begin {nameof(CoreRunScript)} in {Provider}: ${options?.ScriptName}");
140140

141-
var token = options?.CancellationToken ?? CancellationToken.None;
141+
var token = options?.OperationToken ?? CancellationToken.None;
142142
token.ThrowIfCancellationRequested();
143143

144144
using (Py.GIL())
145145
{
146-
token.ThrowIfCancellationRequested();
147-
148146
// Import necessary Python modules
149147
dynamic sys = Py.Import("sys");
150148
dynamic io = Py.Import("io");
@@ -214,7 +212,7 @@ private async Task<CodeInterpretResponse> CoreRunScript(string codeScript, CodeI
214212

215213
private async Task<CodeInterpretResponse> CoreRunProcess(string codeScript, CodeInterpretOptions? options = null)
216214
{
217-
var token = options?.CancellationToken ?? CancellationToken.None;
215+
var token = options?.OperationToken ?? CancellationToken.None;
218216

219217
var psi = new ProcessStartInfo
220218
{

0 commit comments

Comments
 (0)