Skip to content

Commit 281e5a2

Browse files
authored
Merge pull request #1219 from iceljc/master
change code execution response
2 parents ef3b4ff + f1bb73d commit 281e5a2

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Coding.Responses;
2+
13
namespace BotSharp.Abstraction.Coding.Models;
24

35
public class CodeExecutionResponseModel
@@ -6,5 +8,5 @@ public class CodeExecutionResponseModel
68
public AgentCodeScript CodeScript { get; set; }
79
public IDictionary<string, string>? Arguments { get; set; }
810
public string Text { get; set; } = default!;
9-
public string ExecutionResult { get; set; } = default!;
11+
public CodeInterpretResponse? ExecutionResult { get; set; }
1012
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private async Task<bool> TriggerCodeScript(Agent agent, string triggerName, Rule
153153
CodeProcessor = processor.Provider,
154154
CodeScript = codeScript,
155155
Arguments = arguments.DistinctBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value ?? string.Empty),
156-
ExecutionResult = response?.ToString() ?? string.Empty
156+
ExecutionResult = response
157157
};
158158

159159
foreach (var hook in hooks)

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public async Task<InstructResult> Execute(
4949

5050
// Run code template
5151
var codeResponse = await RunCode(agent, message, templateName, codeOptions);
52-
if (codeResponse != null)
52+
if (!string.IsNullOrWhiteSpace(codeResponse?.Text))
5353
{
5454
return codeResponse;
5555
}
@@ -169,11 +169,11 @@ await hook.OnResponseGenerated(new InstructResponseModel
169169
string templateName,
170170
CodeInstructOptions? codeOptions)
171171
{
172-
InstructResult? response = null;
172+
InstructResult? instructResult = null;
173173

174174
if (agent == null)
175175
{
176-
return response;
176+
return instructResult;
177177
}
178178

179179
var agentService = _services.GetRequiredService<IAgentService>();
@@ -192,7 +192,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
192192
#if DEBUG
193193
_logger.LogWarning($"No code processor found. (Agent: {agent.Id}, Code processor: {codeProvider})");
194194
#endif
195-
return response;
195+
return instructResult;
196196
}
197197

198198
// Get code script name
@@ -211,7 +211,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
211211
#if DEBUG
212212
_logger.LogWarning($"Empty code script name. (Agent: {agent.Id}, {scriptName})");
213213
#endif
214-
return response;
214+
return instructResult;
215215
}
216216

217217
// Get code script
@@ -222,7 +222,7 @@ await hook.OnResponseGenerated(new InstructResponseModel
222222
#if DEBUG
223223
_logger.LogWarning($"Empty code script. (Agent: {agent.Id}, {scriptName})");
224224
#endif
225-
return response;
225+
return instructResult;
226226
}
227227

228228
// Get code arguments
@@ -266,33 +266,30 @@ await hook.OnResponseGenerated(new InstructResponseModel
266266
UseProcess = useProcess
267267
}, cancellationToken: cts.Token);
268268

269-
if (codeResponse?.Success == true)
269+
instructResult = new InstructResult
270270
{
271-
response = new InstructResult
272-
{
273-
MessageId = message.MessageId,
274-
Template = context.CodeScript?.Name,
275-
Text = codeResponse.Result
276-
};
277-
}
271+
MessageId = message.MessageId,
272+
Template = context.CodeScript?.Name,
273+
Text = codeResponse?.Result ?? string.Empty
274+
};
278275

279-
var codeExeResponse = new CodeExecutionResponseModel
276+
var codeExecution = new CodeExecutionResponseModel
280277
{
281278
CodeProcessor = codeProcessor.Provider,
282279
CodeScript = context.CodeScript,
283-
ExecutionResult = codeResponse?.ToString() ?? string.Empty,
280+
ExecutionResult = codeResponse,
284281
Text = message.Content,
285282
Arguments = context.Arguments?.DistinctBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value ?? string.Empty)
286283
};
287284

288285
// After code execution
289286
foreach (var hook in hooks)
290287
{
291-
await hook.AfterCompletion(agent, response ?? new());
292-
await hook.AfterCodeExecution(agent, codeExeResponse);
288+
await hook.AfterCompletion(agent, instructResult);
289+
await hook.AfterCodeExecution(agent, codeExecution);
293290
}
294291

295-
return response;
292+
return instructResult;
296293
}
297294

298295
private async Task<string> GetTextCompletion(

src/Infrastructure/BotSharp.Logger/Hooks/InstructionLogHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public override async Task AfterCodeExecution(Agent agent, CodeExecutionResponse
8282
TemplateName = response.CodeScript?.Name,
8383
UserMessage = response.Text,
8484
SystemInstruction = $"Code script name: {response.CodeScript}, Version: {codeScriptVersion.ToString("o")}",
85-
CompletionText = response.ExecutionResult,
85+
CompletionText = response.ExecutionResult?.ToString() ?? string.Empty,
8686
States = response.Arguments?.ToDictionary() ?? [],
8787
UserId = user?.Id
8888
}

0 commit comments

Comments
 (0)