-
-
Notifications
You must be signed in to change notification settings - Fork 582
Refactor InvokeFunction code #1044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace BotSharp.Core.Routing | ||
{ | ||
public interface IFunctionExecutor | ||
{ | ||
public Task<bool> Execute(RoleDialogModel message); | ||
|
||
public Task<string> GetIndication(RoleDialogModel message); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using BotSharp.Core.MCP.Hooks; | ||
using BotSharp.Core.MCP.Managers; | ||
using BotSharp.Core.MCP.Services; | ||
using BotSharp.Core.MCP.Settings; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace BotSharp.Core.MCP; | ||
|
||
public static class BotSharpMcpExtensions | ||
{ | ||
/// <summary> | ||
/// Add mcp | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <param name="config"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddBotSharpMCP(this IServiceCollection services, IConfiguration config) | ||
{ | ||
var settings = config.GetSection("MCP").Get<McpSettings>(); | ||
services.AddScoped(provider => settings); | ||
|
||
if (settings != null && settings.Enabled && !settings.McpServerConfigs.IsNullOrEmpty()) | ||
{ | ||
services.AddScoped<IMcpService, McpService>(); | ||
|
||
var clientManager = new McpClientManager(settings); | ||
services.AddScoped(provider => clientManager); | ||
|
||
// Register hooks | ||
services.AddScoped<IAgentHook, McpToolAgentHook>(); | ||
} | ||
return services; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using BotSharp.Abstraction.Templating; | ||
|
||
namespace BotSharp.Core.Routing.Executor; | ||
|
||
public class DummyFunctionExecutor: IFunctionExecutor | ||
{ | ||
private FunctionDef functionDef; | ||
private readonly IServiceProvider _services; | ||
|
||
public DummyFunctionExecutor(FunctionDef function, IServiceProvider services) | ||
{ | ||
functionDef = function; | ||
_services = services; | ||
} | ||
|
||
|
||
public async Task<bool> ExecuteAsync(RoleDialogModel message) | ||
{ | ||
var render = _services.GetRequiredService<ITemplateRender>(); | ||
var state = _services.GetRequiredService<IConversationStateService>(); | ||
|
||
var dict = new Dictionary<string, object>(); | ||
foreach (var item in state.GetStates()) | ||
{ | ||
dict[item.Key] = item.Value; | ||
} | ||
|
||
var text = render.Render(functionDef.Output, dict); | ||
message.Content = text; | ||
return true; | ||
} | ||
|
||
public async Task<string> GetIndicatorAsync(RoleDialogModel message) | ||
{ | ||
return "Running"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using BotSharp.Abstraction.Functions; | ||
|
||
namespace BotSharp.Core.Routing.Executor; | ||
|
||
public class FunctionCallbackExecutor : IFunctionExecutor | ||
{ | ||
IFunctionCallback functionCallback; | ||
|
||
public FunctionCallbackExecutor(IFunctionCallback functionCallback) | ||
{ | ||
this.functionCallback = functionCallback; | ||
} | ||
|
||
public async Task<bool> ExecuteAsync(RoleDialogModel message) | ||
{ | ||
return await functionCallback.Execute(message); | ||
} | ||
|
||
public async Task<string> GetIndicatorAsync(RoleDialogModel message) | ||
{ | ||
return await functionCallback.GetIndication(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using BotSharp.Abstraction.Functions; | ||
|
||
namespace BotSharp.Core.Routing.Executor; | ||
|
||
internal class FunctionExecutorFactory | ||
{ | ||
public static IFunctionExecutor Create(string functionName, Agent agent, IFunctionCallback functioncall, IServiceProvider serviceProvider) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is not necessary to pass "functionCallback" from outside. Just function name will be enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. functionCallback is botsharp functionexecutor:FunctionCallbackExecutor,Determine whether it is a FunctionCalling defined within the BotSharp framework by checking if FunctionCallback is null |
||
{ | ||
if(functioncall != null) | ||
{ | ||
return new FunctionCallbackExecutor(functioncall); | ||
} | ||
|
||
var funDef = agent?.Functions?.FirstOrDefault(x => x.Name == functionName); | ||
if (funDef != null) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(funDef?.Output)) | ||
{ | ||
return new DummyFunctionExecutor(funDef,serviceProvider); | ||
} | ||
} | ||
else | ||
{ | ||
funDef = agent?.SecondaryFunctions?.FirstOrDefault(x => x.Name == functionName); | ||
if (funDef != null) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(funDef?.Output)) | ||
{ | ||
return new DummyFunctionExecutor(funDef, serviceProvider); | ||
} | ||
else | ||
{ | ||
var mcpServerId = agent?.McpTools?.Where(x => x.Functions.Any(y => y.Name == funDef.Name)) | ||
.FirstOrDefault().ServerId; | ||
return new MCPToolExecutor(mcpServerId, functionName, serviceProvider); | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace BotSharp.Core.Routing.Executor; | ||
|
||
public interface IFunctionExecutor | ||
{ | ||
public Task<bool> ExecuteAsync(RoleDialogModel message); | ||
|
||
public Task<string> GetIndicatorAsync(RoleDialogModel message); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.