diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Enums/MessageTypeName.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Enums/MessageTypeName.cs index 044526692..736ba8738 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Enums/MessageTypeName.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Enums/MessageTypeName.cs @@ -10,7 +10,7 @@ public static class MessageTypeName /// /// A message that belongs to the conversation record but not to the conversation as the user /// sees it -- what an agent said to itself on the way to an answer. Stored like any other - /// message and read back into the model's context; skipped when the dialog is rendered. + /// message and left out of later turns; skipped when the dialog is rendered. /// public const string Internal = "internal"; } diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs index 079d6749e..39be4646d 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs @@ -117,13 +117,14 @@ private async Task InvokeFunction( } else { - // Save to memory dialogs + // Save to memory dialogs and to storage var msg = RoleDialogModel.From(message, role: AgentRole.Function, content: message.Content); dialogs.Add(msg); Context.AddDialogs([msg]); + await Persist(msg); // Send to Next LLM var curAgentId = routing.Context.GetCurrentAgentId(); @@ -132,6 +133,12 @@ private async Task InvokeFunction( } else { + // The function wrote the reply itself. The call is still worth a record, but kept out + // of context, since the assistant message that follows carries the same text. + var record = RoleDialogModel.From(message, role: AgentRole.Function); + record.ExcludeFromContext = true; + await Persist(record); + var msg = RoleDialogModel.From(message, role: AgentRole.Assistant, content: message.Content); @@ -141,4 +148,16 @@ private async Task InvokeFunction( return true; } + + private async Task Persist(RoleDialogModel message) + { + var conv = _services.GetRequiredService(); + if (!conv.IsConversationMode()) + { + return; + } + + var storage = _services.GetRequiredService(); + await storage.Append(conv.ConversationId, message); + } }