diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs index 4843f7105..e5c413cf1 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Chat/ChatCompletionProvider.cs @@ -390,7 +390,12 @@ public async Task GetChatCompletionsStreamingAsync(Agent agent, } else if (candidate.FinishReason.HasValue) { - var text = candidate.FinishMessage ?? candidate.FinishReason.Value.ToString(); + if (candidate.FinishReason == FinishReason.MAX_TOKENS) + { + _logger.LogWarning($"Action: {nameof(GetChatCompletionsStreamingAsync)}, Reason: {candidate.FinishReason}, Agent: {agent.Name}, MaxOutputTokens: {request.GenerationConfig?.MaxOutputTokens}, Content:{textStream.GetText()}"); + } + + var text = ProviderHelper.ResolveStreamedContent(candidate.FinishReason.Value, candidate.FinishMessage); responseMessage = new RoleDialogModel(AgentRole.Assistant, text) { CurrentAgentId = agent.Id, diff --git a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs index 91d38ae09..311889e4d 100644 --- a/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs +++ b/src/Plugins/BotSharp.Plugin.GoogleAI/Providers/ProviderHelper.cs @@ -1,7 +1,23 @@ +using GenerativeAI.Types; + namespace BotSharp.Plugin.GoogleAi.Providers; public static class ProviderHelper { + public const string MaxTokensMessage = "AI response exceeded max output length"; + + /// + /// Content saved when a streamed Gemini reply ends for a reason other than STOP. + /// MAX_TOKENS uses the same notice as the non-streaming path; every other reason + /// keeps FinishMessage, or the reason name when FinishMessage is absent. + /// + public static string ResolveStreamedContent(FinishReason reason, string? finishMessage) + { + return reason == FinishReason.MAX_TOKENS + ? MaxTokensMessage + : finishMessage ?? reason.ToString(); + } + public static GenerativeAI.GoogleAi GetGeminiClient(string provider, string model, IServiceProvider services) { var aiSettings = services.GetRequiredService(); diff --git a/tests/BotSharp.Core.UnitTests/Plugins/GoogleAiStreamedContentTests.cs b/tests/BotSharp.Core.UnitTests/Plugins/GoogleAiStreamedContentTests.cs new file mode 100644 index 000000000..7abab7aa9 --- /dev/null +++ b/tests/BotSharp.Core.UnitTests/Plugins/GoogleAiStreamedContentTests.cs @@ -0,0 +1,30 @@ +using BotSharp.Plugin.GoogleAi.Providers; +using GenerativeAI.Types; +using Xunit; + +namespace BotSharp.Core.UnitTests.Plugins; + +public class GoogleAiStreamedContentTests +{ + // Streaming used to save FinishMessage, or the reason name when it was absent. + // The non-streaming path shows a notice for the same MAX_TOKENS case. + [Theory] + [InlineData("partial answer")] + [InlineData("")] + [InlineData(null)] + public void ResolveStreamedContent_MaxTokens_ReturnsSameNoticeAsNonStreaming(string? finishMessage) + { + Assert.Equal("AI response exceeded max output length", + ProviderHelper.ResolveStreamedContent(FinishReason.MAX_TOKENS, finishMessage)); + } + + [Theory] + [InlineData("blocked by safety")] + [InlineData("")] + [InlineData(null)] + public void ResolveStreamedContent_OtherReason_KeepsFinishMessageOrReasonName(string? finishMessage) + { + var expected = finishMessage ?? FinishReason.SAFETY.ToString(); + Assert.Equal(expected, ProviderHelper.ResolveStreamedContent(FinishReason.SAFETY, finishMessage)); + } +}