Skip to content

Commit 42ad511

Browse files
author
Jicheng Lu
committed
add agent max message count
1 parent bf6daa0 commit 42ad511

File tree

13 files changed

+81
-12
lines changed

13 files changed

+81
-12
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public enum AgentField
1717
Response,
1818
Sample,
1919
LlmConfig,
20-
Utility
20+
Utility,
21+
MaxMessageCount
2122
}
2223

2324
public enum AgentTaskField

src/Infrastructure/BotSharp.Abstraction/Agents/Models/Agent.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public class Agent
104104
/// </summary>
105105
public string? InheritAgentId { get; set; }
106106

107+
/// <summary>
108+
/// Maximum message count when load conversation
109+
/// </summary>
110+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
111+
public int? MaxMessageCount { get; set; }
112+
107113
public List<RoutingRule> RoutingRules { get; set; } = new();
108114

109115
/// <summary>
@@ -133,6 +139,8 @@ public static Agent Clone(Agent agent)
133139
Knowledges = agent.Knowledges,
134140
IsPublic = agent.IsPublic,
135141
Disabled = agent.Disabled,
142+
MergeUtility = agent.MergeUtility,
143+
MaxMessageCount = agent.MaxMessageCount,
136144
Profiles = agent.Profiles,
137145
RoutingRules = agent.RoutingRules,
138146
LlmConfig = agent.LlmConfig,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
5353

5454
#region Agent
5555
void UpdateAgent(Agent agent, AgentField field);
56-
Agent? GetAgent(string agentId);
56+
Agent? GetAgent(string agentId, bool basicsOnly = false);
5757
List<Agent> GetAgents(AgentFilter filter);
5858
List<UserAgent> GetUserAgents(string userId);
5959
void BulkInsertAgents(List<Agent> agents);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public override void OnAgentUtilityLoaded(Agent agent)
4747
var entryAgentId = routing.EntryAgentId;
4848
if (!string.IsNullOrEmpty(entryAgentId))
4949
{
50-
var entryAgent = db.GetAgent(entryAgentId);
50+
var entryAgent = db.GetAgent(entryAgentId, basicsOnly: true);
5151
var (fns, tps) = GetUniqueContent(entryAgent?.Utilities);
5252
functionNames = functionNames.Concat(fns).Distinct().ToList();
5353
templateNames = templateNames.Concat(tps).Distinct().ToList();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
2828
record.IsPublic = agent.IsPublic;
2929
record.Disabled = agent.Disabled;
3030
record.MergeUtility = agent.MergeUtility;
31+
record.MaxMessageCount = agent.MaxMessageCount;
3132
record.Type = agent.Type;
3233
record.Profiles = agent.Profiles ?? [];
3334
record.RoutingRules = agent.RoutingRules ?? [];

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ public List<RoleDialogModel> GetDialogHistory(int lastCount = 100, bool fromBrea
153153
}
154154
}
155155

156-
return dialogs.TakeLast(lastCount).ToList();
156+
var agentMsgCount = GetAgentMessageCount();
157+
var count = agentMsgCount.HasValue && agentMsgCount.Value > 0 ? agentMsgCount.Value : lastCount;
158+
159+
return dialogs.TakeLast(count).ToList();
157160
}
158161

159162
public void SetConversationId(string conversationId, List<MessageState> states, bool isReadOnly = false)
@@ -192,4 +195,16 @@ public bool IsConversationMode()
192195
{
193196
return !string.IsNullOrWhiteSpace(_conversationId);
194197
}
198+
199+
200+
private int? GetAgentMessageCount()
201+
{
202+
var db = _services.GetRequiredService<IBotSharpRepository>();
203+
var routingCtx = _services.GetRequiredService<IRoutingContext>();
204+
205+
if (string.IsNullOrEmpty(routingCtx.EntryAgentId)) return null;
206+
207+
var agent = db.GetAgent(routingCtx.EntryAgentId, basicsOnly: true);
208+
return agent?.MaxMessageCount;
209+
}
195210
}

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
1616
#endregion
1717

1818
#region Agent
19-
public Agent GetAgent(string agentId)
19+
public Agent GetAgent(string agentId, bool basicsOnly = false)
2020
=> throw new NotImplementedException();
2121

