Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using BotSharp.Abstraction.Coding.Responses;

namespace BotSharp.Abstraction.Coding.Models;

public class CodeExecutionResponseModel
Expand All @@ -6,5 +8,5 @@ public class CodeExecutionResponseModel
public AgentCodeScript CodeScript { get; set; }
public IDictionary<string, string>? Arguments { get; set; }
public string Text { get; set; } = default!;
public string ExecutionResult { get; set; } = default!;
public CodeInterpretResponse? ExecutionResult { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private async Task<bool> TriggerCodeScript(Agent agent, string triggerName, Rule
CodeProcessor = processor.Provider,
CodeScript = codeScript,
Arguments = arguments.DistinctBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value ?? string.Empty),
ExecutionResult = response?.ToString() ?? string.Empty
ExecutionResult = response
};

foreach (var hook in hooks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task<InstructResult> Execute(

// Run code template
var codeResponse = await RunCode(agent, message, templateName, codeOptions);
if (codeResponse != null)
if (!string.IsNullOrWhiteSpace(codeResponse?.Text))
{
return codeResponse;
}
Expand Down Expand Up @@ -169,11 +169,11 @@ await hook.OnResponseGenerated(new InstructResponseModel
string templateName,
CodeInstructOptions? codeOptions)
{
InstructResult? response = null;
InstructResult? instructResult = null;

if (agent == null)
{
return response;
return instructResult;
}

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

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

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

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

if (codeResponse?.Success == true)
instructResult = new InstructResult
{
response = new InstructResult
{
MessageId = message.MessageId,
Template = context.CodeScript?.Name,
Text = codeResponse.Result
};
}
MessageId = message.MessageId,
Template = context.CodeScript?.Name,
Text = codeResponse?.Result ?? string.Empty
};

var codeExeResponse = new CodeExecutionResponseModel
var codeExecution = new CodeExecutionResponseModel
{
CodeProcessor = codeProcessor.Provider,
CodeScript = context.CodeScript,
ExecutionResult = codeResponse?.ToString() ?? string.Empty,
ExecutionResult = codeResponse,
Text = message.Content,
Arguments = context.Arguments?.DistinctBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value ?? string.Empty)
};

// After code execution
foreach (var hook in hooks)
{
await hook.AfterCompletion(agent, response ?? new());
await hook.AfterCodeExecution(agent, codeExeResponse);
await hook.AfterCompletion(agent, instructResult);
await hook.AfterCodeExecution(agent, codeExecution);
}

return response;
return instructResult;
}

private async Task<string> GetTextCompletion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override async Task AfterCodeExecution(Agent agent, CodeExecutionResponse
TemplateName = response.CodeScript?.Name,
UserMessage = response.Text,
SystemInstruction = $"Code script name: {response.CodeScript}, Version: {codeScriptVersion.ToString("o")}",
CompletionText = response.ExecutionResult,
CompletionText = response.ExecutionResult?.ToString() ?? string.Empty,
States = response.Arguments?.ToDictionary() ?? [],
UserId = user?.Id
}
Expand Down
Loading