move out MCPConversationHook designed for PizzaBot

This commit is contained in:
Haiping Chen 2025-03-25 20:53:43 -05:00
parent 22b6ce5d0d
commit 2c09c78f97
3 changed files with 20 additions and 31 deletions

View file

@ -40,7 +40,6 @@ public class McpPlugin : IBotSharpPlugin
}
// Register hooks
services.AddScoped<IAgentHook, MCPToolAgentHook>();
services.AddScoped<IConversationHook, MCPResponseHook>();
}
private async Task RegisterFunctionCall(IServiceCollection services, McpServerConfig server)

View file

@ -1,31 +1,43 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace BotSharp.MCP.Hooks;
namespace BotSharp.Plugin.PizzaBot.Hooks;
public class MCPResponseHook : ConversationHookBase
public class PizzaBotConversationHook : ConversationHookBase
{
private readonly IServiceProvider _services;
private readonly IConversationStateService _states;
private readonly IConversationStateService _states;
public MCPResponseHook(IServiceProvider services,
public PizzaBotConversationHook(IServiceProvider services,
IConversationStateService states)
{
_services = services;
_states = states;
}
public override async Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg)
{
if (replyMsg.FunctionName == "get_pizza_types")
{
// message.StopCompletion = true;
}
return;
}
public override Task OnTaskCompleted(RoleDialogModel message)
{
return base.OnTaskCompleted(message);
}
public override async Task OnResponseGenerated(RoleDialogModel message)
{
var agentService = _services.GetRequiredService<IAgentService>();
var state = _services.GetRequiredService<IConversationStateService>();
var agent = await agentService.LoadAgent(message.CurrentAgentId);
if(agent.McpTools.Any(item => item.Functions.Any(x=> x.Name == message.FunctionName)))
if (agent.McpTools.Any(item => item.Functions.Any(x => x.Name == message.FunctionName)))
{
var data = JsonDocument.Parse(JsonSerializer.Serialize(message.Data));
state.SaveStateByArgs(data);
@ -33,4 +45,3 @@ public class MCPResponseHook : ConversationHookBase
await base.OnResponseGenerated(message);
}
}

View file

@ -1,21 +0,0 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.Plugin.PizzaBot.Hooks;
public class PizzaTypeConversationHook : ConversationHookBase
{
public override async Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg)
{
if (replyMsg.FunctionName == "get_pizza_types")
{
// message.StopCompletion = true;
}
return;
}
public override Task OnTaskCompleted(RoleDialogModel message)
{
return base.OnTaskCompleted(message);
}
}