2222
public List<Agent> GetAgents(AgentFilter filter)

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public void UpdateAgent(Agent agent, AgentField field)
5757
case AgentField.Utility:
5858
UpdateAgentUtilities(agent.Id, agent.MergeUtility, agent.Utilities);
5959
break;
60+
case AgentField.MaxMessageCount:
61+
UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount);
62+
break;
6063
case AgentField.All:
6164
UpdateAgentAllFields(agent);
6265
break;
@@ -283,6 +286,17 @@ private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config)
283286
File.WriteAllText(agentFile, json);
284287
}
285288

289+
private void UpdateAgentMaxMessageCount(string agentId, int? maxMessageCount)
290+
{
291+
var (agent, agentFile) = GetAgentFromFile(agentId);
292+
if (agent == null) return;
293+
294+
agent.MaxMessageCount = maxMessageCount;
295+
agent.UpdatedDateTime = DateTime.UtcNow;
296+
var json = JsonSerializer.Serialize(agent, _options);
297+
File.WriteAllText(agentFile, json);
298+
}
299+
286300
private void UpdateAgentAllFields(Agent inputAgent)
287301
{
288302
var (agent, agentFile) = GetAgentFromFile(inputAgent.Id);
@@ -298,6 +312,7 @@ private void UpdateAgentAllFields(Agent inputAgent)
298312
agent.Utilities = inputAgent.Utilities;
299313
agent.RoutingRules = inputAgent.RoutingRules;
300314
agent.LlmConfig = inputAgent.LlmConfig;
315+
agent.MaxMessageCount = inputAgent.MaxMessageCount;
301316
agent.UpdatedDateTime = DateTime.UtcNow;
302317
var json = JsonSerializer.Serialize(agent, _options);
303318
File.WriteAllText(agentFile, json);
@@ -329,7 +344,7 @@ public List<string> GetAgentResponses(string agentId, string prefix, string inte
329344
return responses;
330345
}
331346

332-
public Agent? GetAgent(string agentId)
347+
public Agent? GetAgent(string agentId, bool basicsOnly = false)
333348
{
334349
var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir);
335350
var dir = Directory.GetDirectories(agentDir).FirstOrDefault(x => x.Split(Path.DirectorySeparatorChar).Last() == agentId);
@@ -342,6 +357,8 @@ public List<string> GetAgentResponses(string agentId, string prefix, string inte
342357
var record = JsonSerializer.Deserialize<Agent>(json, _options);
343358
if (record == null) return null;
344359

360+
if (basicsOnly) return record;
361+
345362
var (defaultInstruction, channelInstructions) = FetchInstructions(dir);
346363
var functions = FetchFunctions(dir);
347364
var samples = FetchSamples(dir);

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentCreationModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class AgentCreationModel
5151

5252
public bool MergeUtility { get; set; }
5353

54+
public int? MaxMessageCount { get; set; }
55+
5456
public List<AgentUtility> Utilities { get; set; } = new();
5557
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
5658
public AgentLlmConfig? LlmConfig { get; set; }
@@ -72,6 +74,7 @@ public Agent ToAgent()
7274
Type = Type,
7375
Disabled = Disabled,
7476
MergeUtility = MergeUtility,
77+
MaxMessageCount = MaxMessageCount,
7578
Profiles = Profiles,
7679
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),
7780
LlmConfig = LlmConfig

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentUpdateModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public class AgentUpdateModel
5757

5858
public bool Disabled { get; set; }
5959

60+
[JsonPropertyName("max_message_count")]
61+
public int? MaxMessageCount { get; set; }
62+
6063
/// <summary>
6164
/// Profile by channel
6265
/// </summary>
@@ -77,6 +80,7 @@ public Agent ToAgent()
7780
IsPublic = IsPublic,
7881
Disabled = Disabled,
7982
MergeUtility = MergeUtility,
83+
MaxMessageCount = MaxMessageCount,
8084
Type = Type,
8185
Profiles = Profiles ?? new List<string>(),
8286
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),

0 commit comments

Comments
 (0)