2024-06-24 21:39:08 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2024-06-19 09:24:49 +00:00
|
|
|
|
2024-06-06 18:30:39 +00:00
|
|
|
namespace BotSharp.Core.Files.Hooks;
|
|
|
|
|
|
|
|
|
|
public class AttachmentProcessingHook : AgentHookBase
|
|
|
|
|
{
|
2024-06-24 21:39:08 +00:00
|
|
|
private static string TOOL_ASSISTANT = Guid.Empty.ToString();
|
2024-06-06 18:30:39 +00:00
|
|
|
|
|
|
|
|
public override string SelfId => string.Empty;
|
|
|
|
|
|
|
|
|
|
public AttachmentProcessingHook(IServiceProvider services, AgentSettings settings)
|
|
|
|
|
: base(services, settings)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-18 18:18:24 +00:00
|
|
|
public override void OnAgentLoaded(Agent agent)
|
|
|
|
|
{
|
|
|
|
|
var fileService = _services.GetRequiredService<IBotSharpFileService>();
|
|
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
var hasConvFiles = fileService.HasConversationUserFiles(conv.ConversationId);
|
|
|
|
|
|
|
|
|
|
if (hasConvFiles)
|
|
|
|
|
{
|
2024-06-24 21:39:08 +00:00
|
|
|
var (prompt, loadAttachmentFn) = GetLoadAttachmentFn();
|
|
|
|
|
if (loadAttachmentFn != null)
|
2024-06-06 18:30:39 +00:00
|
|
|
{
|
2024-06-24 21:39:08 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(prompt))
|
|
|
|
|
{
|
|
|
|
|
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (agent.Functions == null)
|
2024-06-07 04:02:10 +00:00
|
|
|
{
|
2024-06-24 21:39:08 +00:00
|
|
|
agent.Functions = new List<FunctionDef> { loadAttachmentFn };
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-06-19 09:24:49 +00:00
|
|
|
{
|
2024-06-24 21:39:08 +00:00
|
|
|
agent.Functions.Add(loadAttachmentFn);
|
|
|
|
|
}
|
2024-06-19 09:24:49 +00:00
|
|
|
}
|
2024-06-06 18:30:39 +00:00
|
|
|
}
|
2024-06-19 09:24:49 +00:00
|
|
|
|
|
|
|
|
base.OnAgentLoaded(agent);
|
2024-06-06 18:30:39 +00:00
|
|
|
}
|
2024-06-24 21:39:08 +00:00
|
|
|
|
|
|
|
|
private (string, FunctionDef?) GetLoadAttachmentFn()
|
|
|
|
|
{
|
|
|
|
|
var fnName = "load_attachment";
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var agent = db.GetAgent(TOOL_ASSISTANT);
|
|
|
|
|
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{fnName}_prompt"))?.Content ?? string.Empty;
|
|
|
|
|
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(fnName));
|
|
|
|
|
return (prompt, loadAttachmentFn);
|
|
|
|
|
}
|
2024-06-06 18:30:39 +00:00
|
|
|
}
|