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;
|
|
|
|
|
|
2025-03-26 01:53:43 +00:00
|
|
|
namespace BotSharp.Plugin.PizzaBot.Hooks;
|
2025-03-18 01:49:36 +00:00
|
|
|
|
2025-03-26 01:53:43 +00:00
|
|
|
public class PizzaBotConversationHook : ConversationHookBase
|
2025-03-18 01:49:36 +00:00
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
2025-03-26 01:53:43 +00:00
|
|
|
private readonly IConversationStateService _states;
|
2025-03-18 01:49:36 +00:00
|
|
|
|
2025-03-26 01:53:43 +00:00
|
|
|
public PizzaBotConversationHook(IServiceProvider services,
|
2025-03-18 01:49:36 +00:00
|
|
|
IConversationStateService states)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_states = states;
|
|
|
|
|
}
|
2025-03-26 01:53:43 +00:00
|
|
|
|
|
|
|
|
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>();
|
2025-03-18 10:43:31 +00:00
|
|
|
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
|
|
|
|
2025-03-26 01:53:43 +00:00
|
|
|
if (agent.McpTools.Any(item => item.Functions.Any(x => x.Name == message.FunctionName)))
|
2025-03-18 01:49:36 +00:00
|
|
|
{
|
2025-03-18 10:43:31 +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
|
|
|
}
|