Skip to content

Commit 040009b

Browse files
authored
Merge pull request #1284 from yileicn/master
optimize IRoutingContext,IRoutingService,IHttpRequestHook use async method
2 parents a02bbcd + a3f27ab commit 040009b

File tree

33 files changed

+145
-153
lines changed

33 files changed

+145
-153
lines changed

src/Infrastructure/BotSharp.Abstraction/Http/IHttpRequestHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace BotSharp.Abstraction.Http;
44

55
public interface IHttpRequestHook
66
{
7-
void OnAddHttpHeaders(HttpHeaders headers, Uri uri);
7+
Task OnAddHttpHeaders(HttpHeaders headers, Uri uri);
88
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ Task<bool> UpdateRole(Role role, bool updateRoleAgents = false)
7070
#region Agent
7171
Task UpdateAgent(Agent agent, AgentField field)
7272
=> throw new NotImplementedException();
73-
Agent? GetAgent(string agentId, bool basicsOnly = false)
74-
=> throw new NotImplementedException();
75-
Task<Agent?> GetAgentAsync(string agentId, bool basicsOnly = false)
73+
Task<Agent?> GetAgent(string agentId, bool basicsOnly = false)
7674
=> throw new NotImplementedException();
7775
Task<List<Agent>> GetAgents(AgentFilter filter)
7876
=> throw new NotImplementedException();

src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public interface IRoutingContext
1313
bool IsEmpty { get; }
1414
string IntentName { get; set; }
1515
int AgentCount { get; }
16-
void Push(string agentId, string? reason = null, bool updateLazyRouting = true);
17-
void Pop(string? reason = null, bool updateLazyRouting = true);
18-
void PopTo(string agentId, string reason, bool updateLazyRouting = true);
19-
void Replace(string agentId, string? reason = null, bool updateLazyRouting = true);
20-
void Empty(string? reason = null);
16+
Task Push(string agentId, string? reason = null, bool updateLazyRouting = true);
17+
Task Pop(string? reason = null, bool updateLazyRouting = true);
18+
Task PopTo(string agentId, string reason, bool updateLazyRouting = true);
19+
Task Replace(string agentId, string? reason = null, bool updateLazyRouting = true);
20+
Task Empty(string? reason = null);
2121

2222

2323
int CurrentRecursionDepth { get; }

src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ public interface IRoutingService
1010
/// </summary>
1111
/// <param name="profiles">router's profile</param>
1212
/// <returns></returns>
13-
RoutableAgent[] GetRoutableAgents(List<string> profiles);
13+
Task<RoutableAgent[]> GetRoutableAgents(List<string> profiles);
1414

1515
/// <summary>
1616
/// Get rules by agent name
1717
/// </summary>
1818
/// <param name="name">agent name</param>
1919
/// <returns></returns>
20-
RoutingRule[] GetRulesByAgentName(string name);
20+
Task<RoutingRule[]> GetRulesByAgentName(string name);
2121

2222
/// <summary>
2323
/// Get rules by agent id
2424
/// </summary>
2525
/// <param name="id">agent id </param>
2626
/// <returns></returns>
27-
RoutingRule[] GetRulesByAgentId(string id);
27+
Task<RoutingRule[]> GetRulesByAgentId(string id);
2828

2929
Task<bool> InvokeAgent(string agentId, List<RoleDialogModel> dialogs, InvokeAgentOptions? options = null);
3030
Task<bool> InvokeFunction(string name, RoleDialogModel messages, InvokeFunctionOptions? options = null);
@@ -40,5 +40,5 @@ public interface IRoutingService
4040

4141
Task<string> GetConversationContent(List<RoleDialogModel> dialogs, int maxDialogCount = 100);
4242

43-
(bool, string) HasMissingRequiredField(RoleDialogModel message, out string agentId);
43+
Task<(bool hasMissing, string reason, string agentId)> HasMissingRequiredField(RoleDialogModel message);
4444
}

src/Infrastructure/BotSharp.Core/Agents/Hooks/BasicAgentHook.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@ public BasicAgentHook(IServiceProvider services, AgentSettings settings)
1111
{
1212
}
1313

14-
public override Task OnAgentUtilityLoaded(Agent agent)
14+
public override async Task OnAgentUtilityLoaded(Agent agent)
1515
{
1616
var conv = _services.GetRequiredService<IConversationService>();
1717
var isConvMode = conv.IsConversationMode();
18-
if (!isConvMode) return Task.CompletedTask;
18+
if (!isConvMode) return;
1919

2020
agent.Utilities ??= [];
2121
agent.SecondaryFunctions ??= [];
2222
agent.SecondaryInstructions ??= [];
2323

24-
var (functions, templates) = GetUtilityContent(agent);
24+
var (functions, templates) = await GetUtilityContent(agent);
2525

2626
agent.SecondaryFunctions = agent.SecondaryFunctions.Concat(functions).DistinctBy(x => x.Name, StringComparer.OrdinalIgnoreCase).ToList();
2727
var contents = templates.Select(x => x.Content);
2828
agent.SecondaryInstructions = agent.SecondaryInstructions.Concat(contents).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
29-
30-
return Task.CompletedTask;
3129
}
32-
30+
3331

