Skip to content

Commit dd34657

Browse files
author
hchen2020
committed
Add Execute Once.
1 parent b97db1b commit dd34657

23 files changed

Lines changed: 180 additions & 323 deletions

‎Directory.Build.props‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<LangVersion>10.0</LangVersion>
44
<OutputPath>..\..\..\packages</OutputPath>
5-
<BotSharpVersion>0.14.6</BotSharpVersion>
5+
<BotSharpVersion>0.14.7</BotSharpVersion>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
</PropertyGroup>
88
</Project>

‎src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Agents.Enums;
2-
31
namespace BotSharp.Abstraction.Conversations.Models;
42

53
public class RoleDialogModel

‎src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionCallFromLlm.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class FunctionCallFromLlm
1515
public string? Question { get; set; }
1616

1717
[JsonPropertyName("answer")]
18-
public string? Answer { get; set; }
18+
public string Answer { get; set; } = string.Empty;
1919

2020
[JsonPropertyName("args")]
2121
public JsonDocument Arguments { get; set; } = JsonDocument.Parse("{}");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BotSharp.Abstraction.Models;
2+
3+
public class NameDesc
4+
{
5+
public string Name { get; set; }
6+
public string Description { get; set; }
7+
8+
public NameDesc(string name, string description)
9+
{
10+
Name = name;
11+
Description = description;
12+
}
13+
}

‎src/Infrastructure/BotSharp.Abstraction/Routing/IRoutingHandler.cs‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ public interface IRoutingHandler
66
{
77
string Name { get; }
88
string Description { get; }
9-
bool IsReasoning { get; }
10-
bool RequireAgent { get; }
11-
List<string> Parameters { get; }
12-
void SetRouter(Agent router);
13-
void SetDialogs(List<RoleDialogModel> dialogs);
14-
Task<FunctionCallFromLlm> GetNextInstructionFromReasoner(string prompt);
15-
Task<RoleDialogModel> GetResponseFromReasoner();
16-
Task<RoleDialogModel> Handle(FunctionCallFromLlm inst);
9+
bool IsReasoning { get => false; }
10+
bool Enabled { get => true; }
11+
List<string> Parameters { get => new List<string>(); }
12+
13+
void SetRouter(Agent router) { }
14+
15+
void SetDialogs(List<RoleDialogModel> dialogs) { }
16+
17+
Task<FunctionCallFromLlm> GetNextInstructionFromReasoner(string prompt)
18+
=> throw new NotImplementedException("");
19+
20+
Task<RoleDialogModel> Handle(FunctionCallFromLlm inst)
21+
=> throw new NotImplementedException("");
1722
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ public interface IRoutingService
44
{
55
Agent LoadRouter();
66
List<RoleDialogModel> Dialogs { get; }
7-
Task<RoleDialogModel> Enter(Agent agent, List<RoleDialogModel> whileDialogs);
7+
void SetDialogs(List<RoleDialogModel> dialogs);
8+
Task<RoleDialogModel> InstructLoop(Agent router);
9+
Task<RoleDialogModel> ExecuteOnce(Agent agent);
810
}

‎src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ namespace BotSharp.Abstraction.Routing.Models;
22

33
public class RoutingArgs
44
{
5-
[JsonPropertyName("user_goal")]
6-
public string UserGoal { get; set; } = string.Empty;
7-
85
[JsonPropertyName("reason")]
96
public string Reason { get; set; } = string.Empty;
107

‎src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
6767
services.AddScoped<IRoutingHandler, RetrieveDataFromAgentRoutingHandler>();
6868
services.AddScoped<IRoutingHandler, TaskEndRoutingHandler>();
6969
services.AddScoped<IRoutingHandler, ConversationEndRoutingHandler>();
70-
services.AddScoped<IRoutingHandler, TransferToCsrRoutingHandler>();
7170

7271
if (myDatabaseSettings.Default == "FileRepository")
7372
{

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

Lines changed: 0 additions & 159 deletions
This file was deleted.

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

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,18 @@ public async Task<bool> SendMessage(string agentId,
5151
}
5252

5353
// Routing with reasoning
54+
var routing = _services.GetRequiredService<IRoutingService>();
5455
var settings = _services.GetRequiredService<RoutingSettings>();
55-
if (settings.RouterId == agent.Id)
56-
{
57-
var routing = _services.GetRequiredService<IRoutingService>();
58-
var reasonedContext = await routing.Enter(agent, wholeDialogs);
59-
60-
if (reasonedContext.StopCompletion)
61-
{
62-
await HandleAssistantMessage(reasonedContext, onMessageReceived);
63-
return true;
64-
}
6556

66-
// Switch agent
67-
if (reasonedContext.CurrentAgentId != agent.Id)
68-
{
69-
agent = await agentService.LoadAgent(reasonedContext.CurrentAgentId);
70-
}
57+
routing.SetDialogs(wholeDialogs);
7158

72-
routing.Dialogs.ForEach(x =>
73-
{
74-
wholeDialogs.Add(x);
75-
if (x.Content != null)
76-
{
77-
_storage.Append(_conversationId, x);
78-
}
79-
});
80-
}
59+
var response = settings.RouterId == agent.Id ?
60+
await routing.InstructLoop(agent) :
61+
await routing.ExecuteOnce(agent);
8162

82-
var result = await GetChatCompletionsAsyncRecursively(agent,
83-
wholeDialogs,
84-
onMessageReceived,
85-
onFunctionExecuting,
86-
onFunctionExecuted);
63+
await HandleAssistantMessage(response, onMessageReceived);
8764

88-
return result;
65+
return true;
8966
}
9067

9168
private async Task<Conversation> GetConversationRecord(string agentId)
@@ -106,19 +83,23 @@ private async Task<Conversation> GetConversationRecord(string agentId)
10683
return converation;
10784
}
10885

109-
private void SaveStateByArgs(string args)
86+
private async Task HandleAssistantMessage(RoleDialogModel message, Func<RoleDialogModel, Task> onMessageReceived)
11087
{
111-
var stateService = _services.GetRequiredService<IConversationStateService>();
112-
var jo = JsonSerializer.Deserialize<object>(args);
113-
if (jo is JsonElement root)
88+
var hooks = _services.GetServices<IConversationHook>().ToList();
89+
90+
// After chat completion hook
91+
foreach (var hook in hooks)
11492
{
115-
foreach (JsonProperty property in root.EnumerateObject())
116-
{
117-
if (!string.IsNullOrEmpty(property.Value.ToString()))
118-
{
119-
stateService.SetState(property.Name, property.Value);
120-
}
121-
}
93+
await hook.AfterCompletion(message);
12294
}
95+
96+
var agent = await _services.GetRequiredService<IAgentService>().GetAgent(message.CurrentAgentId);
97+
98+
_logger.LogInformation($"[{agent?.Name ?? "Router"}] {message.Role}: {message.Content}");
99+
100+
await onMessageReceived(message);
101+
102+
// Add to dialog history
103+
_storage.Append(_conversationId, message);
123104
}
124105
}

0 commit comments

Comments
 (0)