2024-07-16 17:08:37 +00:00
|
|
|
namespace BotSharp.Plugin.FileHandler.Hooks;
|
|
|
|
|
|
|
|
|
|
public class FileHandlerHook : AgentHookBase, IAgentHook
|
|
|
|
|
{
|
2024-07-16 19:13:29 +00:00
|
|
|
private const string READ_IMAGE_FN = "read_image";
|
|
|
|
|
private const string READ_PDF_FN = "read_pdf";
|
2024-07-18 02:41:25 +00:00
|
|
|
private const string GENERATE_IMAGE_FN = "generate_image";
|
2024-07-19 19:18:22 +00:00
|
|
|
private const string EDIT_IMAGE_FN = "edit_image";
|
2024-07-16 17:08:37 +00:00
|
|
|
|
|
|
|
|
public override string SelfId => string.Empty;
|
|
|
|
|
|
|
|
|
|
public FileHandlerHook(IServiceProvider services, AgentSettings settings) : base(services, settings)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void OnAgentLoaded(Agent agent)
|
|
|
|
|
{
|
|
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
var isConvMode = conv.IsConversationMode();
|
|
|
|
|
|
|
|
|
|
if (isConvMode)
|
|
|
|
|
{
|
2024-07-19 19:18:22 +00:00
|
|
|
AddUtility(agent, UtilityName.ImageGenerator, GENERATE_IMAGE_FN);
|
2024-07-16 19:13:29 +00:00
|
|
|
AddUtility(agent, UtilityName.ImageReader, READ_IMAGE_FN);
|
2024-07-19 19:18:22 +00:00
|
|
|
AddUtility(agent, UtilityName.ImageEditor, EDIT_IMAGE_FN);
|
2024-07-16 19:13:29 +00:00
|
|
|
AddUtility(agent, UtilityName.PdfReader, READ_PDF_FN);
|
2024-07-19 19:18:22 +00:00
|
|
|
|
2024-07-16 17:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.OnAgentLoaded(agent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddUtility(Agent agent, string utility, string functionName)
|
|
|
|
|
{
|
|
|
|
|
if (!IsEnableUtility(agent, utility)) return;
|
|
|
|
|
|
|
|
|
|
var (prompt, fn) = GetPromptAndFunction(functionName);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-16 22:09:54 +00:00
|
|
|
|
|
|
|
|
private bool IsEnableUtility(Agent agent, string utility)
|
|
|
|
|
{
|
|
|
|
|
return !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(utility);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private (string, FunctionDef?) GetPromptAndFunction(string functionName)
|
|
|
|
|
{
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
|
|
|
|
|
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty;
|
|
|
|
|
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName));
|
|
|
|
|
return (prompt, loadAttachmentFn);
|
|
|
|
|
}
|
2024-07-16 17:08:37 +00:00
|
|
|
}
|