Skip to content

Commit efeaaed

Browse files
committed
Change agent load timing in InstructService.
1 parent a18d1ea commit efeaaed

5 files changed

Lines changed: 26 additions & 16 deletions

File tree

‎docs/architecture/hooks.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ Task OnHumanInterventionNeeded(RoleDialogModel message);
4242
```
4343
More information about conversation hook please go to [Conversation Hook](../conversation/hook.md).
4444

45-
### Conversation State Hook
4645
`IConversationHook`
4746
```csharp
4847
Task OnStateLoaded(ConversationState state);
4948
Task OnStateChanged(string name, string preValue, string currentValue);
5049
```
5150

52-
### Content Generating Hook
51+
## Content Generating Hook
5352
`IContentGeneratingHook`
5453

5554
Model content generating hook, it can be used for logging, metrics and tracing.
@@ -59,4 +58,10 @@ Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations);
5958

6059
// After content generated.
6160
Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats);
61+
```
62+
63+
`IInstructHook`
64+
```csharp
65+
Task BeforeCompletion(Agent agent, RoleDialogModel message);
66+
Task AfterCompletion(Agent agent, InstructResult result);
6267
```

‎src/Infrastructure/BotSharp.Abstraction/Instructs/IInstructHook.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace BotSharp.Abstraction.Instructs;
55
public interface IInstructHook
66
{
77
string SelfId { get; }
8-
Task BeforeCompletion(RoleDialogModel message);
9-
Task AfterCompletion(InstructResult result);
8+
Task BeforeCompletion(Agent agent, RoleDialogModel message);
9+
Task AfterCompletion(Agent agent, InstructResult result);
1010
}

‎src/Infrastructure/BotSharp.Abstraction/Instructs/InstructHookBase.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ namespace BotSharp.Abstraction.Instructs;
55
public class InstructHookBase : IInstructHook
66
{
77
public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!");
8-
public virtual async Task AfterCompletion(InstructResult result)
8+
9+
public virtual async Task BeforeCompletion(Agent agent, RoleDialogModel message)
910
{
1011
return;
1112
}
1213

13-
public virtual async Task BeforeCompletion(RoleDialogModel message)
14+
public virtual async Task AfterCompletion(Agent agent, InstructResult result)
1415
{
1516
return;
1617
}

‎src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public InstructService(IServiceProvider services, ILogger<InstructService> logge
1616

1717
public async Task<InstructResult> Execute(string agentId, RoleDialogModel message, string? templateName = null)
1818
{
19+
var agentService = _services.GetRequiredService<IAgentService>();
20+
Agent agent = await agentService.LoadAgent(agentId);
21+
1922
// Trigger before completion hooks
2023
var hooks = _services.GetServices<IInstructHook>();
2124
foreach (var hook in hooks)
@@ -25,7 +28,7 @@ public async Task<InstructResult> Execute(string agentId, RoleDialogModel messag
2528
continue;
2629
}
2730

28-
await hook.BeforeCompletion(message);
31+
await hook.BeforeCompletion(agent, message);
2932

3033
// Interrupted by hook
3134
if (message.StopCompletion)
@@ -39,9 +42,7 @@ public async Task<InstructResult> Execute(string agentId, RoleDialogModel messag
3942
}
4043

4144
// Render prompt
42-
var agentService = _services.GetRequiredService<IAgentService>();
43-
Agent agent = await agentService.LoadAgent(agentId);
44-
var prompt = string.IsNullOrEmpty(templateName) ?
45+
var prompt = string.IsNullOrEmpty(templateName) ?
4546
agentService.RenderedInstruction(agent) :
4647
agentService.RenderedTemplate(agent, templateName);
4748

@@ -60,7 +61,7 @@ public async Task<InstructResult> Execute(string agentId, RoleDialogModel messag
6061
continue;
6162
}
6263

63-
await hook.AfterCompletion(response);
64+
await hook.AfterCompletion(agent, response);
6465
}
6566

6667
return response;

‎src/Infrastructure/BotSharp.Core/Users/Services/UserIdentity.cs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Abstraction.Users;
21
using Microsoft.AspNetCore.Http;
32
using System.Security.Claims;
43

@@ -15,11 +14,15 @@ public UserIdentity(IHttpContextAccessor contextAccessor)
1514
}
1615

1716

18-
public string Id => _claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value;
17+
public string Id
18+
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
1919

20-
public string Email => _claims.First(x => x.Type == ClaimTypes.Email).Value;
20+
public string Email
21+
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;
2122

22-
public string FirstName => _claims.First(x => x.Type == ClaimTypes.GivenName).Value;
23+
public string FirstName
24+
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.GivenName)?.Value;
2325

24-
public string LastName => _claims.First(x => x.Type == ClaimTypes.Surname).Value;
26+
public string LastName
27+
=> _claims.FirstOrDefault(x => x.Type == ClaimTypes.Surname)?.Value;
2528
}

0 commit comments

Comments
 (0)