BotSharp/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerHook.cs

60 lines
1.9 KiB
C#
Raw Normal View History

2024-06-26 03:38:01 +00:00
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Settings;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Plugin.HttpHandler.Enums;
namespace BotSharp.Plugin.HttpHandler.Hooks;
public class HttpHandlerHook : AgentHookBase
{
2024-07-02 18:36:45 +00:00
private static string UTILITY_ASSISTANT = Guid.Empty.ToString();
2024-07-02 20:43:38 +00:00
private static string FUNCTION_NAME = "handle_http_request";
2024-06-26 03:38:01 +00:00
public override string SelfId => string.Empty;
public HttpHandlerHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
}
public override void OnAgentLoaded(Agent agent)
{
var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
2024-07-02 18:36:45 +00:00
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(Utility.HttpHandler);
2024-06-26 03:38:01 +00:00
if (isConvMode && isEnabled)
{
var (prompt, fn) = GetPromptAndFunction();
if (fn != null)
{
if (!string.IsNullOrWhiteSpace(prompt))
{
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
}
if (agent.Functions == null)
{
agent.Functions = new List<FunctionDef> { fn };
}
else
{
agent.Functions.Add(fn);
}
}
}
base.OnAgentLoaded(agent);
}
private (string, FunctionDef?) GetPromptAndFunction()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
2024-07-02 18:36:45 +00:00
var agent = db.GetAgent(UTILITY_ASSISTANT);
2024-07-02 20:43:38 +00:00
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
2024-06-26 03:38:01 +00:00
return (prompt, loadAttachmentFn);
}
}