Skip to content

Commit c6125f4

Browse files
authored
Merge pull request #1058 from yileicn/master
optimize hook
2 parents a2fdc76 + d10f0a6 commit c6125f4

File tree

29 files changed

+98
-77
lines changed

29 files changed

+98
-77
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookProvider.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using BotSharp.Abstraction.Conversations;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace BotSharp.Abstraction.Hooks
10+
{
11+
public static class HookProvider
12+
{
13+
public static List<T> GetHooks<T>(this IServiceProvider services, string agentId) where T : IHookBase
14+
{
15+
var hooks = services.GetServices<T>().Where(p => p.IsMatch(agentId));
16+
return hooks.ToList();
17+
}
18+
19+
public static List<T> GetHooksOrderByPriority<T>(this IServiceProvider services, string agentId) where T: IConversationHook
20+
{
21+
var hooks = services.GetServices<T>().Where(p => p.IsMatch(agentId));
22+
return hooks.OrderBy(p => p.Priority).ToList();
23+
}
24+
}
25+
}

src/Infrastructure/BotSharp.Abstraction/Hooks/IHookBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public interface IHookBase
1212
/// Agent Id
1313
/// </summary>
1414
string SelfId => string.Empty;
15-
bool IsMatch(string id) => string.IsNullOrEmpty(SelfId) || SelfId == id;
15+
bool IsMatch(string agentId) => string.IsNullOrEmpty(SelfId) || SelfId == agentId;
1616
}
1717
}

src/Infrastructure/BotSharp.Core.Realtime/Services/RealtimeHub.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Functions.Models;
2+
using BotSharp.Abstraction.Hooks;
23
using BotSharp.Abstraction.Options;
34
using BotSharp.Core.Infrastructures;
45

@@ -23,7 +24,6 @@ public RealtimeHub(IServiceProvider services, ILogger<RealtimeHub> logger)
2324