34-
private (IEnumerable<FunctionDef>, IEnumerable<AgentTemplate>) GetUtilityContent(Agent agent)
32+
private async Task<(IEnumerable<FunctionDef>, IEnumerable<AgentTemplate>)> GetUtilityContent(Agent agent)
3533
{
3634
var db = _services.GetRequiredService<IBotSharpRepository>();
3735
var (functionNames, templateNames) = FilterUtilityContent(agent.Utilities, agent);
@@ -42,14 +40,14 @@ public override Task OnAgentUtilityLoaded(Agent agent)
4240
var entryAgentId = routing.EntryAgentId;
4341
if (!string.IsNullOrEmpty(entryAgentId))
4442
{
45-
var entryAgent = db.GetAgent(entryAgentId, basicsOnly: true);
43+
var entryAgent = await db.GetAgent(entryAgentId, basicsOnly: true);
4644
var (fns, tps) = FilterUtilityContent(entryAgent?.Utilities, agent);
4745
functionNames = functionNames.Concat(fns).Distinct().ToList();
4846
templateNames = templateNames.Concat(tps).Distinct().ToList();
4947
}
5048
}
5149

52-
var ua = db.GetAgent(BuiltInAgentId.UtilityAssistant);
50+
var ua = await db.GetAgent(BuiltInAgentId.UtilityAssistant);
5351
var functions = ua?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
5452
var templates = ua?.Templates?.Where(x => templateNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? [];
5553
return (functions, templates);

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task<Agent> GetAgent(string id)
5050
return null;
5151
}
5252

53-
var profile = await _db.GetAgentAsync(id);
53+
var profile = await _db.GetAgent(id);
5454
if (profile == null)
5555
{
5656
_logger.LogError($"Can't find agent {id}");

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
1818
return;
1919
}
2020

21-
var record = await _db.GetAgentAsync(agent.Id);
21+
var record = await _db.GetAgent(agent.Id);
2222
if (record == null) return;
2323

2424
record.Name = agent.Name ?? string.Empty;
@@ -65,7 +65,7 @@ public async Task<string> PatchAgentTemplate(Agent agent)
6565
return patchResult;
6666
}
6767

68-
var record = await _db.GetAgentAsync(agent.Id);
68+
var record = await _db.GetAgent(agent.Id);
6969
if (record == null)
7070
{
7171
patchResult = $"Cannot find agent {agent.Id}";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public async Task<bool> SendMessage(string agentId,
6262
if (message.StopCompletion)
6363
{
6464
stopCompletion = true;
65-
routing.Context.Pop();
65+
await routing.Context.Pop();
6666
break;
6767
}
6868
}
@@ -83,12 +83,12 @@ public async Task<bool> SendMessage(string agentId,
8383
// Check the routing mode
8484
var states = _services.GetRequiredService<IConversationStateService>();
8585
var routingMode = states.GetState(StateConst.ROUTING_MODE, agent.Mode ?? RoutingMode.Eager);
86-
routing.Context.Push(agent.Id, reason: "request started", updateLazyRouting: false);
86+
await routing.Context.Push(agent.Id, reason: "request started", updateLazyRouting: false);
8787

8888
if (routingMode == RoutingMode.Lazy)
8989
{
9090
message.CurrentAgentId = states.GetState(StateConst.LAZY_ROUTING_AGENT_ID, message.CurrentAgentId);
91-
routing.Context.Push(message.CurrentAgentId, reason: "lazy routing", updateLazyRouting: false);
91+
await routing.Context.Push(message.CurrentAgentId, reason: "lazy routing", updateLazyRouting: false);
9292
}
9393

9494
response = await routing.InstructLoop(agent, message, dialogs);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public bool IsConversationMode()
230230

231231
if (string.IsNullOrEmpty(routingCtx.EntryAgentId)) return null;
232232

233-
var agent = await db.GetAgentAsync(routingCtx.EntryAgentId, basicsOnly: true);
233+
var agent = await db.GetAgent(routingCtx.EntryAgentId, basicsOnly: true);
234234
return agent?.MaxMessageCount;
235235
}
236236

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,14 @@ public async Task<List<string>> GetAgentResponses(string agentId, string prefix,
564564
return responses;
565565
}
566566

567-
public Agent? GetAgent(string agentId, bool basicsOnly = false)
567+
public async Task<Agent?> GetAgent(string agentId, bool basicsOnly = false)
568568
{
569569
var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir);
570570
var dir = Directory.EnumerateDirectories(agentDir).FirstOrDefault(x => x.Split(Path.DirectorySeparatorChar).Last() == agentId);
571571

572572
if (!string.IsNullOrEmpty(dir))
573573
{
574-
var json = File.ReadAllText(Path.Combine(dir, AGENT_FILE));
574+
var json = await File.ReadAllTextAsync(Path.Combine(dir, AGENT_FILE));
575575
if (string.IsNullOrEmpty(json))
576576
{
577577
return null;
@@ -593,23 +593,18 @@ public async Task<List<string>> GetAgentResponses(string agentId, string prefix,
593593
var samples = FetchSamples(dir);
594594
var templates = FetchTemplates(dir);
595595
var responses = FetchResponses(dir);
596-
return record.SetInstruction(defaultInstruction)
596+
var result = record.SetInstruction(defaultInstruction)
597597
.SetChannelInstructions(channelInstructions)
598598
.SetFunctions(functions)
599599
.SetTemplates(templates)
600600
.SetSamples(samples)
601601
.SetResponses(responses);
602+
return result;
602603
}
603604

604605
return null;
605606
}
606607

607-
public Task<Agent?> GetAgentAsync(string agentId, bool basicsOnly = false)
608-
{
609-
var agent = GetAgent(agentId, basicsOnly);
610-
return Task.FromResult(agent);
611-
}
612-
613608
public Task<List<Agent>> GetAgents(AgentFilter filter)
614609
{
615610
if (filter == null)

0 commit comments

Comments
 (0)