From ca262e7ec397689fbe867059ea84790ded704134 Mon Sep 17 00:00:00 2001
From: Haiping Chen <101423@smsassist.com>
Date: Sun, 7 Apr 2024 22:15:51 -0500
Subject: [PATCH] ConversationBreakpoint.Reason
---
.../Conversations/ConversationHookBase.cs | 3 +
.../Conversations/IConversationHook.cs | 7 ++
.../Conversations/IConversationService.cs | 3 +-
.../Models/ConversationBreakpoint.cs | 3 +
.../Functions/Models/FunctionCallFromLlm.cs | 1 -
.../Functions/Models/ParameterPropertyDef.cs | 3 +-
.../Infrastructures/Enums/StateConst.cs | 1 +
.../Repositories/IBotSharpRepository.cs | 4 +-
.../Routing/Models/RoutingArgs.cs | 6 ++
.../ConversationService.SendMessage.cs | 24 ++++---
.../ConversationService.UpdateBreakpoint.cs | 10 ++-
.../Services/ConversationService.cs | 9 ++-
.../Repository/BotSharpDbContext.cs | 4 +-
.../FileRepository.Conversation.cs | 15 +++--
.../Routing/Functions/RouteToAgentFn.cs | 3 +-
.../Handlers/ConversationEndRoutingHandler.cs | 49 --------------
.../Handlers/ResponseToUserRoutingHandler.cs | 3 +-
.../Handlers/RouteToAgentRoutingHandler.cs | 45 +++++++------
.../Handlers/TaskCompletedRoutingHandler.cs | 64 -------------------
.../Models/BreakpointMongoElement.cs | 1 +
.../MongoRepository.Conversation.cs | 34 +++++-----
21 files changed, 112 insertions(+), 180 deletions(-)
delete mode 100644 src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs
delete mode 100644 src/Infrastructure/BotSharp.Core/Routing/Handlers/TaskCompletedRoutingHandler.cs
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs
index 36f065fd..06a97174 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs
@@ -44,6 +44,9 @@ public abstract class ConversationHookBase : IConversationHook
public virtual Task OnConversationEnding(RoleDialogModel message)
=> Task.CompletedTask;
+ public virtual Task OnNewTaskDetected(RoleDialogModel message, string reason)
+ => Task.CompletedTask;
+
public virtual Task OnTaskCompleted(RoleDialogModel message)
=> Task.CompletedTask;
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs
index b9f0f3b8..9ed47c61 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs
@@ -64,6 +64,13 @@ public interface IConversationHook
Task OnResponseGenerated(RoleDialogModel message);
+ ///
+ /// LLM detected user requested a new task different from previous topic.
+ ///
+ ///
+ ///
+ Task OnNewTaskDetected(RoleDialogModel message, string reason);
+
///
/// LLM detected the current task is completed.
/// It's useful for the situation of multiple tasks in the same conversation.
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs
index effb8a94..d0a95ef8 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs
@@ -42,6 +42,7 @@ public interface IConversationService
/// Use this feature when you want to hide some context from LLM.
///
/// Whether to reset all states
+ /// Append user init words
///
- Task UpdateBreakpoint(bool resetStates = false);
+ Task UpdateBreakpoint(bool resetStates = false, string? reason = null);
}
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationBreakpoint.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationBreakpoint.cs
index 69a88d13..0d819353 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationBreakpoint.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/ConversationBreakpoint.cs
@@ -10,4 +10,7 @@ public class ConversationBreakpoint
[JsonPropertyName("created_time")]
public DateTime CreatedTime { get; set; } = DateTime.UtcNow;
+
+ [JsonPropertyName("reason")]
+ public string? Reason { get; set; }
}
diff --git a/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionCallFromLlm.cs b/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionCallFromLlm.cs
index fefade10..661a1437 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionCallFromLlm.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionCallFromLlm.cs
@@ -1,4 +1,3 @@
-using BotSharp.Abstraction.Routing.Models;
using System.Text.Json;
namespace BotSharp.Abstraction.Functions.Models;
diff --git a/src/Infrastructure/BotSharp.Abstraction/Functions/Models/ParameterPropertyDef.cs b/src/Infrastructure/BotSharp.Abstraction/Functions/Models/ParameterPropertyDef.cs
index fd927bbc..4f948aa1 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Functions/Models/ParameterPropertyDef.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Functions/Models/ParameterPropertyDef.cs
@@ -2,10 +2,11 @@ namespace BotSharp.Abstraction.Functions.Models;
public class ParameterPropertyDef : NameDesc
{
- public ParameterPropertyDef(string name, string description, string type = "string")
+ public ParameterPropertyDef(string name, string description, string type = "string", bool required = false)
: base(name, description)
{
Type = type;
+ Required = required;
}
[JsonPropertyName("required")]
diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Enums/StateConst.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Enums/StateConst.cs
index a4039bf7..8b7fc37c 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Enums/StateConst.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Enums/StateConst.cs
@@ -5,5 +5,6 @@ public class StateConst
public const string EXPECTED_ACTION_AGENT = "expected_next_action_agent";
public const string EXPECTED_GOAL_AGENT = "expected_user_goal_agent";
public const string NEXT_ACTION_AGENT = "next_action_agent";
+ public const string NEXT_ACTION_REASON = "next_action_reason";
public const string USER_GOAL_AGENT = "user_goal_agent";
}
diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs
index 64e92a66..e177b354 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs
@@ -58,8 +58,8 @@ public interface IBotSharpRepository
Conversation GetConversation(string conversationId);
PagedItems GetConversations(ConversationFilter filter);
void UpdateConversationTitle(string conversationId, string title);
- void UpdateConversationBreakpoint(string conversationId, string messageId, DateTime breakpoint);
- DateTime GetConversationBreakpoint(string conversationId);
+ void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);
+ ConversationBreakpoint? GetConversationBreakpoint(string conversationId);
List GetLastConversations();
List GetIdleConversations(int batchSize, int messageLimit, int bufferHours);
bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false);
diff --git a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs
index 82de1a81..31311b57 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingArgs.cs
@@ -19,6 +19,12 @@ public class RoutingArgs
[JsonPropertyName("conversation_end")]
public bool ConversationEnd { get; set; }
+ [JsonPropertyName("task_completed")]
+ public bool TaskCompleted { get; set; }
+
+ [JsonPropertyName("is_new_task")]
+ public bool IsNewTask { get; set; }
+
///
/// The content of replying to user
///
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs
index eaa24fa3..c4dd9302 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs
@@ -156,27 +156,31 @@ public partial class ConversationService
response.FunctionName = response.PostbackFunctionName;
}
- var hooks = _services.GetServices().ToList();
-
if (response.Instruction != null)
{
var conversation = _services.GetRequiredService();
var updatedConversation = await conversation.UpdateConversationTitle(_conversationId, response.Instruction.NextActionReason);
+ // Emit conversation task completed hook
+ if (response.Instruction.TaskCompleted)
+ {
+ await HookEmitter.Emit(_services, async hook =>
+ await hook.OnTaskCompleted(response)
+ );
+ }
+
// Emit conversation ending hook
if (response.Instruction.ConversationEnd)
{
- foreach (var hook in hooks)
- {
- await hook.OnConversationEnding(response);
- }
+ await HookEmitter.Emit(_services, async hook =>
+ await hook.OnConversationEnding(response)
+ );
}
}
- foreach (var hook in hooks)
- {
- await hook.OnResponseGenerated(response);
- }
+ await HookEmitter.Emit(_services, async hook =>
+ await hook.OnResponseGenerated(response)
+ );
await onResponseReceived(response);
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs
index ea10dac0..6a3397fc 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.UpdateBreakpoint.cs
@@ -2,12 +2,18 @@ namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService : IConversationService
{
- public async Task UpdateBreakpoint(bool resetStates = false)
+ public async Task UpdateBreakpoint(bool resetStates = false, string? reason = null)
{
var db = _services.GetRequiredService();
var routingCtx = _services.GetRequiredService();
var messageId = routingCtx.MessageId;
- db.UpdateConversationBreakpoint(_conversationId, messageId, DateTime.UtcNow);
+
+ db.UpdateConversationBreakpoint(_conversationId, new ConversationBreakpoint
+ {
+ MessageId = messageId,
+ Breakpoint = DateTime.UtcNow,
+ Reason = reason
+ });
// Reset states
if (resetStates)
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
index 188715d4..72b6cde0 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs
@@ -115,7 +115,14 @@ public partial class ConversationService : IConversationService
{
var db = _services.GetRequiredService();
var breakpoint = db.GetConversationBreakpoint(_conversationId);
- dialogs = dialogs.Where(x => x.CreatedAt >= breakpoint).ToList();
+ if (breakpoint != null)
+ {
+ dialogs = dialogs.Where(x => x.CreatedAt >= breakpoint.Breakpoint).ToList();
+ if (!string.IsNullOrEmpty(breakpoint.Reason))
+ {
+ dialogs.Insert(0, new RoleDialogModel(AgentRole.User, breakpoint.Reason));
+ }
+ }
}
return dialogs
diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
index 904048a8..d1361d02 100644
--- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
+++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
@@ -189,10 +189,10 @@ public class BotSharpDbContext : Database, IBotSharpRepository
public void UpdateConversationTitle(string conversationId, string title)
=> new NotImplementedException();
- public void UpdateConversationBreakpoint(string conversationId, string messageId, DateTime breakpoint)
+ public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint)
=> new NotImplementedException();
- public DateTime GetConversationBreakpoint(string conversationId)
+ public ConversationBreakpoint? GetConversationBreakpoint(string conversationId)
=> throw new NotImplementedException();
public void UpdateConversationStates(string conversationId, List states)
diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs
index bb477391..e5c193c1 100644
--- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs
+++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs
@@ -160,7 +160,7 @@ namespace BotSharp.Core.Repository
}
}
- public void UpdateConversationBreakpoint(string conversationId, string messageId, DateTime breakpoint)
+ public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint)
{
var convDir = FindConversationDirectory(conversationId);
if (!string.IsNullOrEmpty(convDir))
@@ -178,9 +178,10 @@ namespace BotSharp.Core.Repository
{
new ConversationBreakpoint
{
- MessageId = messageId,
- Breakpoint = breakpoint,
- CreatedTime = DateTime.UtcNow
+ MessageId = breakpoint.MessageId,
+ Breakpoint = breakpoint.Breakpoint,
+ CreatedTime = DateTime.UtcNow,
+ Reason = breakpoint.Reason,
}
};
@@ -197,12 +198,12 @@ namespace BotSharp.Core.Repository
}
}
- public DateTime GetConversationBreakpoint(string conversationId)
+ public ConversationBreakpoint? GetConversationBreakpoint(string conversationId)
{
var convDir = FindConversationDirectory(conversationId);
if (string.IsNullOrEmpty(convDir))
{
- return default;
+ return null;
}
var breakpointFile = Path.Combine(convDir, BREAKPOINT_FILE);
@@ -214,7 +215,7 @@ namespace BotSharp.Core.Repository
var content = File.ReadAllText(breakpointFile);
var records = JsonSerializer.Deserialize>(content, _options);
- return records?.LastOrDefault()?.Breakpoint ?? default;
+ return records?.LastOrDefault();
}
public ConversationState GetConversationStates(string conversationId)
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs
index 6eaaef3a..8221cfb5 100644
--- a/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs
+++ b/src/Infrastructure/BotSharp.Core/Routing/Functions/RouteToAgentFn.cs
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Functions;
+using BotSharp.Abstraction.Infrastructures.Enums;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Routing;
@@ -58,7 +59,7 @@ public partial class RouteToAgentFn : IFunctionCallback
if (!string.IsNullOrEmpty(args.AgentName) && args.AgentName.Length < 32)
{
_context.Push(args.AgentName, args.NextActionReason);
- states.SetState("next_action_agent", args.AgentName, isNeedVersion: true);
+ states.SetState(StateConst.NEXT_ACTION_AGENT, args.AgentName, isNeedVersion: true);
}
if (string.IsNullOrEmpty(args.AgentName))
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs
deleted file mode 100644
index bbd4fbb1..00000000
--- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ConversationEndRoutingHandler.cs
+++ /dev/null
@@ -1,49 +0,0 @@
-using BotSharp.Abstraction.Routing.Settings;
-
-namespace BotSharp.Core.Routing.Handlers;
-
-public class ConversationEndRoutingHandler : RoutingHandlerBase, IRoutingHandler
-{
- public string Name => "conversation_end";
-
- public string Description => "User completed his task and wants to end the conversation.";
-
- public List Parameters => new List
- {
- new ParameterPropertyDef("reason", "why end conversation"),
- new ParameterPropertyDef("response", "response content to user")
- };
-
- public List Planers => new List
- {
- };
-
- public ConversationEndRoutingHandler(IServiceProvider services, ILogger logger, RoutingSettings settings)
- : base(services, logger, settings)
- {
- }
-
- public async Task Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
- {
- var response = new RoleDialogModel(AgentRole.Assistant, inst.Response)
- {
- CurrentAgentId = message.CurrentAgentId,
- MessageId = message.MessageId,
- StopCompletion = true,
- FunctionName = inst.Function
- };
-
- _dialogs.Add(response);
-
- var hooks = _services.GetServices()
- .OrderBy(x => x.Priority)
- .ToList();
-
- foreach (var hook in hooks)
- {
- await hook.OnConversationEnding(response);
- }
-
- return true;
- }
-}
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs
index ad6eaf8d..2b010183 100644
--- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs
+++ b/src/Infrastructure/BotSharp.Core/Routing/Handlers/ResponseToUserRoutingHandler.cs
@@ -12,7 +12,8 @@ public class ResponseToUserRoutingHandler : RoutingHandlerBase, IRoutingHandler
{
new ParameterPropertyDef("reason", "why response to user directly without go to other agents"),
new ParameterPropertyDef("response", "response content to user in courteous words. If the user wants to end the conversation, you must set conversation_end to true and response politely."),
- new ParameterPropertyDef("conversation_end", "whether to end this conversation, true or false", type: "boolean")
+ new ParameterPropertyDef("conversation_end", "whether to end this conversation", type: "boolean"),
+ new ParameterPropertyDef("task_completed ", "whether the user's task request has been completed.", type: "boolean")
};
public ResponseToUserRoutingHandler(IServiceProvider services, ILogger logger, RoutingSettings settings)
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs
index cfbfe31e..b5c6a59e 100644
--- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs
+++ b/src/Infrastructure/BotSharp.Core/Routing/Handlers/RouteToAgentRoutingHandler.cs
@@ -11,26 +11,24 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
public List Parameters => new List
{
- new ParameterPropertyDef("next_action_reason", "the reason why route to this virtual agent")
- {
- Required = true
- },
- new ParameterPropertyDef("next_action_agent", "agent for next action based on user latest response, if user is replying last agent's question, you must route to this agent")
- {
- Required = true
- },
- new ParameterPropertyDef("user_goal_description", "user goal based on user initial task.")
- {
- Required = true
- },
- new ParameterPropertyDef("user_goal_agent", "agent who can acheive user initial task, must align with user_goal_description ")
- {
- Required = true
- },
- new ParameterPropertyDef("args", "useful parameters of next action agent, format: { }")
- {
- Type = "object"
- }
+ new ParameterPropertyDef("next_action_reason",
+ "the reason why route to this virtual agent",
+ required: true),
+ new ParameterPropertyDef("next_action_agent",
+ "agent for next action based on user latest response, if user is replying last agent's question, you must route to this agent",
+ required: true),
+ new ParameterPropertyDef("user_goal_description",
+ "user goal based on user initial task.",
+ required: true),
+ new ParameterPropertyDef("user_goal_agent",
+ "agent who can acheive user initial task, must align with user_goal_description.",
+ required: true),
+ new ParameterPropertyDef("args",
+ "useful parameters of next action agent, format: { }",
+ type: "object"),
+ new ParameterPropertyDef("is_new_task",
+ "whether the user is requesting a new task that is different from the previous topic.",
+ type: "boolean")
};
public RouteToAgentRoutingHandler(IServiceProvider services, ILogger logger, RoutingSettings settings)
@@ -51,6 +49,13 @@ public class RouteToAgentRoutingHandler : RoutingHandlerBase, IRoutingHandler
);
}
+ if (inst.IsNewTask)
+ {
+ await HookEmitter.Emit(_services, async hook =>
+ await hook.OnNewTaskDetected(message, inst.NextActionReason)
+ );
+ }
+
message.FunctionArgs = JsonSerializer.Serialize(inst);
var ret = await routing.InvokeFunction(message.FunctionName, message);
diff --git a/src/Infrastructure/BotSharp.Core/Routing/Handlers/TaskCompletedRoutingHandler.cs b/src/Infrastructure/BotSharp.Core/Routing/Handlers/TaskCompletedRoutingHandler.cs
deleted file mode 100644
index 21b7ea13..00000000
--- a/src/Infrastructure/BotSharp.Core/Routing/Handlers/TaskCompletedRoutingHandler.cs
+++ /dev/null
@@ -1,64 +0,0 @@
-using BotSharp.Abstraction.Routing.Settings;
-using BotSharp.Core.Routing.Planning;
-
-namespace BotSharp.Core.Routing.Handlers;
-
-public class TaskCompletedRoutingHandler : RoutingHandlerBase, IRoutingHandler
-{
- public string Name => "task_completed";
-
- public string Description => "User task is completed.";
-
- public List Parameters => new List
- {
- new ParameterPropertyDef("reason", "why the task is completed")
- {
- Required = true
- },
- new ParameterPropertyDef("response", "polite response when the task is completed")
- {
- Required = true
- },
- new ParameterPropertyDef("conversation_end", "whether to end this conversation, true or false")
- {
- Required = true,
- Type = "boolean"
- },
- new ParameterPropertyDef("abandoned_arguments", "the arguments next task can't reuse")
- };
-
- public List Planers => new List
- {
- nameof(HFPlanner)
- };
-
- public TaskCompletedRoutingHandler(IServiceProvider services, ILogger logger, RoutingSettings settings)
- : base(services, logger, settings)
- {
- }
-
- public async Task Handle(IRoutingService routing, FunctionCallFromLlm inst, RoleDialogModel message)
- {
- var response = new RoleDialogModel(AgentRole.Assistant, inst.Response)
- {
- CurrentAgentId = message.CurrentAgentId,
- MessageId = message.MessageId,
- StopCompletion = true,
- FunctionName = inst.Function,
- Instruction = inst,
- };
-
- _dialogs.Add(response);
-
- var hooks = _services.GetServices()
- .OrderBy(x => x.Priority)
- .ToList();
-
- foreach (var hook in hooks)
- {
- await hook.OnTaskCompleted(response);
- }
-
- return true;
- }
-}
diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/BreakpointMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/BreakpointMongoElement.cs
index 96c1fac2..db17d353 100644
--- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/BreakpointMongoElement.cs
+++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/BreakpointMongoElement.cs
@@ -5,4 +5,5 @@ public class BreakpointMongoElement
public string? MessageId { get; set; }
public DateTime Breakpoint { get; set; }
public DateTime CreatedTime { get; set; }
+ public string? Reason { get; set; }
}
diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs
index 7d9eaea7..4c5af6bc 100644
--- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs
+++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs
@@ -44,21 +44,12 @@ public partial class MongoRepository
}
}).ToList();
- var initialBreakpoints = new List()
- {
- new BreakpointMongoElement
- {
- Breakpoint = utcNow.AddMilliseconds(-100),
- CreatedTime = utcNow
- }
- };
-
var stateDoc = new ConversationStateDocument
{
Id = Guid.NewGuid().ToString(),
ConversationId = convDoc.Id,
States = initialStates,
- Breakpoints = initialBreakpoints
+ Breakpoints = new List()
};
_dc.Conversations.InsertOne(convDoc);
@@ -152,15 +143,16 @@ public partial class MongoRepository
_dc.Conversations.UpdateOne(filterConv, updateConv);
}
- public void UpdateConversationBreakpoint(string conversationId, string messageId, DateTime breakpoint)
+ public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint)
{
if (string.IsNullOrEmpty(conversationId)) return;
var newBreakpoint = new BreakpointMongoElement()
{
- MessageId = messageId,
- Breakpoint = breakpoint,
- CreatedTime = DateTime.UtcNow
+ MessageId = breakpoint.MessageId,
+ Breakpoint = breakpoint.Breakpoint,
+ CreatedTime = DateTime.UtcNow,
+ Reason = breakpoint.Reason
};
var filterState = Builders.Filter.Eq(x => x.ConversationId, conversationId);
var updateState = Builders.Update.Push(x => x.Breakpoints, newBreakpoint);
@@ -168,11 +160,11 @@ public partial class MongoRepository
_dc.ConversationStates.UpdateOne(filterState, updateState);
}
- public DateTime GetConversationBreakpoint(string conversationId)
+ public ConversationBreakpoint? GetConversationBreakpoint(string conversationId)
{
if (string.IsNullOrEmpty(conversationId))
{
- return default;
+ return null;
}
var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId);
@@ -180,10 +172,16 @@ public partial class MongoRepository
if (state == null || state.Breakpoints.IsNullOrEmpty())
{
- return default;
+ return null;
}
- return state.Breakpoints.LastOrDefault()?.Breakpoint ?? default;
+ return state.Breakpoints.Select(x => new ConversationBreakpoint
+ {
+ Breakpoint = x.Breakpoint,
+ CreatedTime = x.CreatedTime,
+ MessageId = x.MessageId,
+ Reason = x.Reason,
+ }).LastOrDefault();
}
public ConversationState GetConversationStates(string conversationId)