2425
public async Task ConnectToModel(Func<string, Task>? responseToUser = null, Func<string, Task>? init = null)
2526
{
26-
var hookProvider = _services.GetService<ConversationHookProvider>();
2727
var convService = _services.GetRequiredService<IConversationService>();
2828
convService.SetConversationId(_conn.ConversationId, []);
2929
var conversation = await convService.GetConversation(_conn.ConversationId);
@@ -105,7 +105,8 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
105105
dialogs.Add(message);
106106
storage.Append(_conn.ConversationId, message);
107107

108-
foreach (var hook in hookProvider?.HooksOrderByPriority ?? [])
108+
var hooks = _services.GetHooksOrderByPriority<IConversationHook>(_conn.CurrentAgentId);
109+
foreach (var hook in hooks)
109110
{
110111
hook.SetAgent(agent)
111112
.SetConversation(conversation);
@@ -126,7 +127,8 @@ await HookEmitter.Emit<IRoutingHook>(_services, async hook => await hook.OnRouti
126127
storage.Append(_conn.ConversationId, message);
127128
routing.Context.SetMessageId(_conn.ConversationId, message.MessageId);
128129

129-
foreach (var hook in hookProvider?.HooksOrderByPriority ?? [])
130+
var hooks = _services.GetHooksOrderByPriority<IConversationHook>(_conn.CurrentAgentId);
131+
foreach (var hook in hooks)
130132
{
131133
hook.SetAgent(agent)
132134
.SetConversation(conversation);

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Hooks;
12
using BotSharp.Abstraction.Infrastructures.Enums;
23
using BotSharp.Abstraction.Messaging;
34
using BotSharp.Abstraction.Messaging.Models.RichContent;
@@ -29,7 +30,6 @@ public async Task<bool> SendMessage(string agentId,
2930
var dialogs = conv.GetDialogHistory();
3031

3132
var statistics = _services.GetRequiredService<ITokenStatistics>();
32-
var hookProvider = _services.GetRequiredService<ConversationHookProvider>();
3333

3434
RoleDialogModel response = message;
3535
bool stopCompletion = false;
@@ -44,7 +44,8 @@ public async Task<bool> SendMessage(string agentId,
4444
message.Payload = replyMessage.Payload;
4545
}
4646

47-
foreach (var hook in hookProvider.HooksOrderByPriority)
47+
var hooks = _services.GetHooksOrderByPriority<IConversationHook>(message.CurrentAgentId);
48+
foreach (var hook in hooks)
4849
{
4950
hook.SetAgent(agent)
5051
.SetConversation(conversation);

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Hooks;
12
using BotSharp.Abstraction.Infrastructures.Enums;
23

34
namespace BotSharp.Core.Conversations.Services;
@@ -31,9 +32,7 @@ public async Task UpdateBreakpoint(bool resetStates = false, string? reason = nu
3132
states.CleanStates(excludedStates);
3233
}
3334

34-
var hooks = _services
35-
.GetRequiredService<ConversationHookProvider>()
36-
.HooksOrderByPriority;
35+
var hooks = _services.GetHooksOrderByPriority<IConversationHook>(routingCtx.GetCurrentAgentId());
3736

3837
// Before executing functions
3938
foreach (var hook in hooks)

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Conversations.Enums;
2+
using BotSharp.Abstraction.Hooks;
23
using BotSharp.Abstraction.Models;
34

45
namespace BotSharp.Core.Conversations.Services;
@@ -116,7 +117,7 @@ public async Task<Conversation> NewConversation(Conversation sess)
116117

117118
db.CreateNewConversation(record);
118119

119-
var hooks = _services.GetServices<IConversationHook>();
120+
var hooks = _services.GetHooks<IConversationHook>(record.AgentId);
120121

121122
foreach (var hook in hooks)
122123
{

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using BotSharp.Abstraction.Conversations.Enums;
18+
using BotSharp.Abstraction.Hooks;
1819
using BotSharp.Abstraction.Options;
1920
using BotSharp.Abstraction.SideCar;
2021

@@ -28,6 +29,7 @@ public class ConversationStateService : IConversationStateService
2829
private readonly ILogger _logger;
2930
private readonly IServiceProvider _services;
3031
private readonly IBotSharpRepository _db;
32+
private readonly IRoutingContext _routingContext;
3133
private readonly IConversationSideCar? _sidecar;
3234
private string _conversationId;
3335
/// <summary>
@@ -42,10 +44,12 @@ public class ConversationStateService : IConversationStateService
4244
public ConversationStateService(
4345
IServiceProvider services,
4446
IBotSharpRepository db,
47+
IRoutingContext routingContext,
4548
ILogger<ConversationStateService> logger)
4649
{
4750
_services = services;
4851
_db = db;
52+
_routingContext = routingContext;
4953
_logger = logger;
5054
_curStates = new ConversationState();
5155
_historyStates = new ConversationState();
@@ -87,7 +91,6 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
8791
}
8892

8993
_logger.LogDebug($"[STATE] {name} = {value}");
90-
var routingCtx = _services.GetRequiredService<IRoutingContext>();
9194

9295
var isNoChange = ContainsState(name)
9396
&& preValue == currentValue
@@ -98,15 +101,15 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
98101
&& prevLeafNode?.Active == curActive
99102
&& pair?.Readonly == readOnly;
100103

101-
var hooks = _services.GetServices<IConversationHook>();
104+
var hooks = _services.GetHooks<IConversationHook>(_routingContext.GetCurrentAgentId());
102105
if (!ContainsState(name) || preValue != currentValue || prevLeafNode?.ActiveRounds != curActiveRounds)
103106
{
104107
foreach (var hook in hooks)
105108
{
106109
hook.OnStateChanged(new StateChangeModel
107110
{
108111
ConversationId = _conversationId,
109-
MessageId = routingCtx.MessageId,
112+
MessageId = _routingContext.MessageId,
110113
Name = name,
111114
BeforeValue = preValue,
112115
BeforeActiveRounds = prevLeafNode?.ActiveRounds,
@@ -129,7 +132,7 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
129132
var newValue = new StateValue
130133
{
131134
Data = currentValue,
132-
MessageId = routingCtx.MessageId,
135+
MessageId = _routingContext.MessageId,
133136
Active = curActive,
134137
ActiveRounds = curActiveRounds,
135138
DataType = valueType,
@@ -171,8 +174,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =
171174
return endNodes;
172175
}
173176

174-
var routingCtx = _services.GetRequiredService<IRoutingContext>();
175-
var curMsgId = routingCtx.MessageId;
177+
var curMsgId = _routingContext.MessageId;
176178
var dialogs = _db.GetConversationDialogs(conversationId);
177179
var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User)
178180
.GroupBy(x => x.MetaData?.MessageId)
@@ -225,7 +227,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =
225227
}
226228

227229
_logger.LogInformation($"Loaded conversation states: {conversationId}");
228-
var hooks = _services.GetServices<IConversationHook>();
230+
var hooks = _services.GetHooks<IConversationHook>(_routingContext.GetCurrentAgentId());
229231
foreach (var hook in hooks)
230232
{
231233
hook.OnStateLoaded(_curStates).Wait();
@@ -277,29 +279,28 @@ public bool RemoveState(string name)
277279
{
278280
if (!ContainsState(name)) return false;
279281

280-
var routingCtx = _services.GetRequiredService<IRoutingContext>();
281282
var value = _curStates[name];
282283
var leafNode = value?.Values?.LastOrDefault();
283284
if (value == null || !value.Versioning || leafNode == null) return false;
284285

285286
_curStates[name].Values.Add(new StateValue
286287
{
287288
Data = leafNode.Data,
288-
MessageId = routingCtx.MessageId,
289+
MessageId = _routingContext.MessageId,
289290
Active = false,
290291
ActiveRounds = leafNode.ActiveRounds,
291292
DataType = leafNode.DataType,
292293
Source = leafNode.Source,
293294
UpdateTime = DateTime.UtcNow
294295
});
295296

296-
var hooks = _services.GetServices<IConversationHook>();
297+
var hooks = _services.GetHooks<IConversationHook>(_routingContext.GetCurrentAgentId());
297298
foreach (var hook in hooks)
298299
{
299300
hook.OnStateChanged(new StateChangeModel
300301
{
301302
ConversationId = _conversationId,
302-
MessageId = routingCtx.MessageId,
303+
MessageId = _routingContext.MessageId,
303304
Name = name,
304305
BeforeValue = leafNode.Data,
305306
BeforeActiveRounds = leafNode.ActiveRounds,
@@ -316,8 +317,7 @@ public bool RemoveState(string name)
316317

317318
public void CleanStates(params string[] excludedStates)
318319
{
319-
var routingCtx = _services.GetRequiredService<IRoutingContext>();
320-
var curMsgId = routingCtx.MessageId;
320+
var curMsgId = _routingContext.MessageId;
321321
var utcNow = DateTime.UtcNow;
322322

323323
foreach (var key in _curStates.Keys)

src/Infrastructure/BotSharp.Core/Infrastructures/HookEmitter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static HookEmittedResult Emit<T>(IServiceProvider services, Action<T> act
99
{
1010
var logger = services.GetRequiredService<ILogger<T>>();
1111
var result = new HookEmittedResult();
12-
var hooks = services.GetServices<T>().Where(p => p.IsMatch(agentId));
12+
var hooks = services.GetHooks<T>(agentId);
1313
option = option ?? new();
1414

1515
foreach (var hook in hooks)
@@ -40,7 +40,7 @@ public static async Task<HookEmittedResult> Emit<T>(IServiceProvider services, F
4040
{
4141
var logger = services.GetRequiredService<ILogger<T>>();
4242
var result = new HookEmittedResult();
43-
var hooks = services.GetServices<T>().Where(p => p.IsMatch(agentId));
43+
var hooks = services.GetHooks<T>(agentId);
4444
option = option ?? new();
4545

4646
foreach (var hook in hooks)

src/Infrastructure/BotSharp.Core/Instructs/Services/InstructService.Execute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Hooks;
12
using BotSharp.Abstraction.Instructs;
23
using BotSharp.Abstraction.Instructs.Models;
34
using BotSharp.Abstraction.MLTasks;
@@ -23,7 +24,7 @@ public async Task<InstructResult> Execute(string agentId, RoleDialogModel messag
2324
}
2425

2526
// Trigger before completion hooks
26-
var hooks = _services.GetServices<IInstructHook>().Where(p => p.IsMatch(agentId));
27+
var hooks = _services.GetHooks<IInstructHook>(agentId);
2728
foreach (var hook in hooks)
2829
{
2930
await hook.BeforeCompletion(agent, message);

src/Infrastructure/BotSharp.Core/Routing/Functions/HumanInterventionNeededFn.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Functions;
2+
using BotSharp.Abstraction.Hooks;
23

34
namespace BotSharp.Core.Routing.Functions;
45

@@ -15,9 +16,7 @@ public HumanInterventionNeededFn(IServiceProvider services)
1516

1617
public async Task<bool> Execute(RoleDialogModel message)
1718
{
18-
var hooks = _services
19-
.GetRequiredService<ConversationHookProvider>()
20-
.HooksOrderByPriority;
19+
var hooks = _services.GetHooksOrderByPriority<IConversationHook>(message.CurrentAgentId);
2120

2221
foreach (var hook in hooks)
2322
{

src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeFunction.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Functions;
2+
using BotSharp.Abstraction.Hooks;
23
using BotSharp.Abstraction.Templating;
34
using BotSharp.Core.Routing.Executor;
45

@@ -29,10 +30,6 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
2930
var clonedMessage = RoleDialogModel.From(message);
3031
clonedMessage.FunctionName = name;
3132

32-
var hooks = _services
33-
.GetRequiredService<ConversationHookProvider>()
34-
.HooksOrderByPriority;
35-
3633
var progressService = _services.GetService<IConversationProgressService>();
3734

3835
clonedMessage.Indication = await funcExecutor.GetIndicatorAsync(message);
@@ -41,7 +38,8 @@ public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
4138
{
4239
await progressService.OnFunctionExecuting(clonedMessage);
4340
}
44-
41+
42+
var hooks = _services.GetHooksOrderByPriority<IConversationHook>(clonedMessage.CurrentAgentId);
4543
foreach (var hook in hooks)
4644
{
4745
hook.SetAgent(agent);

src/Plugins/BotSharp.Plugin.AnthropicAI/Providers/ChatCompletionProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Anthropic.SDK.Common;
22
using BotSharp.Abstraction.Conversations;
3+
using BotSharp.Abstraction.Hooks;
34
using BotSharp.Abstraction.MLTasks.Settings;
45
using System.Text.Json.Nodes;
56
using System.Text.Json.Serialization;
@@ -29,7 +30,7 @@ public ChatCompletionProvider(AnthropicSettings settings,
2930

3031
public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
3132
{
32-
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
33+
var contentHooks = _services.GetHooks<IContentGeneratingHook>(agent.Id);
3334

3435
// Before chat completion hook
3536
foreach (var hook in contentHooks)

src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/Chat/ChatCompletionProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Azure;
22
using BotSharp.Abstraction.Files.Utilities;
3+
using BotSharp.Abstraction.Hooks;
34
using OpenAI.Chat;
45
using System.ClientModel;
56

@@ -29,7 +30,7 @@ public ChatCompletionProvider(
2930

3031
public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
3132
{
32-
var contentHooks = _services.GetServices<IContentGeneratingHook>().ToList();
33+
var contentHooks = _services.GetHooks<IContentGeneratingHook>(agent.Id);
3334

3435
// Before chat completion hook
3536
foreach (var hook in contentHooks)
@@ -128,7 +129,7 @@ public async Task<bool> GetChatCompletionsAsync(Agent agent,
128129
Func<RoleDialogModel, Task> onMessageReceived,
129130
Func<RoleDialogModel, Task> onFunctionExecuting)
130131
{
131-
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
132+
var hooks = _services.GetHooks<IContentGeneratingHook>(agent.Id);
132133

133134
// Before chat completion hook
134135
foreach (var hook in hooks)

0 commit comments

Comments
 (0)