Refactor InvokeFunction code
Refactor the InvokeFunction code by categorizing the dummy function, function callback, and MCP tool invocation logic into three distinct classes
This commit is contained in:
parent
a3aabe29aa
commit
927184efee
11
BotSharp.sln
11
BotSharp.sln
|
|
@ -131,8 +131,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.Realtime", "s
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.PizzaBot.MCPServer", "tests\BotSharp.PizzaBot.MCPServer\BotSharp.PizzaBot.MCPServer.csproj", "{8D2AD45F-836A-516F-DE6A-71443CEBB18A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Core.MCP", "src\Infrastructure\BotSharp.Core.MCP\BotSharp.Core.MCP.csproj", "{534D1DD5-9D62-115C-F230-47080D76CE52}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Test.ComputerUse", "tests\BotSharp.Test.ComputerUse\BotSharp.Test.ComputerUse.csproj", "{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.OsDriver", "src\Plugins\BotSharp.Plugin.OsDriver\BotSharp.Plugin.OsDriver.csproj", "{B268E2F0-060F-8466-7D81-ABA4D735CA59}"
|
||||
|
|
@ -553,14 +551,6 @@ Global
|
|||
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{8D2AD45F-836A-516F-DE6A-71443CEBB18A}.Release|x64.Build.0 = Release|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52}.Release|x64.Build.0 = Release|Any CPU
|
||||
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
|
|
@ -672,7 +662,6 @@ Global
|
|||
{AF329442-B48E-4B48-A18A-1C869D1BA6F5} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
{781F1465-365C-0F22-1775-25025DAFA4C7} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||
{8D2AD45F-836A-516F-DE6A-71443CEBB18A} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
|
||||
{534D1DD5-9D62-115C-F230-47080D76CE52} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||
{C19D9AC1-97DD-8E65-E8DB-D295A095AA2D} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
|
||||
{B268E2F0-060F-8466-7D81-ABA4D735CA59} = {51AFE054-AE99-497D-A593-69BAEFB5106F}
|
||||
{970BE341-9AC8-99A5-6572-E703C1E02FCB} = {E29DC6C4-5E57-48C5-BCB0-6B8F84782749}
|
||||
|
|
|
|||
15
IFunctionExecutor.cs
Normal file
15
IFunctionExecutor.cs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>$(TargetFramework)</TargetFramework>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<OutputPath>$(SolutionDir)packages</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="ModelContextProtocol" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
using BotSharp.Core.MCP.Functions;
|
||||
using BotSharp.Core.MCP.Hooks;
|
||||
using BotSharp.Core.MCP.Managers;
|
||||
using BotSharp.Core.MCP.Services;
|
||||
using BotSharp.Core.MCP.Settings;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using ModelContextProtocol.Client;
|
||||
|
||||
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)
|
||||
{
|
||||
services.AddScoped<IMcpService, McpService>();
|
||||
var settings = config.GetSection("MCP").Get<McpSettings>();
|
||||
services.AddScoped(provider => settings);
|
||||
|
||||
if (settings != null && settings.Enabled && !settings.McpServerConfigs.IsNullOrEmpty())
|
||||
{
|
||||
var clientManager = new McpClientManager(settings);
|
||||
services.AddSingleton(clientManager);
|
||||
|
||||
foreach (var server in settings.McpServerConfigs)
|
||||
{
|
||||
RegisterFunctionCall(services, server, clientManager)
|
||||
.ConfigureAwait(false)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
// Register hooks
|
||||
services.AddScoped<IAgentHook, McpToolAgentHook>();
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
private static async Task RegisterFunctionCall(IServiceCollection services, McpServerConfigModel server, McpClientManager clientManager)
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = await clientManager.GetMcpClientAsync(server.Id);
|
||||
var tools = await client.ListToolsAsync();
|
||||
|
||||
foreach (var tool in tools)
|
||||
{
|
||||
services.AddScoped(provider => tool);
|
||||
|
||||
services.AddScoped<IFunctionCallback>(provider =>
|
||||
{
|
||||
var funcTool = new McpToolAdapter(provider, server.Name, tool, clientManager);
|
||||
return funcTool;
|
||||
});
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Linq;
|
||||
global using System.Threading.Tasks;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
global using BotSharp.Abstraction.Agents;
|
||||
global using BotSharp.Abstraction.Agents.Enums;
|
||||
global using BotSharp.Abstraction.Agents.Models;
|
||||
global using BotSharp.Abstraction.Agents.Settings;
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using BotSharp.Abstraction.Functions;
|
||||
global using BotSharp.Abstraction.Functions.Models;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
global using BotSharp.Abstraction.MCP.Models;
|
||||
global using BotSharp.Abstraction.MCP.Services;
|
||||
|
||||
|
|
@ -221,6 +221,7 @@
|
|||
<PackageReference Include="Microsoft.Extensions.Http" />
|
||||
<PackageReference Include="Nanoid" />
|
||||
<PackageReference Include="Rougamo.Fody" />
|
||||
<PackageReference Include="ModelContextProtocol" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
using BotSharp.Core.MCP.Managers;
|
||||
using BotSharp.Core.MCP.Settings;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ModelContextProtocol.Client;
|
||||
|
||||
namespace BotSharp.Core.MCP.Services;
|
||||
|
||||
|
|
@ -7,13 +9,16 @@ public class McpService : IMcpService
|
|||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<McpService> _logger;
|
||||
private readonly McpClientManager _mcpClientManager;
|
||||
|
||||
public McpService(
|
||||
IServiceProvider services,
|
||||
ILogger<McpService> logger)
|
||||
ILogger<McpService> logger,
|
||||
McpClientManager mcpClient)
|
||||
{
|
||||
_services = services;
|
||||
_logger = logger;
|
||||
_mcpClientManager = mcpClient;
|
||||
}
|
||||
|
||||
public IEnumerable<McpServerOptionModel> GetServerConfigs()
|
||||
|
|
@ -24,9 +29,9 @@ public class McpService : IMcpService
|
|||
|
||||
foreach (var config in configs)
|
||||
{
|
||||
var tools = _services.GetServices<IFunctionCallback>()
|
||||
.Where(x => x.Provider == config.Name)
|
||||
.Select(x => x.Name);
|
||||
var tools = _mcpClientManager.GetMcpClientAsync(config.Id)
|
||||
.Result.ListToolsAsync()
|
||||
.Result.Select(x=> x.Name);
|
||||
|
||||
options.Add(new McpServerOptionModel
|
||||
{
|
||||
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
{
|
||||
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 MCPFunctionExecutor(mcpServerId, functionName, serviceProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
namespace BotSharp.Core.Routing.Executor;
|
||||
|
||||
public interface IFunctionExecutor
|
||||
{
|
||||
public Task<bool> ExecuteAsync(RoleDialogModel message);
|
||||
|
||||
public Task<string> GetIndicatorAsync(RoleDialogModel message);
|
||||
}
|
||||
|
|
@ -1,46 +1,34 @@
|
|||
using System.Text.Json;
|
||||
using BotSharp.Core.MCP.Managers;
|
||||
using ModelContextProtocol.Client;
|
||||
|
||||
namespace BotSharp.Core.MCP.Functions;
|
||||
namespace BotSharp.Core.Routing.Executor;
|
||||
|
||||
public class McpToolAdapter : IFunctionCallback
|
||||
public class MCPFunctionExecutor: IFunctionExecutor
|
||||
{
|
||||
private readonly string _provider;
|
||||
private readonly McpClientTool _tool;
|
||||
private readonly McpClientManager _clientManager;
|
||||
private string mcpServer;
|
||||
private string funcName;
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public McpToolAdapter(
|
||||
IServiceProvider services,
|
||||
string serverName,
|
||||
McpClientTool tool,
|
||||
McpClientManager client)
|
||||
{
|
||||
_services = services ?? throw new ArgumentNullException(nameof(services));
|
||||
_tool = tool ?? throw new ArgumentNullException(nameof(tool));
|
||||
_clientManager = client ?? throw new ArgumentNullException(nameof(client));
|
||||
_provider = serverName;
|
||||
public MCPFunctionExecutor(string mcpserver, string functionName, IServiceProvider services)
|
||||
{
|
||||
_services = services;
|
||||
this.mcpServer = mcpserver;
|
||||
this.funcName = functionName;
|
||||
_clientManager = services.GetRequiredService<McpClientManager>();
|
||||
}
|
||||
|
||||
public string Provider => _provider;
|
||||
public string Name => _tool.Name;
|
||||
|
||||
public async Task<bool> Execute(RoleDialogModel message)
|
||||
public async Task<bool> ExecuteAsync(RoleDialogModel message)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Convert arguments to dictionary format expected by mcpdotnet
|
||||
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
|
||||
var currentAgentId = message.CurrentAgentId;
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.LoadAgent(currentAgentId);
|
||||
var serverId = agent.McpTools.Where(t => t.Functions.Any(f => f.Name == Name)).FirstOrDefault().ServerId;
|
||||
Dictionary<string, object> argDict = JsonToDictionary(message.FunctionArgs);
|
||||
|
||||
var client = await _clientManager.GetMcpClientAsync(serverId);
|
||||
var client = await _clientManager.GetMcpClientAsync(mcpServer);
|
||||
|
||||
// Call the tool through mcpdotnet
|
||||
var result = await client.CallToolAsync(_tool.Name, !argDict.IsNullOrEmpty() ? argDict : []);
|
||||
var result = await client.CallToolAsync(funcName, !argDict.IsNullOrEmpty() ? argDict : []);
|
||||
|
||||
// Extract the text content from the result
|
||||
var json = string.Join("\n", result.Content.Where(c => c.Type == "text").Select(c => c.Text));
|
||||
|
|
@ -51,11 +39,17 @@ public class McpToolAdapter : IFunctionCallback
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
message.Content = $"Error when calling tool {Name} of MCP server {Provider}. {ex.Message}";
|
||||
message.Content = $"Error when calling tool {funcName} of MCP server {mcpServer}. {ex.Message}";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetIndicatorAsync(RoleDialogModel message)
|
||||
{
|
||||
return message.Indication ?? string.Empty;
|
||||
}
|
||||
|
||||
|
||||
private static Dictionary<string, object> JsonToDictionary(string? json)
|
||||
{
|
||||
if (string.IsNullOrEmpty(json))
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
using BotSharp.Abstraction.Functions;
|
||||
using BotSharp.Abstraction.Templating;
|
||||
using BotSharp.Core.Routing.Executor;
|
||||
|
||||
namespace BotSharp.Core.Routing;
|
||||
|
||||
|
|
@ -8,22 +9,22 @@ public partial class RoutingService
|
|||
public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
|
||||
{
|
||||
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == name);
|
||||
|
||||
var currentAgentId = message.CurrentAgentId;
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.GetAgent(currentAgentId);
|
||||
|
||||
var isFillDummyContent = false;
|
||||
var dummyFuncResponse = string.Empty;
|
||||
if (function == null)
|
||||
IFunctionExecutor funcExecutor = FunctionExecutorFactory.Create(name, agent, function, _services);
|
||||
|
||||
if (funcExecutor == null)
|
||||
{
|
||||
dummyFuncResponse = await GetDummyFunctionOutput(name, message);
|
||||
isFillDummyContent = !string.IsNullOrEmpty(dummyFuncResponse);
|
||||
if (!isFillDummyContent)
|
||||
{
|
||||
message.StopCompletion = true;
|
||||
message.Content = $"Can't find function implementation of {name}.";
|
||||
_logger.LogError(message.Content);
|
||||
return false;
|
||||
}
|
||||
message.StopCompletion = true;
|
||||
message.Content = $"Can't find function implementation of {name}.";
|
||||
_logger.LogError(message.Content);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Clone message
|
||||
var clonedMessage = RoleDialogModel.From(message);
|
||||
clonedMessage.FunctionName = name;
|
||||
|
|
@ -34,23 +35,13 @@ public partial class RoutingService
|
|||
|
||||
var progressService = _services.GetService<IConversationProgressService>();
|
||||
|
||||
// Before executing functions
|
||||
if (!isFillDummyContent)
|
||||
{
|
||||
clonedMessage.Indication = await function.GetIndication(message);
|
||||
}
|
||||
else
|
||||
{
|
||||
clonedMessage.Indication = "Running";
|
||||
}
|
||||
clonedMessage.Indication = await funcExecutor.GetIndicatorAsync(message);
|
||||
|
||||
if (progressService?.OnFunctionExecuting != null)
|
||||
{
|
||||
await progressService.OnFunctionExecuting(clonedMessage);
|
||||
}
|
||||
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.GetAgent(clonedMessage.CurrentAgentId);
|
||||
|
||||
foreach (var hook in hooks)
|
||||
{
|
||||
hook.SetAgent(agent);
|
||||
|
|
@ -61,19 +52,7 @@ public partial class RoutingService
|
|||
|
||||
try
|
||||
{
|
||||
if (clonedMessage.Handled)
|
||||
{
|
||||
clonedMessage.Content = clonedMessage.Content;
|
||||
}
|
||||
else if (!isFillDummyContent)
|
||||
{
|
||||
result = await function.Execute(clonedMessage);
|
||||
}
|
||||
else
|
||||
{
|
||||
clonedMessage.Content = dummyFuncResponse;
|
||||
result = true;
|
||||
}
|
||||
result = await funcExecutor.ExecuteAsync(clonedMessage);
|
||||
|
||||
// After functions have been executed
|
||||
foreach (var hook in hooks)
|
||||
|
|
@ -111,41 +90,6 @@ public partial class RoutingService
|
|||
message.Content = JsonSerializer.Serialize(message.Data);
|
||||
}
|
||||
|
||||
// Save to Storage as well
|
||||
/*if (!message.StopCompletion && message.FunctionName != "route_to_agent")
|
||||
{
|
||||
var storage = _services.GetRequiredService<IConversationStorage>();
|
||||
storage.Append(Context.ConversationId, message);
|
||||
}*/
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<string?> GetDummyFunctionOutput(string functionName, RoleDialogModel message)
|
||||
{
|
||||
if (string.IsNullOrEmpty(message.CurrentAgentId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var agentService = _services.GetRequiredService<IAgentService>();
|
||||
var agent = await agentService.GetAgent(message.CurrentAgentId);
|
||||
var found = agent?.Functions?.FirstOrDefault(x => x.Name == functionName);
|
||||
if (string.IsNullOrWhiteSpace(found?.Output))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
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(found.Output, dict);
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,51 +1,52 @@
|
|||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Text;
|
||||
global using System.Threading.Tasks;
|
||||
global using System.Linq;
|
||||
global using System.Text.Json;
|
||||
global using System.Net.Mime;
|
||||
global using System.Net.Http;
|
||||
global using System.Threading;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
global using EntityFrameworkCore.BootKit;
|
||||
global using BotSharp.Abstraction.Routing;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using BotSharp.Abstraction.Agents;
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Crontab.Models;
|
||||
global using BotSharp.Abstraction.Users;
|
||||
global using BotSharp.Abstraction.Roles;
|
||||
global using BotSharp.Abstraction.Roles.Models;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using BotSharp.Abstraction.Agents.Settings;
|
||||
global using BotSharp.Abstraction.Conversations.Settings;
|
||||
global using BotSharp.Abstraction.Agents.Enums;
|
||||
global using BotSharp.Abstraction.Agents.Models;
|
||||
global using BotSharp.Abstraction.Agents.Settings;
|
||||
global using BotSharp.Abstraction.Conversations;
|
||||
global using BotSharp.Abstraction.Conversations.Models;
|
||||
global using BotSharp.Abstraction.Conversations.Settings;
|
||||
global using BotSharp.Abstraction.Crontab.Models;
|
||||
global using BotSharp.Abstraction.Files;
|
||||
global using BotSharp.Abstraction.Files.Enums;
|
||||
global using BotSharp.Abstraction.Files.Models;
|
||||
global using BotSharp.Abstraction.Files.Utilities;
|
||||
global using BotSharp.Abstraction.Functions.Models;
|
||||
global using BotSharp.Abstraction.Infrastructures.Events;
|
||||
global using BotSharp.Abstraction.Knowledges.Models;
|
||||
global using BotSharp.Abstraction.Loggers.Services;
|
||||
global using BotSharp.Abstraction.MCP.Models;
|
||||
global using BotSharp.Abstraction.MCP.Services;
|
||||
global using BotSharp.Abstraction.Messaging.Enums;
|
||||
global using BotSharp.Abstraction.Plugins;
|
||||
global using BotSharp.Abstraction.Repositories;
|
||||
global using BotSharp.Abstraction.Repositories.Filters;
|
||||
global using BotSharp.Abstraction.Translation;
|
||||
global using BotSharp.Abstraction.Files;
|
||||
global using BotSharp.Abstraction.Files.Models;
|
||||
global using BotSharp.Abstraction.Files.Enums;
|
||||
global using BotSharp.Abstraction.Files.Utilities;
|
||||
global using BotSharp.Abstraction.Translation.Attributes;
|
||||
global using BotSharp.Abstraction.Messaging.Enums;
|
||||
global using BotSharp.Abstraction.Knowledges.Models;
|
||||
global using BotSharp.Abstraction.Roles;
|
||||
global using BotSharp.Abstraction.Roles.Models;
|
||||
global using BotSharp.Abstraction.Routing;
|
||||
global using BotSharp.Abstraction.SideCar.Attributes;
|
||||
global using BotSharp.Abstraction.Statistics.Models;
|
||||
global using BotSharp.Abstraction.Statistics.Enums;
|
||||
global using BotSharp.Abstraction.Statistics.Models;
|
||||
global using BotSharp.Abstraction.Statistics.Services;
|
||||
global using BotSharp.Abstraction.Loggers.Services;
|
||||
global using BotSharp.Abstraction.Infrastructures.Events;
|
||||
global using BotSharp.Core.Repository;
|
||||
global using BotSharp.Core.Routing;
|
||||
global using BotSharp.Abstraction.Translation;
|
||||
global using BotSharp.Abstraction.Translation.Attributes;
|
||||
global using BotSharp.Abstraction.Users;
|
||||
global using BotSharp.Abstraction.Utilities;
|
||||
global using BotSharp.Core.Agents.Services;
|
||||
global using BotSharp.Core.Conversations.Services;
|
||||
global using BotSharp.Core.Infrastructures;
|
||||
global using BotSharp.Core.Users.Services;
|
||||
global using BotSharp.Core.Loggers.Services;
|
||||
global using BotSharp.Core.Repository;
|
||||
global using BotSharp.Core.Statistics.Services;
|
||||
global using BotSharp.Core.Loggers.Services;
|
||||
global using BotSharp.Core.Users.Services;
|
||||
global using EntityFrameworkCore.BootKit;
|
||||
global using Microsoft.Extensions.DependencyInjection;
|
||||
global using Microsoft.Extensions.Logging;
|
||||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Linq;
|
||||
global using System.Net.Http;
|
||||
global using System.Net.Mime;
|
||||
global using System.Text;
|
||||
global using System.Text.Json;
|
||||
global using System.Threading;
|
||||
global using System.Threading.Tasks;
|
||||
|
|
|
|||
|
|
@ -36,7 +36,6 @@
|
|||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\tests\BotSharp.Plugin.PizzaBot\BotSharp.Plugin.PizzaBot.csproj" />
|
||||
<ProjectReference Include="..\BotSharp.ServiceDefaults\BotSharp.ServiceDefaults.csproj" />
|
||||
<ProjectReference Include="..\Infrastructure\BotSharp.Core.MCP\BotSharp.Core.MCP.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="$(SolutionName)==BotSharp">
|
||||
|
|
|
|||
Loading…
Reference in a new issue