Skip to content

Commit 3e3760f

Browse files
authored
Merge pull request #399 from hchen2020/master
ConversationBreakpoint.Reason
2 parents 4dece4b + ca262e7 commit 3e3760f

21 files changed

Lines changed: 112 additions & 180 deletions

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public virtual Task OnDialogsLoaded(List<RoleDialogModel> dialogs)
4444
public virtual Task OnConversationEnding(RoleDialogModel message)
4545
=> Task.CompletedTask;
4646

47+
public virtual Task OnNewTaskDetected(RoleDialogModel message, string reason)
48+
=> Task.CompletedTask;
49+
4750
public virtual Task OnTaskCompleted(RoleDialogModel message)
4851
=> Task.CompletedTask;
4952

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ public interface IConversationHook
6464

6565
Task OnResponseGenerated(RoleDialogModel message);
6666

67+
/// <summary>
68+
/// LLM detected user requested a new task different from previous topic.
69+
/// </summary>
70+
/// <param name="message"></param>
71+
/// <returns></returns>
72+
Task OnNewTaskDetected(RoleDialogModel message, string reason);
73+
6774
/// <summary>
6875
/// LLM detected the current task is completed.
6976
/// It's useful for the situation of multiple tasks in the same conversation.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Task<bool> SendMessage(string agentId,
4242
/// Use this feature when you want to hide some context from LLM.
4343
/// </summary>
4444
/// <param name="resetStates">Whether to reset all states</param>
45+
/// <param name="reason">Append user init words</param>
4546
/// <returns></returns>
46-
Task UpdateBreakpoint(bool resetStates = false);
47+
Task UpdateBreakpoint(bool resetStates = false, string? reason = null);
4748
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ public class ConversationBreakpoint
1010

1111
[JsonPropertyName("created_time")]
1212
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
13+
14+
[JsonPropertyName("reason")]
15+
public string? Reason { get; set; }
1316
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Abstraction.Routing.Models;
21
using System.Text.Json;
32

43
namespace BotSharp.Abstraction.Functions.Models;

src/Infrastructure/BotSharp.Abstraction/Functions/Models/ParameterPropertyDef.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ namespace BotSharp.Abstraction.Functions.Models;
22

33
public class ParameterPropertyDef : NameDesc
44
{
5-
public ParameterPropertyDef(string name, string description, string type = "string")
5+
public ParameterPropertyDef(string name, string description, string type = "string", bool required = false)
66
: base(name, description)
77
{
88
Type = type;
9+
Required = required;
910
}
1011

1112
[JsonPropertyName("required")]

src/Infrastructure/BotSharp.Abstraction/Infrastructures/Enums/StateConst.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public class StateConst
55
public const string EXPECTED_ACTION_AGENT = "expected_next_action_agent";
66
public const string EXPECTED_GOAL_AGENT = "expected_user_goal_agent";
77
public const string NEXT_ACTION_AGENT = "next_action_agent";
8+
public const string NEXT_ACTION_REASON = "next_action_reason";
89
public const string USER_GOAL_AGENT = "user_goal_agent";
910
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ 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, string messageId, DateTime breakpoint);
62-
DateTime GetConversationBreakpoint(string conversationId);
61+
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);
62+
ConversationBreakpoint? GetConversationBreakpoint(string conversationId);
6363
List<Conversation> GetLastConversations();
6464
List<string> GetIdleConversations(int batchSize, int messageLimit, int bufferHours);
6565
bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ public class RoutingArgs
1919
[JsonPropertyName("conversation_end")]
2020
public bool ConversationEnd { get; set; }
2121

22+
[JsonPropertyName("task_completed")]
23+
public bool TaskCompleted { get; set; }
24+
25+
[JsonPropertyName("is_new_task")]
26+
public bool IsNewTask { get; set; }
27+
2228
/// <summary>
2329
/// The content of replying to user
2430
/// </summary>

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,31 @@ response.RichContent is RichContent<IRichMessage> template &&
156156
response.FunctionName = response.PostbackFunctionName;
157157
}
158158

159-
var hooks = _services.GetServices<IConversationHook>().ToList();
160-
161159
if (response.Instruction != null)
162160
{
163161
var conversation = _services.GetRequiredService<IConversationService>();
164162
var updatedConversation = await conversation.UpdateConversationTitle(_conversationId, response.Instruction.NextActionReason);
165163

164+
// Emit conversation task completed hook
165+
if (response.Instruction.TaskCompleted)
166+
{
167+
await HookEmitter.Emit<IConversationHook>(_services, async hook =>
168+
await hook.OnTaskCompleted(response)
169+
);
170+
}
171+
166172
// Emit conversation ending hook
167173
if (response.Instruction.ConversationEnd)
168174
{
169-
foreach (var hook in hooks)
170-
{
171-
await hook.OnConversationEnding(response);
172-
}
175+
await HookEmitter.Emit<IConversationHook>(_services, async hook =>
176+
await hook.OnConversationEnding(response)
177+
);
173178
}
174179
}
175180

176-
foreach (var hook in hooks)
177-
{
178-
await hook.OnResponseGenerated(response);
179-
}
181+
await HookEmitter.Emit<IConversationHook>(_services, async hook =>
182+
await hook.OnResponseGenerated(response)
183+
);
180184

181185
await onResponseReceived(response);
182186

0 commit comments

Comments
 (0)