BotSharp/tests/BotSharp.Plugin.PizzaBot/Hooks/PizzaBotConversationHook.cs

52 lines
1.5 KiB
C#
Raw Normal View History

2025-03-18 01:49:36 +00:00
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using System.Linq;
using System.Text.Json;
namespace BotSharp.Plugin.PizzaBot.Hooks;
2025-03-18 01:49:36 +00:00
public class PizzaBotConversationHook : ConversationHookBase
2025-03-18 01:49:36 +00:00
{
private readonly IServiceProvider _services;
private readonly IConversationStateService _states;
2025-03-18 01:49:36 +00:00
public PizzaBotConversationHook(IServiceProvider services,
2025-03-18 01:49:36 +00:00
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);
}
2025-03-28 01:49:54 +00:00
#if USE_BOTSHARP
2025-03-18 01:49:36 +00:00
public override async Task OnResponseGenerated(RoleDialogModel message)
{
var agentService = _services.GetRequiredService<IAgentService>();
var state = _services.GetRequiredService<IConversationStateService>();
2025-03-18 01:49:36 +00:00
var agent = await agentService.LoadAgent(message.CurrentAgentId);
2025-03-28 01:49:54 +00:00
if (agent.McpTools.Any(item => item.Functions.Any(x => x.Name == message.FunctionName)))
2025-03-18 01:49:36 +00:00
{
var data = JsonDocument.Parse(JsonSerializer.Serialize(message.Data));
state.SaveStateByArgs(data);
2025-03-18 01:49:36 +00:00
}
2025-03-28 01:49:54 +00:00
2025-03-18 01:49:36 +00:00
await base.OnResponseGenerated(message);
}
2025-03-28 01:49:54 +00:00
#endif
2025-03-18 01:49:36 +00:00
}