Skip to content

Commit 52eb65f

Browse files
authored
Merge pull request #357 from hchen2020/master
Add breakpoint feature.
2 parents 6a87a03 + b9bfe0a commit 52eb65f

File tree

13 files changed

+97
-57
lines changed

13 files changed

+97
-57
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ Task<bool> SendMessage(string agentId,
3535
Func<RoleDialogModel, Task> onFunctionExecuting,
3636
Func<RoleDialogModel, Task> onFunctionExecuted);
3737

38-
List<RoleDialogModel> GetDialogHistory(int lastCount = 50);
38+
List<RoleDialogModel> GetDialogHistory(int lastCount = 50, bool fromBreakpoint = true);
3939
Task CleanHistory(string agentId);
40+
41+
/// <summary>
42+
/// Use this feature when you want to hide some context from LLM.
43+
/// </summary>
44+
/// <returns></returns>
45+
Task UpdateBreakpoint();
4046
}

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ public class Conversation
2626

2727
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;
2828
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
29+
30+
/// <summary>
31+
/// The default value will be same as CreatedTime
32+
/// It used to insert a breakpoint in the conversation to hide the previous dialogs.
33+
/// </summary>
34+
public DateTime Breakpoint { get; set; } = DateTime.UtcNow.AddMilliseconds(-100);
2935
}
3036

3137
public class DialogElement

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public interface IBotSharpRepository
5858
Conversation GetConversation(string conversationId);
5959
PagedItems<Conversation> GetConversations(ConversationFilter filter);
6060
void UpdateConversationTitle(string conversationId, string title);
61+
void UpdateConversationBreakpoint(string conversationId, DateTime breakpoint);
6162
List<Conversation> GetLastConversations();
6263
List<string> GetIdleConversations(int batchSize, int messageLimit, int bufferHours);
6364
bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false);

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

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

53
public partial class ConversationService : IConversationService
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace BotSharp.Core.Conversations.Services;
2+
3+
public partial class ConversationService : IConversationService
4+
{
5+
public async Task UpdateBreakpoint()
6+
{
7+
var db = _services.GetRequiredService<IBotSharpRepository>();
8+
db.UpdateConversationBreakpoint(_conversationId, DateTime.UtcNow);
9+
}
10+
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,22 @@ public Task CleanHistory(string agentId)
9999
throw new NotImplementedException();
100100
}
101101

102-
public List<RoleDialogModel> GetDialogHistory(int lastCount = 50)
102+
public List<RoleDialogModel> GetDialogHistory(int lastCount = 50, bool fromBreakpoint = true)
103103
{
104104
if (string.IsNullOrEmpty(_conversationId))
105105
{
106106
throw new ArgumentNullException("ConversationId is null.");
107107
}
108108

109109
var dialogs = _storage.GetDialogs(_conversationId);
110+
111+
if (fromBreakpoint)
112+
{
113+
var db = _services.GetRequiredService<IBotSharpRepository>();
114+
var conversation = db.GetConversation(_conversationId);
115+
dialogs = dialogs.Where(x => x.CreatedAt >= conversation.Breakpoint).ToList();
116+
}
117+
110118
return dialogs
111119
.TakeLast(lastCount)
112120
.ToList();

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

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -157,74 +157,51 @@ public bool DeleteAgentTasks()
157157

158158
#region Conversation
159159
public void CreateNewConversation(Conversation conversation)
160-
{
161-
throw new NotImplementedException();
162-
}
160+
=> throw new NotImplementedException();
163161

164162
public bool DeleteConversations(IEnumerable<string> conversationIds)
165-
{
166-
throw new NotImplementedException();
167-
}
163+
=> throw new NotImplementedException();
168164

169165
public Conversation GetConversation(string conversationId)
170-
{
171-
throw new NotImplementedException();
172-
}
166+
=> throw new NotImplementedException();
173167

174168
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
175-
{
176-
throw new NotImplementedException();
177-
}
169+
=> throw new NotImplementedException();
178170

179171
public List<Conversation> GetLastConversations()
180-
{
181-
throw new NotImplementedException();
182-
}
172+
=> throw new NotImplementedException();
183173

184174
public List<string> GetIdleConversations(int batchSize, int messageLimit, int bufferHours)
185-
{
186-
throw new NotImplementedException();
187-
}
175+
=> throw new NotImplementedException();
188176

189177
public List<DialogElement> GetConversationDialogs(string conversationId)
190-
{
191-
throw new NotImplementedException();
192-
}
178+
=> throw new NotImplementedException();
193179

194180
public void UpdateConversationDialogElements(string conversationId, List<DialogContentUpdateModel> updateElements)
195-
{
196-
throw new NotImplementedException();
197-
}
181+
=> new NotImplementedException();
198182

199183
public ConversationState GetConversationStates(string conversationId)
200-
{
201-
throw new NotImplementedException();
202-
}
184+
=> throw new NotImplementedException();
203185

204186
public void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs)
205-
{
206-
throw new NotImplementedException();
207-
}
187+
=> new NotImplementedException();
188+
208189
public void UpdateConversationTitle(string conversationId, string title)
209-
{
210-
throw new NotImplementedException();
211-
}
190+
=> new NotImplementedException();
191+
192+
public void UpdateConversationBreakpoint(string conversationId, DateTime breakpoint)
193+
=> new NotImplementedException();
194+
212195
public void UpdateConversationStates(string conversationId, List<StateKeyValue> states)
213-
{
214-
throw new NotImplementedException();
215-
}
196+
=> new NotImplementedException();
216197

