diff --git a/docs/architecture/hooks.md b/docs/architecture/hooks.md index 6071a37d..903bf129 100644 --- a/docs/architecture/hooks.md +++ b/docs/architecture/hooks.md @@ -42,14 +42,13 @@ Task OnHumanInterventionNeeded(RoleDialogModel message); ``` More information about conversation hook please go to [Conversation Hook](../conversation/hook.md). -### Conversation State Hook `IConversationHook` ```csharp Task OnStateLoaded(ConversationState state); Task OnStateChanged(string name, string preValue, string currentValue); ``` -### Content Generating Hook +## Content Generating Hook `IContentGeneratingHook` Model content generating hook, it can be used for logging, metrics and tracing. @@ -59,4 +58,10 @@ Task BeforeGenerating(Agent agent, List conversations); // After content generated. Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats); +``` + +`IInstructHook` +```csharp +Task BeforeCompletion(Agent agent, RoleDialogModel message); +Task AfterCompletion(Agent agent, InstructResult result); ``` \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs index 9ea5f5de..8eaee99b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs @@ -5,6 +5,6 @@ namespace BotSharp.Abstraction.Instructs; public interface IInstructHook { string SelfId { get; } - Task BeforeCompletion(RoleDialogModel message); - Task AfterCompletion(InstructResult result); + Task BeforeCompletion(Agent agent, RoleDialogModel message); + Task AfterCompletion(Agent agent, InstructResult result); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs index 9a973b6e..b59e4640 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs @@ -5,12 +5,13 @@ namespace BotSharp.Abstraction.Instructs; public class InstructHookBase : IInstructHook { public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!"); - public virtual async Task AfterCompletion(InstructResult result) + + public virtual async Task BeforeCompletion(Agent agent, RoleDialogModel message) { return; } - public virtual async Task BeforeCompletion(RoleDialogModel message) + public virtual async Task AfterCompletion(Agent agent, InstructResult result) { return; } diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs index b8f58cfe..30d2caa5 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs @@ -16,6 +16,9 @@ public partial class InstructService : IInstructService public async Task Execute(string agentId, RoleDialogModel message, string? templateName = null) { + var agentService = _services.GetRequiredService(); + Agent agent = await agentService.LoadAgent(agentId); + // Trigger before completion hooks var hooks = _services.GetServices(); foreach (var hook in hooks) @@ -25,7 +28,7 @@ public partial class InstructService : IInstructService continue; } - await hook.BeforeCompletion(message); + await hook.BeforeCompletion(agent, message); // Interrupted by hook if (message.StopCompletion) @@ -39,9 +42,7 @@ public partial class InstructService : IInstructService } // Render prompt - var agentService = _services.GetRequiredService(); - Agent agent = await agentService.LoadAgent(agentId); - var prompt = string.IsNullOrEmpty(templateName) ? + var prompt = string.IsNullOrEmpty(templateName) ? agentService.RenderedInstruction(agent) : agentService.RenderedTemplate(agent, templateName); @@ -60,7 +61,7 @@ public partial class InstructService : IInstructService continue; } - await hook.AfterCompletion(response); + await hook.AfterCompletion(agent, response); } return response; diff --git a/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs b/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs index 7482d5d9..00958b11 100644 --- a/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs +++ b/src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Users; using Microsoft.AspNetCore.Http; using System.Security.Claims; @@ -15,11 +14,15 @@ public class UserIdentity : IUserIdentity } - public string Id => _claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value; + public string Id + => _claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; - public string Email => _claims.First(x => x.Type == ClaimTypes.Email).Value; + public string Email + => _claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value; - public string FirstName => _claims.First(x => x.Type == ClaimTypes.GivenName).Value; + public string FirstName + => _claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value; - public string LastName => _claims.First(x => x.Type == ClaimTypes.Surname).Value; + public string LastName + => _claims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value; }