217198
public void UpdateConversationStatus(string conversationId, string status)
218-
{
219-
throw new NotImplementedException();
220-
}
199+
=> new NotImplementedException();
221200

222201
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
223-
{
224-
throw new NotImplementedException();
225-
}
202+
=> throw new NotImplementedException();
226203
#endregion
227-
204+
228205
#region User
229206
public User? GetUserByEmail(string email)
230207
=> throw new NotImplementedException();

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,23 @@ public void UpdateConversationTitle(string conversationId, string title)
141141
}
142142
}
143143

144+
public void UpdateConversationBreakpoint(string conversationId, DateTime breakpoint)
145+
{
146+
var convDir = FindConversationDirectory(conversationId);
147+
if (!string.IsNullOrEmpty(convDir))
148+
{
149+
var convFile = Path.Combine(convDir, CONVERSATION_FILE);
150+
var content = File.ReadAllText(convFile);
151+
var record = JsonSerializer.Deserialize<Conversation>(content, _options);
152+
if (record != null)
153+
{
154+
record.UpdatedTime = DateTime.UtcNow;
155+
record.Breakpoint = breakpoint;
156+
File.WriteAllText(convFile, JsonSerializer.Serialize(record, _options));
157+
}
158+
}
159+
}
160+
144161
public ConversationState GetConversationStates(string conversationId)
145162
{
146163
var states = new List<StateKeyValue>();

src/Infrastructure/BotSharp.Core/Users/Services/UserService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ private string GenerateJwtToken(User user)
146146
new Claim(JwtRegisteredClaimNames.NameId, user.Id),
147147
new Claim(JwtRegisteredClaimNames.UniqueName, user.UserName),
148148
new Claim(JwtRegisteredClaimNames.Email, user.Email),
149-
new Claim(JwtRegisteredClaimNames.GivenName, user.FirstName),
150-
new Claim(JwtRegisteredClaimNames.FamilyName, user.LastName),
149+
new Claim(JwtRegisteredClaimNames.GivenName, user?.FirstName ?? string.Empty),
150+
new Claim(JwtRegisteredClaimNames.FamilyName, user?.LastName ?? string.Empty),
151151
new Claim("source", user.Source),
152-
new Claim("external_id", user.ExternalId??string.Empty),
152+
new Claim("external_id", user.ExternalId ?? string.Empty),
153153
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
154154
};
155155

src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/planner_prompt.naive.liquid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
What is the next step based on the CONVERSATION?
2-
Route to the appropriate agent last handled agent based on the context.
2+
Route to the last handling agent in priority.
33
{% if expected_next_action_agent != empty -%}
44
Expected next action agent is {{ expected_next_action_agent }}.
55
{%- endif %}

0 commit comments

Comments
 (0)