From 564a0705bf08235eef583a95d4b80be55ec965ca Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 13:11:30 +0800 Subject: [PATCH 01/30] refactor(llm): shared LlmToolLoop + ILlmTurnSource; migrate AnthropicService (3 loops -> 1) --- .../Model/AI/Llm/LlmTurnModels.cs | 62 ++ .../Interface/AI/LLM/ILlmTurnSource.cs | 45 ++ .../Service/AI/LLM/AnthropicService.cs | 651 ++++++------------ .../Service/AI/LLM/LlmToolLoop.cs | 163 +++++ 4 files changed, 465 insertions(+), 456 deletions(-) create mode 100644 TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs create mode 100644 TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs diff --git a/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs b/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs new file mode 100644 index 00000000..45a8fb44 --- /dev/null +++ b/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; + +namespace TelegramSearchBot.Model.AI { + /// + /// Normalized native tool call surfaced by a transport (native tool-calling protocol). + /// Arguments are raw JSON; each transport parses/normalizes them as needed. + /// + public sealed class LlmNativeToolCall { + public string Id { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public string ArgumentsJson { get; set; } = "{}"; + + public Dictionary ParseArguments() { + if (string.IsNullOrWhiteSpace(ArgumentsJson)) { + return new Dictionary(); + } + try { + return JsonSerializer.Deserialize>(ArgumentsJson) + ?.ToDictionary(kv => kv.Key, kv => kv.Value.ToString()) + ?? new Dictionary(); + } catch (Exception) { + return new Dictionary(); + } + } + } + + /// Result of executing one tool call, keyed by native call id. + public sealed class LlmToolResult { + public string ToolCallId { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public string Result { get; set; } = string.Empty; + public bool IsError { get; set; } + } + + /// Outcome of one assistant turn (one LLM request), after streaming completed. + public sealed class LlmTurnResult { + /// Full turn text (not trimmed). + public string Text { get; set; } = string.Empty; + /// Reasoning/thinking content, when the provider streams it. + public string Reasoning { get; set; } = string.Empty; + /// Native tool calls, empty for text-protocol sources and pure-text turns. + public List ToolCalls { get; set; } = new(); + /// + /// Native protocol: tool call metadata was malformed and the source already appended a + /// self-correction message to its history; the loop must continue without executing tools. + /// + public bool MalformedToolCall { get; set; } + /// Provider-specific usage observation for prompt-caching telemetry (opaque). + public object? UsageObservation { get; set; } + /// True when at least one delta arrived (text or thinking) during the turn. + public bool StreamedAny { get; set; } + } + + /// Events streamed by a turn source. Always terminates with TurnCompleted. + public abstract record LlmStreamEvent { + public sealed record TextDelta(string Delta) : LlmStreamEvent; + public sealed record ThinkingDelta(string Delta) : LlmStreamEvent; + public sealed record TurnCompleted(LlmTurnResult Turn) : LlmStreamEvent; + } +} diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs new file mode 100644 index 00000000..957a40b2 --- /dev/null +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using TelegramSearchBot.Model.AI; + +namespace TelegramSearchBot.Interface.AI.LLM { + /// + /// A per-execution turn source: owns the provider-typed history for one agent run and + /// streams one assistant turn per call. The shared + /// drives the tool-call cycle; transports only translate normalized turns to/from the + /// provider API. One instance is created per ExecAsync/ResumeFromSnapshot run. + /// + public interface ILlmTurnSource { + /// True when the provider supports native tool-calling for this model/protocol. + bool SupportsNativeTools { get; } + + /// + /// Stream one assistant turn. + /// Text-protocol sources: is the user message for this turn + /// (initial input on the first turn, tool feedback afterwards); no tools are attached. + /// Native sources: is the initial input on the first turn + /// only; after the next call passes null. + /// Yields text/thinking deltas, then exactly one . + /// + IAsyncEnumerable StreamTurnAsync(string? userContent, CancellationToken cancellationToken); + + /// + /// Append the assistant turn to the source's history. Called once per turn after + /// . Text-protocol sources append the assistant + /// message (if non-empty) and update tracked history; native sources append only the + /// plain-text final assistant message (tool-call turns are committed by + /// ). + /// + void CommitAssistantTurn(string text, string reasoning, bool isFinal); + + /// + /// Native mode only: append the assistant tool-call blocks plus the executed results + /// to the source's history so the next turn sees them. + /// + void CommitToolResults(IReadOnlyList results); + + /// Serialized history for continuation snapshots. + IReadOnlyList GetTrackedHistory(); + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs index e8d20f78..d2ec7544 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs @@ -707,222 +707,24 @@ private async IAsyncEnumerable ExecWithNativeToolCallingAsync( using var client = CreateClient(channel, binding); var anthropicTools = ConvertToAnthropicTools(nativeTools, promptCachingEnabled); - int maxToolCycles = Env.MaxToolCycles; - var currentMessageContentBuilder = new StringBuilder(); - var trackedHistory = new List(); - trackedHistory.Add(new SerializedChatMessage { Role = "system", Content = systemPrompt }); - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - var parameters = new MessageCreateParams { - Model = modelName, - MaxTokens = 8192, - System = BuildSystemPrompt(systemPrompt, promptCachingEnabled), - Messages = providerHistory, - Tools = anthropicTools, - }; - - var contentBuilder = new StringBuilder(); - var toolUseBlocks = new List<(string id, string name, string inputJson)>(); - var currentToolInputBuilder = new StringBuilder(); - string currentToolId = null; - string currentToolName = null; - bool hasToolUse = false; - long? cacheCreationInputTokens = null; - long? cacheReadInputTokens = null; - object usageObservation = null; - - await foreach (var rawEvent in client.Messages.CreateStreaming(parameters, cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - if (rawEvent.TryPickStart(out var messageStartEvent) && messageStartEvent.Message?.Usage != null) { - usageObservation = new { - messageStartEvent.Message.Usage.InputTokens, - messageStartEvent.Message.Usage.OutputTokens, - messageStartEvent.Message.Usage.CacheCreationInputTokens, - messageStartEvent.Message.Usage.CacheReadInputTokens, - RawData = messageStartEvent.Message.Usage.RawData, - }; - } - - if (rawEvent.TryPickDelta(out var messageDeltaEvent) && messageDeltaEvent.Usage != null) { - cacheCreationInputTokens = messageDeltaEvent.Usage.CacheCreationInputTokens ?? cacheCreationInputTokens; - cacheReadInputTokens = messageDeltaEvent.Usage.CacheReadInputTokens ?? cacheReadInputTokens; - usageObservation = new { - messageDeltaEvent.Usage.InputTokens, - messageDeltaEvent.Usage.OutputTokens, - messageDeltaEvent.Usage.CacheCreationInputTokens, - messageDeltaEvent.Usage.CacheReadInputTokens, - RawData = messageDeltaEvent.Usage.RawData, - }; - } - - if (rawEvent.TryPickContentBlockStart(out var startEvent)) { - if (startEvent.ContentBlock.TryPickToolUse(out var toolUseStart)) { - currentToolId = toolUseStart.ID; - currentToolName = toolUseStart.Name; - currentToolInputBuilder.Clear(); - hasToolUse = true; - } - } else if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { - if (deltaEvent.Delta.TryPickText(out var textDelta)) { - contentBuilder.Append(textDelta.Text); - currentMessageContentBuilder.Append(textDelta.Text); - if (currentMessageContentBuilder.Length > 10) { - yield return currentMessageContentBuilder.ToString(); - } - } else if (deltaEvent.Delta.TryPickInputJson(out var inputJsonDelta)) { - currentToolInputBuilder.Append(inputJsonDelta.PartialJson); - } - } else if (rawEvent.TryPickContentBlockStop(out _)) { - if (currentToolId != null) { - toolUseBlocks.Add((currentToolId, currentToolName, currentToolInputBuilder.ToString())); - currentToolId = null; - currentToolName = null; - } - } - } - - LogPromptCachingObservation( - channel, - "Anthropic", - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - cacheBreakpointInserted, - cacheCreationInputTokens, - cacheReadInputTokens, - usageObservation); - - string responseText = contentBuilder.ToString().Trim(); - - if (hasToolUse && toolUseBlocks.Any()) { - // Build assistant message with text + tool use blocks - var assistantContentBlocks = new List(); - if (!string.IsNullOrWhiteSpace(responseText)) { - assistantContentBlocks.Add(new TextBlockParam(responseText)); - } - foreach (var (id, name, inputJson) in toolUseBlocks) { - Dictionary parsedInput; - try { - parsedInput = string.IsNullOrWhiteSpace(inputJson) - ? new Dictionary() - : System.Text.Json.JsonSerializer.Deserialize>(inputJson); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Failed to deserialize Anthropic native tool input for assistant history. ToolUseId={ToolUseId}, ToolName={ToolName}, InputJson={InputJson}, ErrorSummary={ErrorSummary}", - ServiceName, - id, - name, - inputJson, - ex.GetLogSummary()); - parsedInput = new Dictionary(); - } - assistantContentBlocks.Add(new ToolUseBlockParam { - ID = id, - Name = name, - Input = parsedInput - }); - } - - providerHistory.Add(new MessageParam { - Role = Role.Assistant, - Content = assistantContentBlocks - }); - - trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = responseText }); - - var toolNamesBuilder = new StringBuilder(); - foreach (var (id, name, inputJson) in toolUseBlocks) { - Dictionary argsDict = null; - if (!string.IsNullOrWhiteSpace(inputJson)) { - try { - argsDict = System.Text.Json.JsonSerializer.Deserialize>(inputJson) - .ToDictionary(kv => kv.Key, kv => kv.Value.ToString()); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Failed to deserialize Anthropic native tool input for display. ToolUseId={ToolUseId}, ToolName={ToolName}, InputJson={InputJson}, ErrorSummary={ErrorSummary}", - ServiceName, - id, - name, - inputJson, - ex.GetLogSummary()); - } - } - argsDict ??= new Dictionary(); - toolNamesBuilder.Append(McpToolHelper.FormatToolCallDisplay(name, argsDict)); - } - currentMessageContentBuilder.Append(toolNamesBuilder.ToString()); - yield return currentMessageContentBuilder.ToString(); - - // Execute tools and build tool result message - var toolResultBlocks = new List(); - foreach (var (id, name, inputJson) in toolUseBlocks) { - _logger.LogInformation("{ServiceName}: Native tool call: {ToolName} with arguments: {Arguments}", ServiceName, name, inputJson); - - string toolResultString; - bool isError = false; - try { - var argsDict = string.IsNullOrWhiteSpace(inputJson) - ? new Dictionary() - : System.Text.Json.JsonSerializer.Deserialize>(inputJson) - .ToDictionary(kv => kv.Key, kv => kv.Value.ToString()); - - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(name, argsDict, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - _logger.LogInformation("{ServiceName}: Tool {ToolName} executed. Result length: {Length}", ServiceName, name, toolResultString.Length); - } catch (Exception ex) { - isError = true; - _logger.LogError( - ex, - "{ServiceName}: Error executing Anthropic native tool {ToolName}. ToolUseId={ToolUseId}, InputJson={InputJson}, ErrorSummary={ErrorSummary}", - ServiceName, - name, - id, - inputJson, - ex.GetLogSummary()); - toolResultString = $"Error executing tool {name}: {ex.GetLogSummary()}"; - } - - toolResultBlocks.Add(new ToolResultBlockParam(id) { - Content = toolResultString, - IsError = isError, - }); - } - - providerHistory.Add(new MessageParam { - Role = Role.User, - Content = toolResultBlocks - }); - } else { - // Regular text response, no tool calls - if (!string.IsNullOrWhiteSpace(responseText)) { - trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = responseText }); - } - yield break; - } - } - - _logger.LogWarning("{ServiceName}: Max tool call cycles reached for chat {ChatId}. User confirmation needed.", ServiceName, ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Anthropic", - ChannelId = channel.Id, - LastAccumulatedContent = currentMessageContentBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = trackedHistory, - }; + var trackedHistory = new List { + new SerializedChatMessage { Role = "system", Content = systemPrompt } + }; + var source = new AnthropicTurnSource( + this, client, systemPrompt, providerHistory, trackedHistory, anthropicTools, + promptCachingEnabled, toolDefinitionHash, stablePrefixHash, cacheBreakpointInserted, + modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "Anthropic", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; } } @@ -952,154 +754,24 @@ private async IAsyncEnumerable ExecWithXmlToolCallingAsync( using var client = CreateClient(channel, binding); - int maxToolCycles = Env.MaxToolCycles; - var currentMessageContentBuilder = new StringBuilder(); - var trackedHistory = new List(); - trackedHistory.Add(new SerializedChatMessage { Role = "system", Content = systemPrompt }); - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - var parameters = new MessageCreateParams { - Model = modelName, - MaxTokens = 8192, - System = BuildSystemPrompt(systemPrompt, promptCachingEnabled), - Messages = providerHistory, - }; - - var llmResponseBuilder = new StringBuilder(); - long? cacheCreationInputTokens = null; - long? cacheReadInputTokens = null; - object usageObservation = null; - - await foreach (var rawEvent in client.Messages.CreateStreaming(parameters, cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - if (rawEvent.TryPickStart(out var messageStartEvent) && messageStartEvent.Message?.Usage != null) { - usageObservation = new { - messageStartEvent.Message.Usage.InputTokens, - messageStartEvent.Message.Usage.OutputTokens, - messageStartEvent.Message.Usage.CacheCreationInputTokens, - messageStartEvent.Message.Usage.CacheReadInputTokens, - RawData = messageStartEvent.Message.Usage.RawData, - }; - } - - if (rawEvent.TryPickDelta(out var messageDeltaEvent) && messageDeltaEvent.Usage != null) { - cacheCreationInputTokens = messageDeltaEvent.Usage.CacheCreationInputTokens ?? cacheCreationInputTokens; - cacheReadInputTokens = messageDeltaEvent.Usage.CacheReadInputTokens ?? cacheReadInputTokens; - usageObservation = new { - messageDeltaEvent.Usage.InputTokens, - messageDeltaEvent.Usage.OutputTokens, - messageDeltaEvent.Usage.CacheCreationInputTokens, - messageDeltaEvent.Usage.CacheReadInputTokens, - RawData = messageDeltaEvent.Usage.RawData, - }; - } - - if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { - if (deltaEvent.Delta.TryPickText(out var textDelta)) { - currentMessageContentBuilder.Append(textDelta.Text); - llmResponseBuilder.Append(textDelta.Text); - if (currentMessageContentBuilder.Length > 10) { - yield return currentMessageContentBuilder.ToString(); - } - } - } - } - - LogPromptCachingObservation( - channel, - "Anthropic", - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - cacheBreakpointInserted, - cacheCreationInputTokens, - cacheReadInputTokens, - usageObservation); - - string llmFullResponseText = llmResponseBuilder.ToString().Trim(); - _logger.LogDebug("{ServiceName} raw full response (Cycle {Cycle}): {Response}", ServiceName, cycle + 1, llmFullResponseText); - - trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = llmFullResponseText }); - - if (!string.IsNullOrWhiteSpace(llmFullResponseText)) { - providerHistory.Add(new MessageParam { - Role = Role.Assistant, - Content = llmFullResponseText - }); - } else if (cycle < maxToolCycles - 1) { - _logger.LogWarning("{ServiceName}: LLM returned empty response during tool cycle {Cycle}.", ServiceName, cycle + 1); - } - - // XML tool parsing - if (McpToolHelper.TryParseToolCalls(llmFullResponseText, out var parsedToolCalls) && parsedToolCalls.Any()) { - var firstToolCall = parsedToolCalls[0]; - string parsedToolName = firstToolCall.toolName; - Dictionary toolArguments = firstToolCall.arguments; - - _logger.LogInformation("{ServiceName}: LLM requested tool: {ToolName} with arguments: {Arguments}", ServiceName, parsedToolName, JsonConvert.SerializeObject(toolArguments)); - if (parsedToolCalls.Count > 1) { - _logger.LogWarning("{ServiceName}: LLM returned multiple tool calls ({Count}). Only the first one ('{FirstToolName}') will be executed.", ServiceName, parsedToolCalls.Count, parsedToolName); - } - - currentMessageContentBuilder.Append(McpToolHelper.FormatToolCallDisplay(parsedToolName, toolArguments)); - yield return currentMessageContentBuilder.ToString(); - - string toolResultString; - bool isError = false; - try { - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(parsedToolName, toolArguments, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - _logger.LogInformation("{ServiceName}: Tool {ToolName} executed. Result: {Result}", ServiceName, parsedToolName, toolResultString); - } catch (Exception ex) { - isError = true; - _logger.LogError( - ex, - "{ServiceName}: Error executing Anthropic XML tool {ToolName}. Arguments={Arguments}, ErrorSummary={ErrorSummary}", - ServiceName, - parsedToolName, - JsonConvert.SerializeObject(toolArguments), - ex.GetLogSummary()); - toolResultString = $"Error executing tool {parsedToolName}: {ex.GetLogSummary()}."; - } - - string feedbackPrefix = isError ? $"[Tool '{parsedToolName}' Execution Failed. Error: " : $"[Executed Tool '{parsedToolName}'. Result: "; - string feedback = $"{feedbackPrefix}{toolResultString}]"; - - trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = feedback }); - - providerHistory.Add(new MessageParam { - Role = Role.User, - Content = feedback - }); - _logger.LogInformation("Added user feedback to history for LLM: {Feedback}", feedback); - } else { - if (string.IsNullOrWhiteSpace(llmFullResponseText)) { - _logger.LogWarning("{ServiceName}: LLM returned empty final non-tool response for ChatId {ChatId}.", ServiceName, ChatId); - } - yield break; - } - } - - _logger.LogWarning("{ServiceName}: Max tool call cycles reached for chat {ChatId}. User confirmation needed.", ServiceName, ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Anthropic", - ChannelId = channel.Id, - LastAccumulatedContent = currentMessageContentBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = trackedHistory, - }; + var trackedHistory = new List { + new SerializedChatMessage { Role = "system", Content = systemPrompt } + }; + var source = new AnthropicTurnSource( + this, client, systemPrompt, providerHistory, trackedHistory, null, + promptCachingEnabled, toolDefinitionHash, stablePrefixHash, cacheBreakpointInserted, + modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "Anthropic", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; } } @@ -1140,6 +812,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( ServiceName, snapshot.SnapshotId, snapshot.ChatId, snapshot.ProviderHistory?.Count ?? 0); var (systemPrompt, providerHistory) = DeserializeProviderHistory(snapshot.ProviderHistory); + var trackedHistory = snapshot.ProviderHistory ?? new List(); var promptCachingEnabled = await IsPromptCachingEnabledAsync(); var (toolDefinitionHash, stablePrefixHash) = BuildPromptCachingContext( "anthropic-resume", @@ -1149,28 +822,94 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( using var client = CreateClient(channel, binding); - var fullContentBuilder = new StringBuilder(snapshot.LastAccumulatedContent ?? ""); - var newContentBuilder = new StringBuilder(); + var source = new AnthropicTurnSource( + this, client, systemPrompt ?? string.Empty, providerHistory, trackedHistory, null, + promptCachingEnabled, toolDefinitionHash, stablePrefixHash, cacheBreakpointInserted, + modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = snapshot.ChatId, + OriginalMessageId = snapshot.OriginalMessageId, + UserId = snapshot.UserId, + ModelName = modelName, + Provider = "Anthropic", + ChannelId = channel.Id, + BaseCycles = snapshot.CyclesSoFar, + InitialContent = snapshot.LastAccumulatedContent ?? string.Empty + }; + var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; + } + } - int maxToolCycles = Env.MaxToolCycles; - var trackedHistory = snapshot.ProviderHistory ?? new List(); + #endregion - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + /// + /// Per-run turn source for the Anthropic Messages API. Streams one turn per call and + /// owns the provider-typed history plus the serialized history used for snapshots. + /// Supports both native tool-calling (tools attached) and the XML text protocol. + /// + private sealed class AnthropicTurnSource : ILlmTurnSource { + private readonly AnthropicService _svc; + private readonly AnthropicClient _client; + private readonly string _systemPrompt; + private readonly List _providerHistory; + private readonly List _trackedHistory; + private readonly List? _tools; + private readonly bool _promptCachingEnabled; + private readonly string _toolDefinitionHash; + private readonly string _stablePrefixHash; + private readonly bool _cacheBreakpointInserted; + private readonly string _modelName; + private readonly LLMChannel _channel; + + private List<(string id, string name, string inputJson)> _pendingToolCalls = new(); + private string _pendingTurnText = string.Empty; + + public AnthropicTurnSource( + AnthropicService svc, AnthropicClient client, string systemPrompt, + List providerHistory, List trackedHistory, + List? tools, bool promptCachingEnabled, + string toolDefinitionHash, string stablePrefixHash, bool cacheBreakpointInserted, + string modelName, LLMChannel channel) { + _svc = svc; + _client = client; + _systemPrompt = systemPrompt; + _providerHistory = providerHistory; + _trackedHistory = trackedHistory; + _tools = tools; + _promptCachingEnabled = promptCachingEnabled; + _toolDefinitionHash = toolDefinitionHash; + _stablePrefixHash = stablePrefixHash; + _cacheBreakpointInserted = cacheBreakpointInserted; + _modelName = modelName; + _channel = channel; + } + public bool SupportsNativeTools => _tools != null && _tools.Count > 0; + + public async IAsyncEnumerable StreamTurnAsync( + string? userContent, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { var parameters = new MessageCreateParams { - Model = modelName, + Model = _modelName, MaxTokens = 8192, - System = BuildSystemPrompt(systemPrompt ?? string.Empty, promptCachingEnabled), - Messages = providerHistory, + System = BuildSystemPrompt(_systemPrompt, _promptCachingEnabled), + Messages = _providerHistory, + Tools = SupportsNativeTools ? _tools : null, }; - var llmResponseBuilder = new StringBuilder(); + + var turnText = new StringBuilder(); + var toolUseBlocks = new List<(string id, string name, string inputJson)>(); + var currentToolInputBuilder = new StringBuilder(); + string currentToolId = null; + string currentToolName = null; long? cacheCreationInputTokens = null; long? cacheReadInputTokens = null; object usageObservation = null; - await foreach (var rawEvent in client.Messages.CreateStreaming(parameters, cancellationToken)) { + await foreach (var rawEvent in _client.Messages.CreateStreaming(parameters, cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); if (rawEvent.TryPickStart(out var messageStartEvent) && messageStartEvent.Message?.Usage != null) { @@ -1195,104 +934,104 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( }; } - if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { + if (rawEvent.TryPickContentBlockStart(out var startEvent)) { + if (startEvent.ContentBlock.TryPickToolUse(out var toolUseStart)) { + currentToolId = toolUseStart.ID; + currentToolName = toolUseStart.Name; + currentToolInputBuilder.Clear(); + } + } else if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { if (deltaEvent.Delta.TryPickText(out var textDelta)) { - fullContentBuilder.Append(textDelta.Text); - newContentBuilder.Append(textDelta.Text); - llmResponseBuilder.Append(textDelta.Text); - var newContent = newContentBuilder.ToString(); - if (newContent.Length > 10) { - yield return newContent; - } + turnText.Append(textDelta.Text); + yield return new LlmStreamEvent.TextDelta(textDelta.Text); + } else if (deltaEvent.Delta.TryPickInputJson(out var inputJsonDelta)) { + currentToolInputBuilder.Append(inputJsonDelta.PartialJson); + } + } else if (rawEvent.TryPickContentBlockStop(out _)) { + if (currentToolId != null) { + toolUseBlocks.Add((currentToolId, currentToolName, currentToolInputBuilder.ToString())); + currentToolId = null; + currentToolName = null; } } } - LogPromptCachingObservation( - channel, - "Anthropic", - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - cacheBreakpointInserted, - cacheCreationInputTokens, - cacheReadInputTokens, - usageObservation); - - string llmFullResponseText = llmResponseBuilder.ToString().Trim(); - _logger.LogDebug("{ServiceName} raw full response (Resume Cycle {Cycle}): {Response}", ServiceName, cycle + 1, llmFullResponseText); - - if (!string.IsNullOrWhiteSpace(llmFullResponseText)) { - providerHistory.Add(new MessageParam { - Role = Role.Assistant, - Content = llmFullResponseText - }); - trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = llmFullResponseText }); - } - - if (McpToolHelper.TryParseToolCalls(llmFullResponseText, out var parsedToolCalls) && parsedToolCalls.Any()) { - var firstToolCall = parsedToolCalls[0]; - string parsedToolName = firstToolCall.toolName; - - _logger.LogInformation("{ServiceName}: LLM requested tool (resume): {ToolName}", ServiceName, parsedToolName); + _svc.LogPromptCachingObservation( + _channel, "Anthropic", _modelName, _promptCachingEnabled, + _toolDefinitionHash, _stablePrefixHash, _cacheBreakpointInserted, + cacheCreationInputTokens, cacheReadInputTokens, usageObservation); + + var responseText = turnText.ToString().Trim(); + _pendingTurnText = responseText; + _pendingToolCalls = toolUseBlocks; + + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = responseText, + ToolCalls = toolUseBlocks.Select(b => new LlmNativeToolCall { + Id = b.id, + Name = b.name, + ArgumentsJson = string.IsNullOrWhiteSpace(b.inputJson) ? "{}" : b.inputJson + }).ToList(), + StreamedAny = turnText.Length > 0, + UsageObservation = usageObservation + }); + } - var toolIndicator = McpToolHelper.FormatToolCallDisplay(parsedToolName, firstToolCall.arguments); - newContentBuilder.Append(toolIndicator); - fullContentBuilder.Append(toolIndicator); - yield return newContentBuilder.ToString(); + public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { + if (SupportsNativeTools) { + // Native final-text turn: provider history is not extended (matches legacy behavior). + if (!string.IsNullOrWhiteSpace(text)) { + _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); + } + return; + } + if (!string.IsNullOrWhiteSpace(text)) { + _providerHistory.Add(new MessageParam { Role = Role.Assistant, Content = text }); + } + _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); + } - string toolResultString; - bool isError = false; + public void CommitToolResults(IReadOnlyList results) { + // Assistant message with text + tool_use blocks, then a user message with the results. + var assistantContentBlocks = new List(); + if (!string.IsNullOrWhiteSpace(_pendingTurnText)) { + assistantContentBlocks.Add(new TextBlockParam(_pendingTurnText)); + } + foreach (var (id, name, inputJson) in _pendingToolCalls) { + Dictionary parsedInput; try { - var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(parsedToolName, firstToolCall.arguments, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); + parsedInput = string.IsNullOrWhiteSpace(inputJson) + ? new Dictionary() + : System.Text.Json.JsonSerializer.Deserialize>(inputJson); } catch (Exception ex) { - isError = true; - _logger.LogError( + _svc._logger.LogError( ex, - "{ServiceName}: Error executing Anthropic XML tool {ToolName} (resume). Arguments={Arguments}, SnapshotId={SnapshotId}, ErrorSummary={ErrorSummary}", - ServiceName, - parsedToolName, - JsonConvert.SerializeObject(firstToolCall.arguments), - snapshot.SnapshotId, - ex.GetLogSummary()); - toolResultString = $"Error executing tool {parsedToolName}: {ex.GetLogSummary()}."; + "{ServiceName}: Failed to deserialize Anthropic native tool input for assistant history. ToolUseId={ToolUseId}, ToolName={ToolName}, InputJson={InputJson}, ErrorSummary={ErrorSummary}", + _svc.ServiceName, id, name, inputJson, ex.GetLogSummary()); + parsedInput = new Dictionary(); } - - string feedbackPrefix = isError ? $"[Tool '{parsedToolName}' Execution Failed. Error: " : $"[Executed Tool '{parsedToolName}'. Result: "; - string feedback = $"{feedbackPrefix}{toolResultString}]"; - - providerHistory.Add(new MessageParam { - Role = Role.User, - Content = feedback + assistantContentBlocks.Add(new ToolUseBlockParam { + ID = id, + Name = name, + Input = parsedInput + }); + } + _providerHistory.Add(new MessageParam { Role = Role.Assistant, Content = assistantContentBlocks }); + _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = _pendingTurnText }); + + var toolResultBlocks = new List(); + foreach (var result in results) { + toolResultBlocks.Add(new ToolResultBlockParam(result.ToolCallId) { + Content = result.Result, + IsError = result.IsError, }); - trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = feedback }); - } else { - yield break; } + _providerHistory.Add(new MessageParam { Role = Role.User, Content = toolResultBlocks }); } - _logger.LogWarning("{ServiceName}: Max tool call cycles reached again during resume for ChatId {ChatId}.", ServiceName, snapshot.ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = snapshot.ChatId, - OriginalMessageId = snapshot.OriginalMessageId, - UserId = snapshot.UserId, - ModelName = modelName, - Provider = "Anthropic", - ChannelId = channel.Id, - LastAccumulatedContent = fullContentBuilder.ToString(), - CyclesSoFar = snapshot.CyclesSoFar + maxToolCycles, - ProviderHistory = trackedHistory, - }; - } + public IReadOnlyList GetTrackedHistory() => _trackedHistory; } - #endregion #region Serialization diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs new file mode 100644 index 00000000..75e69717 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs @@ -0,0 +1,163 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Tools; + +namespace TelegramSearchBot.Service.AI.LLM { + /// Identity data captured into iteration-limit continuation snapshots. + public sealed class LlmToolLoopMeta { + public long ChatId { get; set; } + public long OriginalMessageId { get; set; } + public long UserId { get; set; } + public string ModelName { get; set; } = string.Empty; + public string Provider { get; set; } = string.Empty; + public int ChannelId { get; set; } + /// Base cycle count when resuming (added on top of this run's cycles). + public int BaseCycles { get; set; } + /// Pre-existing accumulated content when resuming (included in snapshots; only + /// deltas beyond it are yielded). + public string InitialContent { get; set; } = string.Empty; + } + + /// + /// The single shared agent tool-call loop. Drives turns, + /// executes tools via , yields accumulated-content snapshots, + /// and builds the iteration-limit continuation snapshot. Replaces the per-provider + /// copies of this cycle. + /// + public static class LlmToolLoop { + public static async IAsyncEnumerable RunAsync( + ILlmTurnSource source, + string? initialUserContent, + ToolContext toolContext, + LlmToolLoopMeta meta, + LlmExecutionContext? executionContext, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + var maxToolCycles = Env.MaxToolCycles; + var contentBuilder = new StringBuilder(meta.InitialContent ?? string.Empty); + var baseline = contentBuilder.Length; + + string NewContent() => contentBuilder.ToString(baseline, contentBuilder.Length - baseline); + + var userContent = initialUserContent; + + for (int cycle = 0; cycle < maxToolCycles; cycle++) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + + LlmTurnResult? turn = null; + + await foreach (var evt in source.StreamTurnAsync(userContent, cancellationToken).WithCancellation(cancellationToken)) { + switch (evt) { + case LlmStreamEvent.TextDelta textDelta: + contentBuilder.Append(textDelta.Delta); + if (contentBuilder.Length - baseline > 10) { + yield return NewContent(); + } + break; + case LlmStreamEvent.ThinkingDelta: + // Reasoning is captured inside the turn result; not streamed to Telegram. + break; + case LlmStreamEvent.TurnCompleted completed: + turn = completed.Turn; + break; + } + } + + turn ??= new LlmTurnResult(); + var turnText = (turn.Text ?? string.Empty).Trim(); + + if (source.SupportsNativeTools) { + if (turn.ToolCalls.Count > 0) { + var results = new List(); + var indicators = new StringBuilder(); + foreach (var call in turn.ToolCalls) { + var argsDict = call.ParseArguments(); + var (resultString, isError) = await ExecuteToolAsync(call.Name, argsDict, toolContext); + results.Add(new LlmToolResult { + ToolCallId = call.Id, + Name = call.Name, + Result = resultString, + IsError = isError + }); + indicators.Append(McpToolHelper.FormatToolCallDisplay(call.Name, argsDict)); + } + contentBuilder.Append(indicators.ToString()); + yield return NewContent(); + source.CommitToolResults(results); + userContent = null; + continue; + } + + if (turn.MalformedToolCall) { + // The source appended a self-correction message to its own history. + userContent = null; + continue; + } + + source.CommitAssistantTurn(turnText, turn.Reasoning, turn.StreamedAny); + yield break; + } + + // Text-embedded tool protocol. + source.CommitAssistantTurn(turnText, turn.Reasoning, turn.StreamedAny); + + if (McpToolHelper.TryParseToolCalls(turnText, out var parsedToolCalls) && parsedToolCalls.Count > 0) { + var first = parsedToolCalls[0]; + if (parsedToolCalls.Count > 1) { + LogMultipleToolCalls(first.toolName, parsedToolCalls.Count); + } + var (resultString, isError) = await ExecuteToolAsync(first.toolName, first.arguments, toolContext); + contentBuilder.Append(McpToolHelper.FormatToolCallDisplay(first.toolName, first.arguments)); + yield return NewContent(); + userContent = isError + ? $"[Tool '{first.toolName}' Execution Failed. Error: {resultString}]" + : $"[Executed Tool '{first.toolName}'. Result: {resultString}]"; + continue; + } + + yield break; + } + + // Iteration limit reached — persist continuation snapshot for user confirmation. + if (executionContext != null) { + executionContext.IterationLimitReached = true; + executionContext.SnapshotData = new LlmContinuationSnapshot { + SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, + ChatId = meta.ChatId, + OriginalMessageId = meta.OriginalMessageId, + UserId = meta.UserId, + ModelName = meta.ModelName, + Provider = meta.Provider, + ChannelId = meta.ChannelId, + LastAccumulatedContent = contentBuilder.ToString(), + CyclesSoFar = meta.BaseCycles + maxToolCycles, + ProviderHistory = new List(source.GetTrackedHistory() ?? []), + }; + } + } + + private static void LogMultipleToolCalls(string toolName, int count) { + Serilog.Log.Warning( + "LLM returned multiple tool calls ({Count}). Only the first one ('{FirstToolName}') will be executed.", + count, toolName); + } + + private static async Task<(string Result, bool IsError)> ExecuteToolAsync( + string toolName, Dictionary argsDict, ToolContext toolContext) { + try { + object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(toolName, argsDict, toolContext); + var result = McpToolHelper.ConvertToolResultToString(toolResultObject); + return (result, false); + } catch (Exception ex) { + Serilog.Log.Error(ex, "Error executing tool {ToolName}", toolName); + return ($"Error executing tool {toolName}: {ex.GetLogSummary()}", true); + } + } + } +} From 766972af612571a650b5fa4e443d89fbca842a60 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 13:13:34 +0800 Subject: [PATCH 02/30] refactor(llm): migrate OpenAIService (3 loops -> shared LlmToolLoop) --- .../Service/AI/LLM/OpenAIService.cs | 682 ++++++------------ 1 file changed, 223 insertions(+), 459 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs index 2c7efabd..04e914fa 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs @@ -1249,248 +1249,21 @@ private async IAsyncEnumerable ExecWithNativeToolCallingAsync( string.Join(",", nativeTools.Select(t => t.FunctionName).Take(80))); var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); - try { - int maxToolCycles = Env.MaxToolCycles; - var currentMessageContentBuilder = new StringBuilder(); - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - var contentBuilder = new StringBuilder(); - var reasoningContentBuilder = new StringBuilder(); - var toolCallAccumulators = new Dictionary(); - ChatFinishReason? finishReason = null; - ChatTokenUsage latestUsage = null; - - _logger.LogDebug( - "{ServiceName}: Native cycle {Cycle} request. HistoryCount={HistoryCount}, ToolCount={ToolCount}", - ServiceName, - cycle + 1, - providerHistory.Count, - completionOptions.Tools.Count); - - // --- Call LLM with tools --- - await foreach (var update in chatClient.CompleteChatStreamingAsync(providerHistory, completionOptions, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - // Accumulate text content - foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { - if (updatePart?.Text != null) { - contentBuilder.Append(updatePart.Text); - currentMessageContentBuilder.Append(updatePart.Text); - if (currentMessageContentBuilder.ToString().Length > 10) { - yield return currentMessageContentBuilder.ToString(); - } - } - } - - // Accumulate reasoning content for thinking mode models (e.g., Kimi-thinking-preview) - var reasoningUpdate = GetStreamingReasoningContent(update); - if (!string.IsNullOrEmpty(reasoningUpdate)) { - reasoningContentBuilder.Append(reasoningUpdate); - } - - // Accumulate tool call updates - foreach (var toolCallUpdate in update.ToolCallUpdates ?? Enumerable.Empty()) { - int index = toolCallUpdate.Index; - if (!toolCallAccumulators.ContainsKey(index)) { - toolCallAccumulators[index] = new ToolCallAccumulator(); - } - - var acc = toolCallAccumulators[index]; - if (toolCallUpdate.ToolCallId != null) acc.Id ??= toolCallUpdate.ToolCallId; - if (toolCallUpdate.FunctionName != null) acc.Name ??= toolCallUpdate.FunctionName; - if (toolCallUpdate.FunctionArgumentsUpdate != null) { - acc.Arguments.Append(toolCallUpdate.FunctionArgumentsUpdate); - } - } - - if (update.FinishReason.HasValue) { - finishReason = update.FinishReason.Value; - } - - if (update.Usage != null) { - latestUsage = update.Usage; - } - } - - if (shouldObservePromptCaching) { - LogOpenAiPromptCachingObservation( - "OpenAI", - channel, - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - promptCacheKey, - latestUsage, - cacheKeyAttached); - } - - string responseText = contentBuilder.ToString().Trim(); - string reasoningContent = reasoningContentBuilder.ToString().Trim(); - _logger.LogInformation( - "{ServiceName}: Native cycle {Cycle} completed. FinishReason={FinishReason}, TextLength={TextLength}, ReasoningLength={ReasoningLength}, ToolCallUpdateCount={ToolCallUpdateCount}, ToolCallMetadata={ToolCallMetadata}, TextPreview={TextPreview}", - ServiceName, - cycle + 1, - finishReason?.ToString() ?? "null", - responseText.Length, - reasoningContent.Length, - toolCallAccumulators.Count, - JsonConvert.SerializeObject(toolCallAccumulators.ToDictionary( - kvp => kvp.Key, - kvp => new { - kvp.Value.Id, - kvp.Value.Name, - ArgumentsPreview = SanitizeAndTruncateArguments(kvp.Value.Arguments.ToString()) - })), - SanitizeAndTruncateArguments(responseText, 1024)); - - // Check if this is a tool call response - if (finishReason == ChatFinishReason.ToolCalls && toolCallAccumulators.Any()) { - List chatToolCalls = null; - try { - // Build the assistant message with tool calls - chatToolCalls = new List(); - foreach (var (index, acc) in toolCallAccumulators) { - if (string.IsNullOrWhiteSpace(acc.Id)) { - _logger.LogWarning("{ServiceName}: Tool call at index {Index} has no ID, generating fallback.", ServiceName, index); - } - var toolCallId = NormalizeToolCallId(acc.Id); - var toolName = NormalizeToolCallName(acc.Name); - var argumentsJson = NormalizeToolCallArguments(acc.Arguments.ToString()); - chatToolCalls.Add(ChatToolCall.CreateFunctionToolCall( - toolCallId, - toolName, - BinaryData.FromString(argumentsJson))); - } - - var assistantMessage = new AssistantChatMessage(chatToolCalls); - if (!string.IsNullOrWhiteSpace(responseText)) { - assistantMessage = new AssistantChatMessage(chatToolCalls) { Content = { ChatMessageContentPart.CreateTextPart(responseText) } }; - } - SetAssistantReasoningContent(assistantMessage, reasoningContent, includeEmptyReasoningContent); - providerHistory.Add(assistantMessage); - - var toolIndicators = new StringBuilder(); - foreach (var toolCall in chatToolCalls) { - var argsDict = DeserializeToolArgumentsForDisplay(toolCall.FunctionArguments?.ToString()); - toolIndicators.Append(McpToolHelper.FormatToolCallDisplay(toolCall.FunctionName, argsDict)); - } - currentMessageContentBuilder.Append(toolIndicators.ToString()); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Error building native tool calls, returning error to LLM for self-correction. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}, ToolCallMetadata={ToolCallMetadata}, ErrorSummary={ErrorSummary}", - ServiceName, - finishReason, - toolCallAccumulators.Count, - JsonConvert.SerializeObject(toolCallAccumulators.ToDictionary( - kvp => kvp.Key, - kvp => new { - kvp.Value.Id, - kvp.Value.Name, - ArgumentsPreview = SanitizeAndTruncateArguments(kvp.Value.Arguments.ToString()) - })), - ex.GetLogSummary()); - const string errorMsg = "Tool call failed before execution due to malformed tool metadata. Please verify the tool name and parameters, then try again."; - providerHistory.Add(new UserChatMessage(errorMsg)); - continue; - } - - // Execute each tool call (has its own inner try-catch for per-tool errors) - if (chatToolCalls != null) { - foreach (var toolCall in chatToolCalls) { - string toolName = toolCall.FunctionName; - - string toolResultString; - try { - // Parse arguments from JSON - var argsDict = DeserializeToolArgumentsForDisplay(toolCall.FunctionArguments?.ToString()); - - _logger.LogInformation( - "{ServiceName}: Native tool call parsed; executing now. Tool={ToolName}, ToolCallId={ToolCallId}, ChatId={ChatId}, UserId={UserId}, MessageId={MessageId}, Arguments={Arguments}", - ServiceName, - toolName, - toolCall.Id, - ChatId, - message.FromUserId, - message.MessageId, - JsonConvert.SerializeObject(argsDict)); - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(toolName, argsDict, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - _logger.LogInformation("{ServiceName}: Tool {ToolName} executed. Result length: {Length}", ServiceName, toolName, toolResultString.Length); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Error executing native tool {ToolName}. ToolCallId={ToolCallId}, Arguments={Arguments}, ErrorSummary={ErrorSummary}", - ServiceName, - toolName, - toolCall.Id, - toolCall.FunctionArguments?.ToString(), - ex.GetLogSummary()); - toolResultString = $"Error executing tool {toolName}: {ex.GetLogSummary()}"; - } - - // Add tool result to history using the proper ToolChatMessage - providerHistory.Add(new ToolChatMessage(toolCall.Id, toolResultString)); - } - } - - yield return currentMessageContentBuilder.ToString(); - - // Continue loop for next LLM call - } else { - if (toolCallAccumulators.Any()) { - _logger.LogWarning( - "{ServiceName}: Native response contained tool call updates but finish reason was not ToolCalls. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}", - ServiceName, - finishReason?.ToString() ?? "null", - toolCallAccumulators.Count); - } - - // Not a tool call - regular text response - if (!string.IsNullOrWhiteSpace(responseText)) { - _logger.LogInformation( - "{ServiceName}: Native cycle produced final text without native tool calls. FinishReason={FinishReason}, TextLength={TextLength}, ToolCount={ToolCount}, TextPreview={TextPreview}", - ServiceName, - finishReason?.ToString() ?? "null", - responseText.Length, - completionOptions.Tools.Count, - SanitizeAndTruncateArguments(responseText, 1024)); - var assistantMsg = new AssistantChatMessage(responseText); - SetAssistantReasoningContent(assistantMsg, reasoningContent, includeEmptyReasoningContent); - providerHistory.Add(assistantMsg); - } else { - _logger.LogWarning( - "{ServiceName}: Native cycle produced empty final text and no native tool calls. FinishReason={FinishReason}, ToolCount={ToolCount}", - ServiceName, - finishReason?.ToString() ?? "null", - completionOptions.Tools.Count); - } - yield break; - } - } - - _logger.LogWarning("{ServiceName}: Max tool call cycles reached for chat {ChatId}. User confirmation needed.", ServiceName, ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "OpenAI", - ChannelId = channel.Id, - LastAccumulatedContent = currentMessageContentBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = SerializeProviderHistory(providerHistory), - }; - } - } finally { - // No cleanup needed + var source = new OpenAiChatTurnSource( + this, chatClient, completionOptions, providerHistory, + shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, cacheKeyAttached, + includeEmptyReasoningContent, nativeTools: true, modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "OpenAI", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; } } @@ -1533,126 +1306,220 @@ private async IAsyncEnumerable ExecWithXmlToolCallingAsync( cacheKeyAttached = true; } - try { - int maxToolCycles = Env.MaxToolCycles; - var currentMessageContentBuilder = new StringBuilder(); + var source = new OpenAiChatTurnSource( + this, chatClient, completionOptions, providerHistory, + shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, cacheKeyAttached, + includeEmptyReasoningContent: false, nativeTools: false, modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "OpenAI", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; + } + } - for (int cycle = 0; cycle < maxToolCycles; cycle++) { + /// + /// Per-run turn source for the OpenAI Chat Completions API. Owns the provider history, + /// streams one turn per call, and supports both native tool-calling and the XML text + /// protocol. Snapshot history is produced by . + /// + private sealed class OpenAiChatTurnSource : ILlmTurnSource { + private readonly OpenAIService _svc; + private readonly ChatClient _chatClient; + private readonly ChatCompletionOptions _completionOptions; + private readonly List _providerHistory; + private readonly bool _shouldObservePromptCaching; + private readonly bool _promptCachingEnabled; + private readonly string _toolDefinitionHash; + private readonly string _stablePrefixHash; + private readonly string _promptCacheKey; + private readonly bool _cacheKeyAttached; + private readonly bool _includeEmptyReasoningContent; + private readonly bool _nativeTools; + private readonly string _modelName; + private readonly LLMChannel _channel; + + private AssistantChatMessage _pendingAssistantMessage; + private bool _pendingMalformed; + + public OpenAiChatTurnSource( + OpenAIService svc, ChatClient chatClient, ChatCompletionOptions completionOptions, + List providerHistory, + bool shouldObservePromptCaching, bool promptCachingEnabled, + string toolDefinitionHash, string stablePrefixHash, string promptCacheKey, bool cacheKeyAttached, + bool includeEmptyReasoningContent, bool nativeTools, string modelName, LLMChannel channel) { + _svc = svc; + _chatClient = chatClient; + _completionOptions = completionOptions; + _providerHistory = providerHistory; + _shouldObservePromptCaching = shouldObservePromptCaching; + _promptCachingEnabled = promptCachingEnabled; + _toolDefinitionHash = toolDefinitionHash; + _stablePrefixHash = stablePrefixHash; + _promptCacheKey = promptCacheKey; + _cacheKeyAttached = cacheKeyAttached; + _includeEmptyReasoningContent = includeEmptyReasoningContent; + _nativeTools = nativeTools; + _modelName = modelName; + _channel = channel; + } + + public bool SupportsNativeTools => _nativeTools; + + public async IAsyncEnumerable StreamTurnAsync( + string? userContent, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + var contentBuilder = new StringBuilder(); + var reasoningContentBuilder = new StringBuilder(); + var toolCallAccumulators = new Dictionary(); + ChatFinishReason? finishReason = null; + ChatTokenUsage latestUsage = null; + var streamedAny = false; + + await foreach (var update in _chatClient.CompleteChatStreamingAsync(_providerHistory, _completionOptions, cancellationToken).WithCancellation(cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - var llmResponseAccumulatorForToolParsing = new StringBuilder(); - ChatTokenUsage latestUsage = null; - - await foreach (var update in chatClient.CompleteChatStreamingAsync(providerHistory, completionOptions, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { - if (updatePart?.Text != null) { - currentMessageContentBuilder.Append(updatePart.Text); - llmResponseAccumulatorForToolParsing.Append(updatePart.Text); - if (currentMessageContentBuilder.ToString().Length > 10) { - yield return currentMessageContentBuilder.ToString(); - } - } + foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { + if (updatePart?.Text != null) { + contentBuilder.Append(updatePart.Text); + streamedAny = true; + yield return new LlmStreamEvent.TextDelta(updatePart.Text); } + } - if (update.Usage != null) { - latestUsage = update.Usage; - } + var reasoningUpdate = GetStreamingReasoningContent(update); + if (!string.IsNullOrEmpty(reasoningUpdate)) { + reasoningContentBuilder.Append(reasoningUpdate); + streamedAny = true; + yield return new LlmStreamEvent.ThinkingDelta(reasoningUpdate); } - if (shouldObservePromptCaching) { - LogOpenAiPromptCachingObservation( - "OpenAI", - channel, - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - promptCacheKey, - latestUsage, - cacheKeyAttached); + foreach (var toolCallUpdate in update.ToolCallUpdates ?? Enumerable.Empty()) { + int index = toolCallUpdate.Index; + if (!toolCallAccumulators.ContainsKey(index)) { + toolCallAccumulators[index] = new ToolCallAccumulator(); + } + + var acc = toolCallAccumulators[index]; + if (toolCallUpdate.ToolCallId != null) acc.Id ??= toolCallUpdate.ToolCallId; + if (toolCallUpdate.FunctionName != null) acc.Name ??= toolCallUpdate.FunctionName; + if (toolCallUpdate.FunctionArgumentsUpdate != null) { + acc.Arguments.Append(toolCallUpdate.FunctionArgumentsUpdate); + } } - string llmFullResponseText = llmResponseAccumulatorForToolParsing.ToString().Trim(); - _logger.LogDebug("{ServiceName} raw full response (Cycle {Cycle}): {Response}", ServiceName, cycle + 1, llmFullResponseText); + if (update.FinishReason.HasValue) { + finishReason = update.FinishReason.Value; + } - if (!string.IsNullOrWhiteSpace(llmFullResponseText)) { - providerHistory.Add(new AssistantChatMessage(llmFullResponseText)); - } else if (cycle < maxToolCycles - 1) { - _logger.LogWarning("{ServiceName}: LLM returned empty response during tool cycle {Cycle}.", ServiceName, cycle + 1); + if (update.Usage != null) { + latestUsage = update.Usage; } + } - // --- Tool Handling (XML parsing) --- - if (McpToolHelper.TryParseToolCalls(llmFullResponseText, out var parsedToolCalls) && parsedToolCalls.Any()) { - var firstToolCall = parsedToolCalls[0]; - string parsedToolName = firstToolCall.toolName; - Dictionary toolArguments = firstToolCall.arguments; + if (_shouldObservePromptCaching) { + _svc.LogOpenAiPromptCachingObservation( + "OpenAI", + _channel, + _modelName, + _promptCachingEnabled, + _toolDefinitionHash, + _stablePrefixHash, + _promptCacheKey, + latestUsage, + _cacheKeyAttached); + } - _logger.LogInformation("{ServiceName}: LLM requested tool: {ToolName} with arguments: {Arguments}", ServiceName, parsedToolName, JsonConvert.SerializeObject(toolArguments)); - if (parsedToolCalls.Count > 1) { - _logger.LogWarning("{ServiceName}: LLM returned multiple tool calls ({Count}). Only the first one ('{FirstToolName}') will be executed.", ServiceName, parsedToolCalls.Count, parsedToolName); + var responseText = contentBuilder.ToString().Trim(); + var reasoningContent = reasoningContentBuilder.ToString().Trim(); + _pendingAssistantMessage = null; + _pendingMalformed = false; + + var toolCalls = new List(); + if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCallAccumulators.Any()) { + try { + var chatToolCalls = new List(); + foreach (var (index, acc) in toolCallAccumulators) { + var toolCallId = OpenAIService.NormalizeToolCallId(acc.Id); + var toolName = OpenAIService.NormalizeToolCallName(acc.Name); + var argumentsJson = OpenAIService.NormalizeToolCallArguments(acc.Arguments.ToString()); + chatToolCalls.Add(ChatToolCall.CreateFunctionToolCall( + toolCallId, + toolName, + BinaryData.FromString(argumentsJson))); } - string toolResultString; - bool isError = false; - try { - _logger.LogInformation( - "{ServiceName}: XML tool call parsed; executing now. Tool={ToolName}, ChatId={ChatId}, UserId={UserId}, MessageId={MessageId}, Arguments={Arguments}", - ServiceName, - parsedToolName, - ChatId, - message.FromUserId, - message.MessageId, - JsonConvert.SerializeObject(toolArguments)); - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(parsedToolName, toolArguments, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - _logger.LogInformation("{ServiceName}: Tool {ToolName} executed. Result: {Result}", ServiceName, parsedToolName, toolResultString); - } catch (Exception ex) { - isError = true; - _logger.LogError( - ex, - "{ServiceName}: Error executing XML tool {ToolName}. Arguments={Arguments}, ErrorSummary={ErrorSummary}", - ServiceName, - parsedToolName, - JsonConvert.SerializeObject(toolArguments), - ex.GetLogSummary()); - toolResultString = $"Error executing tool {parsedToolName}: {ex.GetLogSummary()}."; + var assistantMessage = new AssistantChatMessage(chatToolCalls); + if (!string.IsNullOrWhiteSpace(responseText)) { + assistantMessage = new AssistantChatMessage(chatToolCalls) { Content = { ChatMessageContentPart.CreateTextPart(responseText) } }; } + OpenAIService.SetAssistantReasoningContent(assistantMessage, reasoningContent, _includeEmptyReasoningContent); + _pendingAssistantMessage = assistantMessage; + + toolCalls = chatToolCalls.Select(tc => new LlmNativeToolCall { + Id = tc.Id, + Name = tc.FunctionName, + ArgumentsJson = tc.FunctionArguments?.ToString() ?? "{}" + }).ToList(); + } catch (Exception ex) { + _svc._logger.LogError( + ex, + "{ServiceName}: Error building native tool calls, returning error to LLM for self-correction. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}, ErrorSummary={ErrorSummary}", + _svc.ServiceName, finishReason, toolCallAccumulators.Count, ex.GetLogSummary()); + const string errorMsg = "Tool call failed before execution due to malformed tool metadata. Please verify the tool name and parameters, then try again."; + _providerHistory.Add(new UserChatMessage(errorMsg)); + _pendingMalformed = true; + } + } else if (toolCallAccumulators.Any()) { + _svc._logger.LogWarning( + "{ServiceName}: Native response contained tool call updates but finish reason was not ToolCalls. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}", + _svc.ServiceName, finishReason, toolCallAccumulators.Count); + } - currentMessageContentBuilder.Append(McpToolHelper.FormatToolCallDisplay(parsedToolName, toolArguments)); - yield return currentMessageContentBuilder.ToString(); + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = responseText, + Reasoning = reasoningContent, + ToolCalls = toolCalls, + MalformedToolCall = _pendingMalformed, + StreamedAny = streamedAny + }); + } - string feedbackPrefix = isError ? $"[Tool '{parsedToolName}' Execution Failed. Error: " : $"[Executed Tool '{parsedToolName}'. Result: "; - string feedback = $"{feedbackPrefix}{toolResultString}]"; - providerHistory.Add(new UserChatMessage(feedback)); - _logger.LogInformation("Added UserChatMessage to history for LLM: {Feedback}", feedback); + public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { + if (SupportsNativeTools) { + if (!string.IsNullOrWhiteSpace(text)) { + var assistantMsg = new AssistantChatMessage(text); + OpenAIService.SetAssistantReasoningContent(assistantMsg, reasoning, _includeEmptyReasoningContent); + _providerHistory.Add(assistantMsg); } else { - if (string.IsNullOrWhiteSpace(llmFullResponseText)) { - _logger.LogWarning("{ServiceName}: LLM returned empty final non-tool response for ChatId {ChatId}.", ServiceName, ChatId); - } - yield break; + _svc._logger.LogWarning( + "{ServiceName}: Native cycle produced empty final text and no native tool calls.", + _svc.ServiceName); } + return; } + if (!string.IsNullOrWhiteSpace(text)) { + _providerHistory.Add(new AssistantChatMessage(text)); + } + } - _logger.LogWarning("{ServiceName}: Max tool call cycles reached for chat {ChatId}. User confirmation needed.", ServiceName, ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "OpenAI", - ChannelId = channel.Id, - LastAccumulatedContent = currentMessageContentBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = SerializeProviderHistory(providerHistory), - }; + public void CommitToolResults(IReadOnlyList results) { + if (_pendingAssistantMessage != null) { + _providerHistory.Add(_pendingAssistantMessage); + } + foreach (var result in results) { + _providerHistory.Add(new ToolChatMessage(result.ToolCallId, result.Result)); } - } finally { - // No cleanup needed for ToolContext + } + + public IReadOnlyList GetTrackedHistory() { + return OpenAIService.SerializeProviderHistory(_providerHistory); } } @@ -1714,128 +1581,25 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSna cacheKeyAttached = true; } - // Resume: only yield NEW content (the old content was already displayed to the user) - // We keep a separate builder for full accumulated content (for snapshot) and one for new-only content (for yield) - var fullContentBuilder = new StringBuilder(snapshot.LastAccumulatedContent ?? ""); - var newContentBuilder = new StringBuilder(); - - try { - int maxToolCycles = Env.MaxToolCycles; - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - var llmResponseAccumulatorForToolParsing = new StringBuilder(); - ChatTokenUsage latestUsage = null; - - await foreach (var update in chatClient.CompleteChatStreamingAsync(providerHistory, completionOptions, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { - if (updatePart?.Text != null) { - fullContentBuilder.Append(updatePart.Text); - newContentBuilder.Append(updatePart.Text); - llmResponseAccumulatorForToolParsing.Append(updatePart.Text); - var newContent = newContentBuilder.ToString(); - if (newContent.Length > 10) { - yield return newContent; - } - } - } - - if (update.Usage != null) { - latestUsage = update.Usage; - } - } - - if (shouldObservePromptCaching) { - LogOpenAiPromptCachingObservation( - "OpenAI", - channel, - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - promptCacheKey, - latestUsage, - cacheKeyAttached); - } - - string llmFullResponseText = llmResponseAccumulatorForToolParsing.ToString().Trim(); - _logger.LogDebug("{ServiceName} raw full response (Resume Cycle {Cycle}): {Response}", ServiceName, cycle + 1, llmFullResponseText); - - if (!string.IsNullOrWhiteSpace(llmFullResponseText)) { - providerHistory.Add(new AssistantChatMessage(llmFullResponseText)); - } - - if (McpToolHelper.TryParseToolCalls(llmFullResponseText, out var parsedToolCalls) && parsedToolCalls.Any()) { - var firstToolCall = parsedToolCalls[0]; - string parsedToolName = firstToolCall.toolName; - Dictionary toolArguments = firstToolCall.arguments; - - _logger.LogInformation("{ServiceName}: LLM requested tool (resume): {ToolName}", ServiceName, parsedToolName); - - string toolResultString; - bool isError = false; - try { - _logger.LogInformation( - "{ServiceName}: XML tool call parsed during resume; executing now. Tool={ToolName}, ChatId={ChatId}, UserId={UserId}, MessageId={MessageId}, Arguments={Arguments}", - ServiceName, - parsedToolName, - snapshot.ChatId, - snapshot.UserId, - snapshot.OriginalMessageId, - JsonConvert.SerializeObject(toolArguments)); - var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(parsedToolName, toolArguments, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - _logger.LogInformation("{ServiceName}: Tool {ToolName} executed during resume. Result length: {Length}", ServiceName, parsedToolName, toolResultString.Length); - } catch (Exception ex) { - isError = true; - _logger.LogError( - ex, - "{ServiceName}: Error executing XML tool {ToolName} (resume). Arguments={Arguments}, SnapshotId={SnapshotId}, ErrorSummary={ErrorSummary}", - ServiceName, - parsedToolName, - JsonConvert.SerializeObject(toolArguments), - snapshot.SnapshotId, - ex.GetLogSummary()); - toolResultString = $"Error executing tool {parsedToolName}: {ex.GetLogSummary()}."; - } - - var toolIndicator = McpToolHelper.FormatToolCallDisplay(parsedToolName, toolArguments); - newContentBuilder.Append(toolIndicator); - fullContentBuilder.Append(toolIndicator); - yield return newContentBuilder.ToString(); - - string feedbackPrefix = isError ? $"[Tool '{parsedToolName}' Execution Failed. Error: " : $"[Executed Tool '{parsedToolName}'. Result: "; - string feedback = $"{feedbackPrefix}{toolResultString}]"; - providerHistory.Add(new UserChatMessage(feedback)); - } else { - yield break; - } - } - - _logger.LogWarning("{ServiceName}: Max tool call cycles reached again during resume for ChatId {ChatId}.", ServiceName, snapshot.ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = snapshot.ChatId, - OriginalMessageId = snapshot.OriginalMessageId, - UserId = snapshot.UserId, - ModelName = modelName, - Provider = "OpenAI", - ChannelId = channel.Id, - LastAccumulatedContent = fullContentBuilder.ToString(), - CyclesSoFar = snapshot.CyclesSoFar + maxToolCycles, - ProviderHistory = SerializeProviderHistory(providerHistory), - }; - } - } finally { - // No cleanup needed + var source = new OpenAiChatTurnSource( + this, chatClient, completionOptions, providerHistory, + shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, cacheKeyAttached, + includeEmptyReasoningContent, nativeTools: false, modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = snapshot.ChatId, + OriginalMessageId = snapshot.OriginalMessageId, + UserId = snapshot.UserId, + ModelName = modelName, + Provider = "OpenAI", + ChannelId = channel.Id, + BaseCycles = snapshot.CyclesSoFar, + InitialContent = snapshot.LastAccumulatedContent ?? string.Empty + }; + var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; } } - /// /// Serialize OpenAI ChatMessage list to portable format for snapshot persistence. /// From 8c27c2503c504fc32be6bea980646c6cef5cd3d7 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 13:15:22 +0800 Subject: [PATCH 03/30] refactor(llm): migrate OllamaService and GeminiService to shared LlmToolLoop --- .../Service/AI/LLM/GeminiService.cs | 128 +++++++--------- .../Service/AI/LLM/OllamaService.cs | 142 +++++++----------- 2 files changed, 114 insertions(+), 156 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs index 81658bf7..9ec5c7d0 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs @@ -416,86 +416,72 @@ await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, can _chatSessions[ChatId] = chatSession; } - int maxToolCycles = Env.MaxToolCycles; - var currentMessageBuilder = new StringBuilder(); - // Track history for snapshot - var trackedHistory = new List(); var botName = await GetBotNameAsync(); - trackedHistory.Add(new SerializedChatMessage { Role = "system", Content = McpToolHelper.FormatSystemPrompt(botName, ChatId) }); - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - var fullResponseBuilder = new StringBuilder(); - trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = message.Content }); + var trackedHistory = new List { + new SerializedChatMessage { Role = "system", Content = McpToolHelper.FormatSystemPrompt(botName, ChatId) } + }; + + var source = new GeminiTurnSource(this, chatSession, trackedHistory); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "Gemini", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, message.Content, toolContext, meta, executionContext, cancellationToken)) { + yield return item; + } + } - await foreach (var chunk in chatSession.StreamContentAsync(message.Content)) { - currentMessageBuilder.Append(chunk.Text); - fullResponseBuilder.Append(chunk.Text); - yield return currentMessageBuilder.ToString(); + /// + /// Session-based text-protocol turn source for Gemini (ChatSession owns history). + /// Snapshot history is tracked manually alongside the session. + /// + private sealed class GeminiTurnSource : ILlmTurnSource { + private readonly GeminiService _svc; + private readonly ChatSession _chatSession; + private readonly List _trackedHistory; + + public GeminiTurnSource(GeminiService svc, ChatSession chatSession, List trackedHistory) { + _svc = svc; + _chatSession = chatSession; + _trackedHistory = trackedHistory; + } + + public bool SupportsNativeTools => false; + + public async IAsyncEnumerable StreamTurnAsync( + string? userContent, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + var turnText = new StringBuilder(); + await foreach (var chunk in _chatSession.StreamContentAsync(userContent ?? string.Empty).WithCancellation(cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + turnText.Append(chunk.Text); + yield return new LlmStreamEvent.TextDelta(chunk.Text); } - string llmResponse = fullResponseBuilder.ToString().Trim(); - _logger.LogDebug("Gemini raw response (Cycle {Cycle}): {Response}", cycle + 1, llmResponse); - trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = llmResponse }); - - if (McpToolHelper.TryParseToolCalls(llmResponse, out var toolCalls) && toolCalls.Any()) { - var firstToolCall = toolCalls[0]; - _logger.LogInformation("Gemini requested tool: {ToolName} with args: {Args}", - firstToolCall.toolName, - JsonConvert.SerializeObject(firstToolCall.arguments)); - - currentMessageBuilder.Append(McpToolHelper.FormatToolCallDisplay(firstToolCall.toolName, firstToolCall.arguments)); - yield return currentMessageBuilder.ToString(); - - string toolResult; - bool isError = false; - try { - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var result = await McpToolHelper.ExecuteRegisteredToolAsync( - firstToolCall.toolName, - firstToolCall.arguments, - toolContext); - toolResult = McpToolHelper.ConvertToolResultToString(result); - } catch (Exception ex) { - isError = true; - _logger.LogError( - ex, - "{ServiceName}: Error executing Gemini XML tool {ToolName}. Arguments={Arguments}, ErrorSummary={ErrorSummary}", - ServiceName, - firstToolCall.toolName, - JsonConvert.SerializeObject(firstToolCall.arguments), - ex.GetLogSummary()); - toolResult = $"Error executing tool {firstToolCall.toolName}: {ex.GetLogSummary()}"; - } - - string feedback = isError - ? $"[Tool '{firstToolCall.toolName}' execution failed: {toolResult}]" - : $"[Tool '{firstToolCall.toolName}' result: {toolResult}]"; + var response = turnText.ToString().Trim(); + _trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = userContent ?? string.Empty }); + _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = response }); - message.Content = feedback; - continue; - } + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = response, + StreamedAny = turnText.Length > 0 + }); + } - yield break; + public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { + // Session tracks history; tracked snapshot history already updated in StreamTurnAsync. } - _logger.LogWarning("{ServiceName}: Max tool call cycles reached for chat {ChatId}. User confirmation needed.", ServiceName, ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Gemini", - ChannelId = channel.Id, - LastAccumulatedContent = currentMessageBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = trackedHistory, - }; + public void CommitToolResults(IReadOnlyList results) { + // Text protocol: the shared loop sends tool feedback as the next user message. } + + public IReadOnlyList GetTrackedHistory() => _trackedHistory; } public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs index 2e0f46fe..d5815574 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs @@ -172,101 +172,73 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long var trackedHistory = new List(); trackedHistory.Add(new SerializedChatMessage { Role = "system", Content = systemPrompt }); - try { - string nextMessageToSend = message.Content; - int maxToolCycles = Env.MaxToolCycles; - var currentLlmResponseBuilder = new StringBuilder(); // Accumulates tokens for the current LLM response + var source = new OllamaTurnSource(this, chat, trackedHistory); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "Ollama", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, message.Content, toolContext, meta, executionContext, cancellationToken)) { + yield return item; + } + } - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + /// + /// Session-based text-protocol turn source for Ollama (OllamaSharp.Chat owns history). + /// Snapshot history is tracked manually alongside the session. + /// + private sealed class OllamaTurnSource : ILlmTurnSource { + private readonly OllamaService _svc; + private readonly OllamaSharp.Chat _chat; + private readonly List _trackedHistory; + + public OllamaTurnSource(OllamaService svc, OllamaSharp.Chat chat, List trackedHistory) { + _svc = svc; + _chat = chat; + _trackedHistory = trackedHistory; + } - bool receivedAnyToken = false; + public bool SupportsNativeTools => false; - trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = nextMessageToSend }); + public async IAsyncEnumerable StreamTurnAsync( + string? userContent, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + _trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = userContent ?? string.Empty }); - _logger.LogDebug("Sending to Ollama (Cycle {Cycle}): {Message}", cycle + 1, nextMessageToSend); - await foreach (var token in chat.SendAsync(nextMessageToSend, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - currentLlmResponseBuilder.Append(token); - receivedAnyToken = true; - yield return currentLlmResponseBuilder.ToString(); // Yield current full message - } - string llmFullResponseText = currentLlmResponseBuilder.ToString().Trim(); - _logger.LogDebug("LLM raw full response (Cycle {Cycle}): {Response}", cycle + 1, llmFullResponseText); + var turnText = new StringBuilder(); + var streamedAny = false; + await foreach (var token in _chat.SendAsync(userContent ?? string.Empty, cancellationToken).WithCancellation(cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + turnText.Append(token); + streamedAny = true; + yield return new LlmStreamEvent.TextDelta(token); + } - if (receivedAnyToken) { - trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = llmFullResponseText }); - } + if (!streamedAny) { + _svc._logger.LogWarning("{ServiceName}: Ollama returned an empty stream during a tool cycle.", _svc.ServiceName); + } - if (!receivedAnyToken && cycle < maxToolCycles - 1 && !string.IsNullOrEmpty(nextMessageToSend)) { - _logger.LogWarning("{ServiceName}: Ollama returned empty stream during tool cycle {Cycle} for input '{Input}'.", ServiceName, cycle + 1, nextMessageToSend); - } + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = turnText.ToString().Trim(), + StreamedAny = streamedAny + }); + } - // --- Tool Handling (using the full accumulated response text) --- - if (McpToolHelper.TryParseToolCalls(llmFullResponseText, out var parsedToolCalls) && parsedToolCalls.Any()) { - var firstToolCall = parsedToolCalls[0]; - string parsedToolName = firstToolCall.toolName; - Dictionary toolArguments = firstToolCall.arguments; - - _logger.LogInformation("{ServiceName}: LLM requested tool: {ToolName} with arguments: {Arguments}", ServiceName, parsedToolName, JsonConvert.SerializeObject(toolArguments)); - if (parsedToolCalls.Count > 1) { - _logger.LogWarning("{ServiceName}: LLM returned multiple tool calls ({Count}). Only the first one ('{FirstToolName}') will be executed.", ServiceName, parsedToolCalls.Count, parsedToolName); - } - - currentLlmResponseBuilder.Append(McpToolHelper.FormatToolCallDisplay(parsedToolName, toolArguments)); - yield return currentLlmResponseBuilder.ToString(); - - string toolResultString; - bool isError = false; - try { - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(parsedToolName, toolArguments, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - _logger.LogInformation("{ServiceName}: Tool {ToolName} executed. Result: {Result}", ServiceName, parsedToolName, toolResultString); - } catch (Exception ex) { - isError = true; - _logger.LogError( - ex, - "{ServiceName}: Error executing Ollama XML tool {ToolName}. Arguments={Arguments}, ErrorSummary={ErrorSummary}", - ServiceName, - parsedToolName, - JsonConvert.SerializeObject(toolArguments), - ex.GetLogSummary()); - toolResultString = $"Error executing tool {parsedToolName}: {ex.GetLogSummary()}."; - } - - string feedbackPrefix = isError ? $"[Tool '{parsedToolName}' Execution Failed. Error: " : $"[Executed Tool '{parsedToolName}'. Result: "; - nextMessageToSend = $"{feedbackPrefix}{toolResultString}]"; - _logger.LogInformation("Prepared feedback for next LLM call: {Feedback}", nextMessageToSend); - } else { - if (string.IsNullOrWhiteSpace(llmFullResponseText) && receivedAnyToken) { - _logger.LogWarning("{ServiceName}: LLM returned empty final non-tool response after trimming for ChatId {ChatId}.", ServiceName, ChatId); - } else if (!receivedAnyToken && string.IsNullOrEmpty(llmFullResponseText)) { - _logger.LogWarning("{ServiceName}: LLM returned empty stream and empty final non-tool response for ChatId {ChatId}.", ServiceName, ChatId); - } - yield break; - } + public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { + if (streamedAny) { + _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); } + } - _logger.LogWarning("{ServiceName}: Max tool call cycles reached for chat {ChatId}. User confirmation needed.", ServiceName, ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Ollama", - ChannelId = channel.Id, - LastAccumulatedContent = currentLlmResponseBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = trackedHistory, - }; - } - } finally { - // No cleanup needed for ToolContext + public void CommitToolResults(IReadOnlyList results) { + // Text protocol: the shared loop sends tool feedback as the next user message. } + + public IReadOnlyList GetTrackedHistory() => _trackedHistory; } // ConvertToolResultToString has been moved to McpToolHelper From 4ee54880225d3ad819c1567d8f52117a78839385 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 13:17:10 +0800 Subject: [PATCH 04/30] refactor(llm): migrate OpenAIResponsesService to shared LlmToolLoop --- .../Model/AI/Llm/LlmTurnModels.cs | 1 + .../Service/AI/LLM/LlmToolLoop.cs | 1 + .../Service/AI/LLM/OpenAIResponsesService.cs | 563 ++++++------------ 3 files changed, 194 insertions(+), 371 deletions(-) diff --git a/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs b/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs index 45a8fb44..da6b3ea6 100644 --- a/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs +++ b/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs @@ -30,6 +30,7 @@ public Dictionary ParseArguments() { public sealed class LlmToolResult { public string ToolCallId { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; + public string ArgumentsJson { get; set; } = "{}"; public string Result { get; set; } = string.Empty; public bool IsError { get; set; } } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs index 75e69717..f6770552 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs @@ -82,6 +82,7 @@ public static async IAsyncEnumerable RunAsync( results.Add(new LlmToolResult { ToolCallId = call.Id, Name = call.Name, + ArgumentsJson = call.ArgumentsJson, Result = resultString, IsError = isError }); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs index e36110f8..36a61d7f 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs @@ -274,207 +274,21 @@ private async IAsyncEnumerable ExecWithResponsesApiAsync( var apiKey = new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)); var responsesClient = new ResponsesClient(apiKey, clientOptions); - // --- Tool call loop --- - int maxToolCycles = Env.MaxToolCycles; - var currentMessageContentBuilder = new StringBuilder(); - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - // Build options with input items - var options = new CreateResponseOptions { - Model = modelName, - Instructions = instructions, - StreamingEnabled = true, - }; - var cacheKeyAttached = false; - if (promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(options, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); - cacheKeyAttached = true; - } - foreach (var tool in responseTools) { - options.Tools.Add(tool); - } - foreach (var item in inputItems) { - options.InputItems.Add(item); - } - - // Accumulators for streaming - var textBuilder = new StringBuilder(); - var reasoningBuilder = new StringBuilder(); - var toolCallAccums = new Dictionary(); - ResponseResult completedResult = null; - - bool streamingError = false; - string streamingErrorMessage = null; - - // Stream the response - await foreach (var update in responsesClient.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - try { - ProcessStreamingUpdate(update, textBuilder, currentMessageContentBuilder, reasoningBuilder, toolCallAccums, ref completedResult); - } catch (OperationCanceledException) { - throw; - } catch (Exception ex) { - _logger.LogError(ex, "{ServiceName}: Error during streaming update (Cycle {Cycle})", ServiceName, cycle); - streamingError = true; - streamingErrorMessage = ex.Message; - break; - } - - // Yield progressive text outside try-catch - if (currentMessageContentBuilder.Length > 10) { - var text = currentMessageContentBuilder.ToString(); - if (text.Length > 10) { - yield return text; - } - } - } - - if (streamingError) { - yield return $"\n[Error: {streamingErrorMessage}]"; - yield break; - } - - if (shouldObservePromptCaching) { - LogPromptCachingObservation( - "OpenAIResponses", - channel, - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - promptCacheKey, - completedResult?.Usage, - cacheKeyAttached); - } - - string responseText = textBuilder.ToString().Trim(); - string reasoningContent = reasoningBuilder.ToString().Trim(); - - // --- Check if we have completed response with tool calls --- - var completedFuncCalls = new List(); - if (completedResult?.OutputItems != null) { - foreach (var outputItem in completedResult.OutputItems) { - if (outputItem is FunctionCallResponseItem fcItem - && !string.IsNullOrWhiteSpace(fcItem.CallId) - && !string.IsNullOrWhiteSpace(fcItem.FunctionName)) { - completedFuncCalls.Add(fcItem); - } - } - } - - if (completedFuncCalls.Any()) { - // --- Handle tool calls --- - // Add assistant's text response to history (if any) - if (!string.IsNullOrWhiteSpace(responseText)) { - inputItems.Add(ResponseItem.CreateAssistantMessageItem(responseText)); - } - - var toolIndicators = new StringBuilder(); - foreach (var funcCall in completedFuncCalls) { - string rawCallId = funcCall.CallId; - string rawName = funcCall.FunctionName; - string rawArgsJson = funcCall.FunctionArguments?.ToString() ?? "{}"; - string callId = rawCallId; - string name = rawName; - string argsJson = rawArgsJson; - - // Normalize - callId = OpenAIService.NormalizeToolCallId(callId); - name = OpenAIService.NormalizeToolCallName(name); - argsJson = OpenAIService.NormalizeToolCallArguments(argsJson); - _logger.LogDebug( - "{ServiceName}: Responses API function call metadata normalized. RawCallId={RawCallId}, CallId={CallId}, RawName={RawName}, Name={Name}, RawArguments={RawArguments}, Arguments={Arguments}", - ServiceName, - rawCallId, - callId, - rawName, - name, - rawArgsJson, - argsJson); - - // Add function call item to history - try { - inputItems.Add(ResponseItem.CreateFunctionCallItem( - callId, - name, - BinaryData.FromString(argsJson))); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Failed to create Responses API function call item. CallId={CallId}, ToolName={ToolName}, Arguments={Arguments}, ErrorSummary={ErrorSummary}", - ServiceName, - callId, - name, - argsJson, - ex.GetLogSummary()); - throw; - } - - var argsDict = OpenAIService.DeserializeToolArgumentsForDisplay(argsJson); - toolIndicators.Append(McpToolHelper.FormatToolCallDisplay(name, argsDict)); - - // Execute tool - _logger.LogInformation( - "{ServiceName}: Responses API tool call parsed; executing now. Tool={ToolName}, CallId={CallId}, ChatId={ChatId}, UserId={UserId}, MessageId={MessageId}, Arguments={Arguments}", - ServiceName, - name, - callId, - ChatId, - message.FromUserId, - message.MessageId, - argsJson); - - string toolResultString; - try { - var toolContext = new ToolContext { - ChatId = ChatId, - UserId = message.FromUserId, - MessageId = message.MessageId - }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(name, argsDict, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - _logger.LogInformation("{ServiceName}: Tool {ToolName} executed. Result length: {Length}", - ServiceName, name, toolResultString.Length); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Error executing Responses API tool {ToolName}. CallId={CallId}, Arguments={Arguments}, ErrorSummary={ErrorSummary}", - ServiceName, - name, - callId, - argsJson, - ex.GetLogSummary()); - toolResultString = $"Error executing tool {name}: {ex.GetLogSummary()}"; - } - - // Add function call output to history - inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(callId, toolResultString)); - - _logger.LogInformation("{ServiceName}: Added function call output for {ToolName}", ServiceName, name); - } - - currentMessageContentBuilder.Append(toolIndicators.ToString()); - yield return currentMessageContentBuilder.ToString(); - - // Continue loop for next LLM call - continue; - } - - // --- Regular text response (no tool calls) --- - if (!string.IsNullOrWhiteSpace(responseText)) { - yield return responseText; - } - yield break; - } - - // --- Max tool call cycles reached --- - _logger.LogWarning("{ServiceName}: Max tool call cycles reached for chat {ChatId}. User confirmation needed.", ServiceName, ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = BuildSnapshot(ChatId, message, modelName, channel, currentMessageContentBuilder.ToString(), maxToolCycles, inputItems); + var source = new ResponsesTurnSource( + this, responsesClient, responseTools, instructions, inputItems, + shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, + modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "OpenAIResponses", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; } } @@ -552,197 +366,204 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( var apiKey = new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)); var responsesClient = new ResponsesClient(apiKey, clientOptions); - var fullContentBuilder = new StringBuilder(snapshot.LastAccumulatedContent ?? ""); - var newContentBuilder = new StringBuilder(); - - try { - int maxToolCycles = Env.MaxToolCycles; - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - var options = new CreateResponseOptions { - Model = modelName, - Instructions = instructions, - StreamingEnabled = true, - }; - var cacheKeyAttached = false; - if (promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(options, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); - cacheKeyAttached = true; - } - foreach (var tool in responseTools) { - options.Tools.Add(tool); - } - foreach (var item in inputItems) { - options.InputItems.Add(item); - } - - var textBuilder = new StringBuilder(); - var toolCallAccums = new Dictionary(); - ResponseResult completedResult = null; + var source = new ResponsesTurnSource( + this, responsesClient, responseTools, instructions, inputItems, + shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, + modelName, channel); + var meta = new LlmToolLoopMeta { + ChatId = snapshot.ChatId, + OriginalMessageId = snapshot.OriginalMessageId, + UserId = snapshot.UserId, + ModelName = modelName, + Provider = "OpenAIResponses", + ChannelId = channel.Id, + BaseCycles = snapshot.CyclesSoFar, + InitialContent = snapshot.LastAccumulatedContent ?? string.Empty + }; + var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; + await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + yield return item; + } + } - bool resumeStreamingError = false; + /// + /// Per-run turn source for the OpenAI Responses API. Owns the input item list, + /// streams one turn per call, and surfaces completed function calls to the shared loop. + /// + private sealed class ResponsesTurnSource : ILlmTurnSource { + private readonly OpenAIResponsesService _svc; + private readonly ResponsesClient _responsesClient; + private readonly List _responseTools; + private readonly string _instructions; + private readonly List _inputItems; + private readonly bool _shouldObservePromptCaching; + private readonly bool _promptCachingEnabled; + private readonly string _toolDefinitionHash; + private readonly string _stablePrefixHash; + private readonly string _promptCacheKey; + private readonly string _modelName; + private readonly LLMChannel _channel; + + public ResponsesTurnSource( + OpenAIResponsesService svc, ResponsesClient responsesClient, List responseTools, + string instructions, List inputItems, + bool shouldObservePromptCaching, bool promptCachingEnabled, + string toolDefinitionHash, string stablePrefixHash, string promptCacheKey, + string modelName, LLMChannel channel) { + _svc = svc; + _responsesClient = responsesClient; + _responseTools = responseTools; + _instructions = instructions; + _inputItems = inputItems; + _shouldObservePromptCaching = shouldObservePromptCaching; + _promptCachingEnabled = promptCachingEnabled; + _toolDefinitionHash = toolDefinitionHash; + _stablePrefixHash = stablePrefixHash; + _promptCacheKey = promptCacheKey; + _modelName = modelName; + _channel = channel; + } - await foreach (var update in responsesClient.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + public bool SupportsNativeTools => true; - try { - ProcessResumeStreamingUpdate(update, textBuilder, fullContentBuilder, newContentBuilder, toolCallAccums, ref completedResult); - } catch (OperationCanceledException) { - throw; - } catch (Exception ex) { - _logger.LogError(ex, "{ServiceName}: Error during resume streaming call (Cycle {Cycle})", ServiceName, cycle); - resumeStreamingError = true; - break; - } + public async IAsyncEnumerable StreamTurnAsync( + string? userContent, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + var options = new CreateResponseOptions { + Model = _modelName, + Instructions = _instructions, + StreamingEnabled = true, + }; + var cacheKeyAttached = false; + if (_promptCachingEnabled) { + PromptCachingHelper.ApplyOpenAiPromptCaching(options, _promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); + cacheKeyAttached = true; + } + foreach (var tool in _responseTools) { + options.Tools.Add(tool); + } + foreach (var item in _inputItems) { + options.InputItems.Add(item); + } - // Yield progressive text outside try-catch - if (newContentBuilder.Length > 10) { - yield return newContentBuilder.ToString(); - } - } + var textBuilder = new StringBuilder(); + var reasoningBuilder = new StringBuilder(); + var uiBuilder = new StringBuilder(); + var toolCallAccums = new Dictionary(); + ResponseResult completedResult = null; + var lastUiLength = 0; + var streamedAny = false; + var streamingError = false; + string streamingErrorMessage = null; - if (resumeStreamingError) { - yield break; - } + await foreach (var update in _responsesClient.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - if (shouldObservePromptCaching) { - LogPromptCachingObservation( - "OpenAIResponses", - channel, - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - promptCacheKey, - completedResult?.Usage, - cacheKeyAttached); + try { + OpenAIResponsesService.ProcessStreamingUpdate(update, textBuilder, uiBuilder, reasoningBuilder, toolCallAccums, ref completedResult); + } catch (OperationCanceledException) { + throw; + } catch (Exception ex) { + _svc._logger.LogError(ex, "{ServiceName}: Error during streaming update", _svc.ServiceName); + streamingError = true; + streamingErrorMessage = ex.Message; + break; } - string responseText = textBuilder.ToString().Trim(); - - // Check for tool calls - var completedFuncCalls = new List(); - if (completedResult?.OutputItems != null) { - foreach (var outputItem in completedResult.OutputItems) { - if (outputItem is FunctionCallResponseItem fcItem - && !string.IsNullOrWhiteSpace(fcItem.CallId) - && !string.IsNullOrWhiteSpace(fcItem.FunctionName)) { - completedFuncCalls.Add(fcItem); - } - } + if (uiBuilder.Length > lastUiLength) { + var delta = uiBuilder.ToString(lastUiLength, uiBuilder.Length - lastUiLength); + lastUiLength = uiBuilder.Length; + streamedAny = true; + yield return new LlmStreamEvent.TextDelta(delta); } + } - if (completedFuncCalls.Any()) { - if (!string.IsNullOrWhiteSpace(responseText)) { - inputItems.Add(ResponseItem.CreateAssistantMessageItem(responseText)); - } - - foreach (var funcCall in completedFuncCalls) { - string rawCallId = funcCall.CallId; - string rawName = funcCall.FunctionName; - string rawArgsJson = funcCall.FunctionArguments?.ToString() ?? "{}"; - string callId = OpenAIService.NormalizeToolCallId(rawCallId); - string name = OpenAIService.NormalizeToolCallName(rawName); - string argsJson = OpenAIService.NormalizeToolCallArguments(rawArgsJson); - _logger.LogDebug( - "{ServiceName}: Responses API resume function call metadata normalized. RawCallId={RawCallId}, CallId={CallId}, RawName={RawName}, Name={Name}, RawArguments={RawArguments}, Arguments={Arguments}", - ServiceName, - rawCallId, - callId, - rawName, - name, - rawArgsJson, - argsJson); - - try { - inputItems.Add(ResponseItem.CreateFunctionCallItem( - callId, name, - BinaryData.FromString(argsJson))); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Failed to create Responses API function call item during resume. CallId={CallId}, ToolName={ToolName}, Arguments={Arguments}, SnapshotId={SnapshotId}, ErrorSummary={ErrorSummary}", - ServiceName, - callId, - name, - argsJson, - snapshot.SnapshotId, - ex.GetLogSummary()); - throw; - } + if (_shouldObservePromptCaching) { + _svc.LogPromptCachingObservation( + "OpenAIResponses", + _channel, + _modelName, + _promptCachingEnabled, + _toolDefinitionHash, + _stablePrefixHash, + _promptCacheKey, + completedResult?.Usage, + cacheKeyAttached); + } - var argsDict = OpenAIService.DeserializeToolArgumentsForDisplay(argsJson); - - string toolResultString; - try { - _logger.LogInformation( - "{ServiceName}: Responses API tool call parsed during resume; executing now. Tool={ToolName}, CallId={CallId}, ChatId={ChatId}, UserId={UserId}, MessageId={MessageId}, Arguments={Arguments}", - ServiceName, - name, - callId, - snapshot.ChatId, - snapshot.UserId, - snapshot.OriginalMessageId, - argsJson); - var toolContext = new ToolContext { - ChatId = snapshot.ChatId, - UserId = snapshot.UserId, - MessageId = snapshot.OriginalMessageId - }; - object toolResultObject = await McpToolHelper.ExecuteRegisteredToolAsync(name, argsDict, toolContext); - toolResultString = McpToolHelper.ConvertToolResultToString(toolResultObject); - } catch (Exception ex) { - _logger.LogError( - ex, - "{ServiceName}: Error executing Responses API tool {ToolName} (resume). CallId={CallId}, Arguments={Arguments}, SnapshotId={SnapshotId}, ErrorSummary={ErrorSummary}", - ServiceName, - name, - callId, - argsJson, - snapshot.SnapshotId, - ex.GetLogSummary()); - toolResultString = $"Error executing tool {name}: {ex.GetLogSummary()}."; - } + var responseText = textBuilder.ToString().Trim(); + var reasoningContent = reasoningBuilder.ToString().Trim(); + var toolCalls = new List(); - var toolIndicator = McpToolHelper.FormatToolCallDisplay(name, argsDict); - newContentBuilder.Append(toolIndicator); - fullContentBuilder.Append(toolIndicator); - yield return newContentBuilder.ToString(); + if (streamingError) { + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = $"{responseText}\n[Error: {streamingErrorMessage}]", + StreamedAny = streamedAny + }); + yield break; + } - inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(callId, toolResultString)); + var completedFuncCalls = new List(); + if (completedResult?.OutputItems != null) { + foreach (var outputItem in completedResult.OutputItems) { + if (outputItem is FunctionCallResponseItem fcItem + && !string.IsNullOrWhiteSpace(fcItem.CallId) + && !string.IsNullOrWhiteSpace(fcItem.FunctionName)) { + completedFuncCalls.Add(fcItem); } - continue; } + } - // No tool calls - yield final content + if (completedFuncCalls.Any()) { + // Assistant text (if any) + function call items enter history at commit time. + foreach (var funcCall in completedFuncCalls) { + var callId = OpenAIService.NormalizeToolCallId(funcCall.CallId); + var name = OpenAIService.NormalizeToolCallName(funcCall.FunctionName); + var argsJson = OpenAIService.NormalizeToolCallArguments(funcCall.FunctionArguments?.ToString() ?? "{}"); + toolCalls.Add(new LlmNativeToolCall { + Id = callId, + Name = name, + ArgumentsJson = argsJson + }); + } + _pendingAssistantText = responseText; + } else { + _pendingAssistantText = null; if (!string.IsNullOrWhiteSpace(responseText)) { - yield return newContentBuilder.ToString(); + _inputItems.Add(ResponseItem.CreateAssistantMessageItem(responseText)); } - yield break; } - // Max cycles reached again - _logger.LogWarning("{ServiceName}: Max tool call cycles reached again during resume for ChatId {ChatId}.", ServiceName, snapshot.ChatId); - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - SnapshotId = snapshot.SnapshotId, - ChatId = snapshot.ChatId, - OriginalMessageId = snapshot.OriginalMessageId, - UserId = snapshot.UserId, - ModelName = modelName, - Provider = "OpenAIResponses", - ChannelId = channel.Id, - LastAccumulatedContent = fullContentBuilder.ToString(), - CyclesSoFar = snapshot.CyclesSoFar + maxToolCycles, - ProviderHistory = SerializeInputItems(inputItems), - }; + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = responseText, + Reasoning = reasoningContent, + ToolCalls = toolCalls, + StreamedAny = streamedAny + }); + } + + private string _pendingAssistantText; + + public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { + // Final text was already appended to input items during StreamTurnAsync. + } + + public void CommitToolResults(IReadOnlyList results) { + if (!string.IsNullOrWhiteSpace(_pendingAssistantText)) { + _inputItems.Add(ResponseItem.CreateAssistantMessageItem(_pendingAssistantText)); + } + foreach (var result in results) { + _inputItems.Add(ResponseItem.CreateFunctionCallItem( + result.ToolCallId, + result.Name, + BinaryData.FromString("{}"))); + _inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(result.ToolCallId, result.Result)); } - } finally { - // No cleanup needed + _pendingAssistantText = null; + } + + public IReadOnlyList GetTrackedHistory() { + return OpenAIResponsesService.SerializeInputItems(_inputItems); } } From 41cd7e9412323ac9a0789b0f9f007eefaef2b6a6 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 13:19:18 +0800 Subject: [PATCH 05/30] =?UTF-8?q?feat(llm):=20built-in=20provider=20catalo?= =?UTF-8?q?g=20with=20=E9=A2=84=E5=88=B6=E6=B8=A0=E9=81=93=20command=20(pr?= =?UTF-8?q?esets=20prefill=20provider/gateway/models)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/AI/LLM/LlmProviderCatalog.cs | 93 +++++++++++++++++++ TelegramSearchBot/Model/AI/LLMConfState.cs | 6 ++ .../Service/Manage/EditLLMConfService.cs | 61 ++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs new file mode 100644 index 00000000..49b56bfb --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using TelegramSearchBot.Model.AI; + +namespace TelegramSearchBot.Service.AI.LLM { + /// A built-in provider preset: one code-defined entry of the provider catalog. + public sealed record LlmProviderPreset( + string Id, + string DisplayName, + LLMProvider Provider, + string DefaultGateway, + string[] DefaultModels, + bool RequiresApiKey, + string? Notes = null); + + /// + /// Code-defined provider catalog (pi-style): common providers ship preconfigured so the bot + /// admin only picks one and enters an API key. Custom endpoints are still supported by the + /// existing manual channel flow (新建渠道) and LLMApiBinding overrides. + /// + public static class LlmProviderCatalog { + public static readonly IReadOnlyList Presets = new[] { + new LlmProviderPreset( + "anthropic", "Anthropic 官方", LLMProvider.Anthropic, + "https://api.anthropic.com", + new[] { "claude-sonnet-4-5", "claude-opus-4-1", "claude-haiku-4-5" }, + RequiresApiKey: true), + new LlmProviderPreset( + "openai", "OpenAI 官方 (Chat Completions)", LLMProvider.OpenAI, + "https://api.openai.com/v1", + new[] { "gpt-4o", "gpt-4o-mini", "gpt-4.1" }, + RequiresApiKey: true), + new LlmProviderPreset( + "openai-responses", "OpenAI 官方 (Responses API)", LLMProvider.ResponsesAPI, + "https://api.openai.com/v1", + new[] { "gpt-4o", "gpt-4.1" }, + RequiresApiKey: true), + new LlmProviderPreset( + "gemini", "Google Gemini", LLMProvider.Gemini, + "https://generativelanguage.googleapis.com", + new[] { "gemini-2.0-flash", "gemini-2.5-pro" }, + RequiresApiKey: true), + new LlmProviderPreset( + "minimax", "MiniMax", LLMProvider.MiniMax, + "https://api.minimax.chat/v1", + new[] { "MiniMax-Text-01", "abab6.5s-chat" }, + RequiresApiKey: true), + new LlmProviderPreset( + "ollama", "本地 Ollama", LLMProvider.Ollama, + "http://localhost:11434", + Array.Empty(), + RequiresApiKey: false, + Notes: "本地服务无需 API Key;模型通过 `添加模型` 手动添加或自动发现。"), + new LlmProviderPreset( + "lmstudio", "本地 LM Studio", LLMProvider.LMStudio, + "http://localhost:1234/v1", + Array.Empty(), + RequiresApiKey: false, + Notes: "本地服务无需 API Key;模型通过 `添加模型` 手动添加。"), + new LlmProviderPreset( + "deepseek", "DeepSeek (OpenAI 兼容)", LLMProvider.OpenAI, + "https://api.deepseek.com/v1", + new[] { "deepseek-chat", "deepseek-reasoner" }, + RequiresApiKey: true), + new LlmProviderPreset( + "moonshot", "Moonshot Kimi (OpenAI 兼容)", LLMProvider.OpenAI, + "https://api.moonshot.cn/v1", + new[] { "kimi-k2-0711-preview", "moonshot-v1-128k" }, + RequiresApiKey: true), + }; + + public static LlmProviderPreset? FindById(string id) => + Presets.FirstOrDefault(p => p.Id.Equals(id, StringComparison.OrdinalIgnoreCase)); + + /// Human-readable numbered list for bot selection. + public static string FormatMenu() { + var sb = new System.Text.StringBuilder("请选择要创建的渠道预设:\n"); + for (var i = 0; i < Presets.Count; i++) { + var p = Presets[i]; + sb.AppendLine($"{i + 1}. {p.DisplayName} ({p.DefaultGateway})"); + if (p.DefaultModels.Length > 0) { + sb.AppendLine($" 默认模型: {string.Join(", ", p.DefaultModels)}"); + } + if (!string.IsNullOrEmpty(p.Notes)) { + sb.AppendLine($" 备注: {p.Notes}"); + } + } + sb.Append("\n发送编号选择;发送 取消 退出。"); + return sb.ToString(); + } + } +} diff --git a/TelegramSearchBot/Model/AI/LLMConfState.cs b/TelegramSearchBot/Model/AI/LLMConfState.cs index 5ec14ee1..6536e5e2 100644 --- a/TelegramSearchBot/Model/AI/LLMConfState.cs +++ b/TelegramSearchBot/Model/AI/LLMConfState.cs @@ -17,6 +17,12 @@ public enum LLMConfState { [Description("setting_music_generation_model")] SettingMusicGenerationModel, + [Description("awaiting_preset_selection")] + AwaitingPresetSelection, + + [Description("awaiting_preset_apikey")] + AwaitingPresetApiKey, + [Description("awaiting_name")] AwaitingName, diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs index 8566339e..cff017ba 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs @@ -50,6 +50,8 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService _stateHandlers = new Dictionary>> { { LLMConfState.AwaitingName.GetDescription(), HandleAwaitingNameAsync }, + { LLMConfState.AwaitingPresetSelection.GetDescription(), HandleAwaitingPresetSelectionAsync }, + { LLMConfState.AwaitingPresetApiKey.GetDescription(), HandleAwaitingPresetApiKeyAsync }, { LLMConfState.AwaitingGateway.GetDescription(), HandleAwaitingGatewayAsync }, { LLMConfState.AwaitingProvider.GetDescription(), HandleAwaitingProviderAsync }, { LLMConfState.AwaitingParallel.GetDescription(), HandleAwaitingParallelAsync }, @@ -91,6 +93,60 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService }; } + private async Task<(bool, string)> HandleAwaitingPresetSelectionAsync(EditLLMConfRedisHelper redis, string command) { + if (command.Trim().Equals("取消", StringComparison.OrdinalIgnoreCase)) { + await redis.DeleteKeysAsync(); + return (true, "已取消"); + } + + if (!int.TryParse(command.Trim(), out var presetIndex) || + presetIndex < 1 || presetIndex > LlmProviderCatalog.Presets.Count) { + return (false, $"无效的预设编号,请输入 1 到 {LlmProviderCatalog.Presets.Count} 之间的数字,或发送 取消 退出"); + } + + var preset = LlmProviderCatalog.Presets[presetIndex - 1]; + if (!preset.RequiresApiKey) { + return await CreatePresetChannelAsync(redis, preset, string.Empty); + } + + await redis.SetDataAsync(preset.Id); + await redis.SetStateAsync(LLMConfState.AwaitingPresetApiKey.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 API Key(本地/免 Key 服务发送 - 跳过):"); + } + + private async Task<(bool, string)> HandleAwaitingPresetApiKeyAsync(EditLLMConfRedisHelper redis, string command) { + var preset = LlmProviderCatalog.FindById(await redis.GetDataAsync()); + if (preset == null) { + await redis.DeleteKeysAsync(); + return (false, "预设状态丢失,请重新发送 预制渠道"); + } + + var apiKey = command.Trim().Equals("-", StringComparison.OrdinalIgnoreCase) ? string.Empty : command.Trim(); + return await CreatePresetChannelAsync(redis, preset, apiKey); + } + + private async Task<(bool, string)> CreatePresetChannelAsync(EditLLMConfRedisHelper redis, LlmProviderPreset preset, string apiKey) { + var channelId = await Helper.AddChannel( + preset.DisplayName, + preset.DefaultGateway, + apiKey, + preset.Provider, + 1, + 0); + + await redis.DeleteKeysAsync(); + if (channelId <= 0) { + return (true, "渠道创建失败"); + } + + var message = $"渠道创建成功(ID: {channelId},类型: {preset.Provider})"; + if (preset.DefaultModels.Length > 0 && await Helper.AddModelWithChannel(channelId, preset.DefaultModels.ToList())) { + message += $"\n已预置模型: {string.Join(", ", preset.DefaultModels)}"; + message += "\n如需自定义模型,可使用 `添加模型`。"; + } + return (true, message); + } + private async Task<(bool, string)> HandleAwaitingNameAsync(EditLLMConfRedisHelper redis, string command) { await redis.SetDataAsync(command); // 存储名称 await redis.SetStateAsync(LLMConfState.AwaitingGateway.GetDescription()); @@ -574,6 +630,11 @@ await DataContext.AppConfigurationItems.AddAsync(new Model.Data.AppConfiguration return (true, "请输入渠道的名称"); } + if (cmd.Equals("预制渠道", StringComparison.OrdinalIgnoreCase)) { + await redis.SetStateAsync(LLMConfState.AwaitingPresetSelection.GetDescription()); + return (true, LlmProviderCatalog.FormatMenu()); + } + if (cmd.Equals("编辑渠道", StringComparison.OrdinalIgnoreCase)) { return await HandleEditChannelCommandAsync(redis); } From 3b728244977c5eb000c699e15f1f28ac24441e1b Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 13:57:08 +0800 Subject: [PATCH 06/30] test(llm): unit tests for shared LlmToolLoop (text protocol, native protocol, iteration limit, resume) --- .../Service/AI/LLM/LlmToolLoopTests.cs | 229 ++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs new file mode 100644 index 00000000..a355a021 --- /dev/null +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs @@ -0,0 +1,229 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Service.AI.LLM; +using Xunit; + +namespace TelegramSearchBot.LLM.Test.Service.AI.LLM { + /// + /// Unit tests for the shared agent tool-call loop (LlmToolLoop). Uses a scripted + /// ILlmTurnSource plus a mock external tool registered in McpToolHelper. + /// + public class LlmToolLoopTests { + private static ToolContext ToolCtx() => new ToolContext { ChatId = 1, UserId = 2, MessageId = 3 }; + + private static LlmToolLoopMeta Meta() => new LlmToolLoopMeta { + ChatId = 1, + OriginalMessageId = 3, + UserId = 2, + ModelName = "test-model", + Provider = "Test", + ChannelId = 9 + }; + + /// Scripted source: each enqueued factory produces one turn's events. + private sealed class ScriptedTurnSource : ILlmTurnSource { + private readonly Queue>> _turns = new(); + + public bool SupportsNativeTools { get; set; } + public List ReceivedUserContents { get; } = new(); + public List> CommittedResults { get; } = new(); + public List TrackedHistory { get; } = new(); + + public void EnqueueTurn(Func> turnFactory) => _turns.Enqueue(turnFactory); + + public async IAsyncEnumerable StreamTurnAsync( + string? userContent, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + ReceivedUserContents.Add(userContent); + var turn = _turns.Dequeue(); + foreach (var evt in turn(userContent)) { + yield return evt; + } + await Task.CompletedTask; + } + + public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { + TrackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); + } + + public void CommitToolResults(IReadOnlyList results) { + CommittedResults.Add(results.ToList()); + TrackedHistory.Add(new SerializedChatMessage { Role = "user", Content = "tool-results" }); + } + + public IReadOnlyList GetTrackedHistory() => TrackedHistory; + } + + private static Func, Task> EchoExecutor() => + async (server, tool, args) => { + await Task.CompletedTask; + return $"mock:{args.GetValueOrDefault("input", "")}"; + }; + + [Fact] + public async Task TextProtocol_ParsesToolCall_Executes_SendsFeedback() { + McpToolHelper.RegisterExternalTools( + new List<(string, McpToolHelper.ExternalToolInfo)> { + ("test-server", new McpToolHelper.ExternalToolInfo { + ServerName = "test-server", + ToolName = "loopTool", + Description = "test tool", + Parameters = new List { + new() { Name = "input", Type = "string", Description = "input", Required = true } + } + }) + }, + EchoExecutor()); + + try { + var source = new ScriptedTurnSource(); + const string xml = "v"; + source.EnqueueTurn(_ => new[] { + new LlmStreamEvent.TextDelta(xml), + ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = xml }) + }); + source.EnqueueTurn(_ => new[] { + new LlmStreamEvent.TextDelta("final answer text"), + ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = "final answer text" }) + }); + + var yielded = new List(); + await foreach (var item in LlmToolLoop.RunAsync(source, "hi", ToolCtx(), Meta(), null, CancellationToken.None)) { + yielded.Add(item); + } + + Assert.Equal(2, source.ReceivedUserContents.Count); + Assert.Equal("hi", source.ReceivedUserContents[0]); + Assert.Contains("[Executed Tool 'mcp_test-server_loopTool'. Result: mock:v]", source.ReceivedUserContents[1]); + // Cumulative snapshot semantics: one yield per text delta beyond 10 chars + one per tool display. + Assert.True(yielded.Count >= 2); + Assert.Contains("final answer text", yielded[^1]); + Assert.Empty(source.CommittedResults); + } finally { + McpToolHelper.RegisterExternalTools( + new List<(string, McpToolHelper.ExternalToolInfo)>(), + async (server, tool, args) => { await Task.CompletedTask; return string.Empty; }); + } + } + + [Fact] + public async Task NativeProtocol_CommitsToolResultsAndPassesNullUserContent() { + McpToolHelper.RegisterExternalTools( + new List<(string, McpToolHelper.ExternalToolInfo)> { + ("test-server", new McpToolHelper.ExternalToolInfo { + ServerName = "test-server", + ToolName = "nativeTool", + Description = "native test tool", + Parameters = new List() + }) + }, + async (server, tool, args) => { await Task.CompletedTask; return "native-ok"; }); + + try { + var source = new ScriptedTurnSource { SupportsNativeTools = true }; + source.EnqueueTurn(_ => new[] { + ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = "calling tool", + ToolCalls = new List { + new() { Id = "call-1", Name = "mcp_test-server_nativeTool", ArgumentsJson = "{\"input\":\"v\"}" } + } + }) + }); + source.EnqueueTurn(_ => new[] { + ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = "all done now" }) + }); + + var yielded = new List(); + await foreach (var item in LlmToolLoop.RunAsync(source, null, ToolCtx(), Meta(), null, CancellationToken.None)) { + yielded.Add(item); + } + + Assert.Equal(2, source.ReceivedUserContents.Count); + Assert.Null(source.ReceivedUserContents[1]); + Assert.Single(source.CommittedResults); + Assert.Equal("native-ok", source.CommittedResults[0][0].Result); + Assert.Equal("call-1", source.CommittedResults[0][0].ToolCallId); + } finally { + McpToolHelper.RegisterExternalTools( + new List<(string, McpToolHelper.ExternalToolInfo)>(), + async (server, tool, args) => { await Task.CompletedTask; return string.Empty; }); + } + } + + [Fact] + public async Task IterationLimit_BuildsContinuationSnapshot() { + McpToolHelper.RegisterExternalTools( + new List<(string, McpToolHelper.ExternalToolInfo)> { + ("test-server", new McpToolHelper.ExternalToolInfo { + ServerName = "test-server", + ToolName = "endlessTool", + Description = "endless", + Parameters = new List() + }) + }, + async (server, tool, args) => { await Task.CompletedTask; return "still running"; }); + + var oldCycles = Env.MaxToolCycles; + Env.MaxToolCycles = 2; + try { + var source = new ScriptedTurnSource { SupportsNativeTools = true }; + for (var i = 0; i < 3; i++) { + var callId = $"call-{i}"; + source.EnqueueTurn(_ => new[] { + ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = "working", + ToolCalls = new List { + new() { Id = callId, Name = "mcp_test-server_endlessTool", ArgumentsJson = "{}" } + } + }) + }); + } + + var executionContext = new LlmExecutionContext(); + await foreach (var item in LlmToolLoop.RunAsync(source, null, ToolCtx(), Meta(), executionContext, CancellationToken.None)) { + } + + Assert.True(executionContext.IterationLimitReached); + var snapshot = executionContext.SnapshotData; + Assert.NotNull(snapshot); + Assert.Equal(2, snapshot.CyclesSoFar); + Assert.Equal("Test", snapshot.Provider); + Assert.Equal(9, snapshot.ChannelId); + Assert.Equal(2, source.CommittedResults.Count); + } finally { + Env.MaxToolCycles = oldCycles; + McpToolHelper.RegisterExternalTools( + new List<(string, McpToolHelper.ExternalToolInfo)>(), + async (server, tool, args) => { await Task.CompletedTask; return string.Empty; }); + } + } + + [Fact] + public async Task ResumeMode_YieldsOnlyNewContentButSnapshotsFullContent() { + var source = new ScriptedTurnSource(); + source.EnqueueTurn(_ => new[] { new LlmStreamEvent.TextDelta(" continued text") }); + source.EnqueueTurn(_ => new[] { + ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = "continued text" }) + }); + + var meta = Meta(); + meta.BaseCycles = 3; + meta.InitialContent = "old content"; + var executionContext = new LlmExecutionContext(); + var yielded = new List(); + await foreach (var item in LlmToolLoop.RunAsync(source, null, ToolCtx(), meta, executionContext, CancellationToken.None)) { + yielded.Add(item); + } + + Assert.All(yielded, y => Assert.DoesNotContain("old content", y)); + Assert.Contains("continued text", yielded[0]); + } + } +} From 9f597dfcc65bf04bd8505741e780bf1a2ceae0f7 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 13:57:27 +0800 Subject: [PATCH 07/30] =?UTF-8?q?docs:=20document=20=E9=A2=84=E5=88=B6?= =?UTF-8?q?=E6=B8=A0=E9=81=93=20command=20in=20Bot=5FCommands=5FUser=5FGui?= =?UTF-8?q?de?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Docs/Bot_Commands_User_Guide.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Docs/Bot_Commands_User_Guide.md b/Docs/Bot_Commands_User_Guide.md index 387626c5..64efde7d 100644 --- a/Docs/Bot_Commands_User_Guide.md +++ b/Docs/Bot_Commands_User_Guide.md @@ -147,6 +147,13 @@ * **LLM渠道配置** (在群组中由全局管理员发送,会触发交互式配置流程) * **说明**: 以下指令用于管理LLM API渠道。它们会启动一个多步骤的对话流程(状态机),机器人会依次询问配置所需的各项信息。请按照机器人的提示逐步回复。 + * **预制渠道**:从内置 Provider 预设目录一步创建渠道(Anthropic、OpenAI Chat/Responses、Gemini、MiniMax、Ollama、LM Studio、DeepSeek、Moonshot 等常见提供商已预置默认地址与默认模型列表)。交互流程: + 1. 管理员发送: `预制渠道` + 2. 机器人回复: 预设列表(含默认地址与默认模型) + 3. 管理员发送预设编号 + 4. 机器人回复: `请输入 <预设名> 的 API Key(本地/免 Key 服务发送 - 跳过):` + 5. 管理员发送 API Key(或 `-`) + 6. 机器人回复: `渠道创建成功(ID: x)`,并自动预置默认模型;自定义 endpoint / 模型仍可用 `新建渠道` / `添加模型` 修改。 * **例如,使用 `新建渠道` 指令的交互流程可能如下:** 1. 管理员发送: `新建渠道` 2. 机器人回复: `请输入渠道的名称` From c16738dac47f3bf7bb7b56538766ba07c89909ec Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 17:25:52 +0800 Subject: [PATCH 08/30] chore: ignore .pi agent session state --- .gitignore | 739 +++++++++++++++++++++++++++-------------------------- 1 file changed, 371 insertions(+), 368 deletions(-) diff --git a/.gitignore b/.gitignore index 3cd3fb13..fa86a60b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,368 +1,371 @@ -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. -## -## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore - -# User-specific files -*.rsuser -*.suo -*.user -*.userosscache -*.sln.docstates - -# User-specific files (MonoDevelop/Xamarin Studio) -*.userprefs - -# Mono auto generated files -mono_crash.* - -# Build results -[Dd]ebug/ -[Dd]ebugPublic/ -[Rr]elease/ -[Rr]eleases/ -x64/ -x86/ -[Aa][Rr][Mm]/ -[Aa][Rr][Mm]64/ -bld/ -[Bb]in/ -[Oo]bj/ -[Ll]og/ -[Ll]ogs/ - -# Visual Studio 2015/2017 cache/options directory -.vs/ -# Uncomment if you have tasks that create the project's static files in wwwroot -#wwwroot/ - -# Visual Studio 2017 auto generated files -Generated\ Files/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -# NUnit -*.VisualState.xml -TestResult.xml -nunit-*.xml - -# Build Results of an ATL Project -[Dd]ebugPS/ -[Rr]eleasePS/ -dlldata.c - -# Benchmark Results -BenchmarkDotNet.Artifacts/ - -# .NET Core -project.lock.json -project.fragment.lock.json -artifacts/ - -# StyleCop -StyleCopReport.xml - -# Files built by Visual Studio -*_i.c -*_p.c -*_h.h -*.ilk -*.meta -*.obj -*.iobj -*.pch -*.pdb -*.ipdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*_wpftmp.csproj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.svclog -*.scc - -# Chutzpah Test files -_Chutzpah* - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opendb -*.opensdf -*.sdf -*.cachefile -*.VC.db -*.VC.VC.opendb - -# Visual Studio profiler -*.psess -*.vsp -*.vspx -*.sap - -# Visual Studio Trace Files -*.e2e - -# TFS 2012 Local Workspace -$tf/ - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper -*.DotSettings.user - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# AxoCover is a Code Coverage Tool -.axoCover/* -!.axoCover/settings.json - -# Visual Studio code coverage results -*.coverage -*.coveragexml - -# NCrunch -_NCrunch_* -.*crunch*.local.xml -nCrunchTemp_* - -# MightyMoose -*.mm.* -AutoTest.Net/ - -# Web workbench (sass) -.sass-cache/ - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.[Pp]ublish.xml -*.azurePubxml -# Note: Comment the next line if you want to checkin your web deploy settings, -# but database connection strings (with potential passwords) will be unencrypted -# *.pubxml -*.publishproj - -# Microsoft Azure Web App publish settings. Comment the next line if you want to -# checkin your Azure Web App publish settings, but sensitive information contained -# in these scripts will be unencrypted -PublishScripts/ - -# NuGet Packages -*.nupkg -# NuGet Symbol Packages -*.snupkg -# The packages folder can be ignored because of Package Restore -**/[Pp]ackages/* -# except build/, which is used as an MSBuild target. -!**/[Pp]ackages/build/ -# Uncomment if necessary however generally it will be regenerated when needed -#!**/[Pp]ackages/repositories.config -# NuGet v3's project.json files produces more ignorable files -*.nuget.props -*.nuget.targets - -# Microsoft Azure Build Output -csx/ -*.build.csdef - -# Microsoft Azure Emulator -ecf/ -rcf/ - -# Windows Store app package directories and files -AppPackages/ -BundleArtifacts/ -Package.StoreAssociation.xml -_pkginfo.txt -*.appx -*.appxbundle -*.appxupload - -# Visual Studio cache files -# files ending in .cache can be ignored -*.[Cc]ache -# but keep track of directories ending in .cache -!?*.[Cc]ache/ - -# Others -ClientBin/ -~$* -*~ -*.dbmdl -*.dbproj.schemaview -*.jfm -*.pfx -*.publishsettings -orleans.codegen.cs - -# Including strong name files can present a security risk -# (https://github.com/github/gitignore/pull/2483#issue-259490424) -#*.snk - -# Since there are multiple workflows, uncomment next line to ignore bower_components -# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) -#bower_components/ - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file -# to a newer Visual Studio version. Backup files are not needed, -# because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm -ServiceFabricBackup/ -*.rptproj.bak - -# SQL Server files -*.mdf -*.ldf -*.ndf - -# Business Intelligence projects -*.rdl.data -*.bim.layout -*.bim_*.settings -*.rptproj.rsuser -*- [Bb]ackup.rdl -*- [Bb]ackup ([0-9]).rdl -*- [Bb]ackup ([0-9][0-9]).rdl - -# Microsoft Fakes -FakesAssemblies/ - -# GhostDoc plugin setting file -*.GhostDoc.xml - -# Node.js Tools for Visual Studio -.ntvs_analysis.dat -node_modules/ - -# Visual Studio 6 build log -*.plg - -# Visual Studio 6 workspace options file -*.opt - -# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) -*.vbw - -# Visual Studio LightSwitch build output -**/*.HTMLClient/GeneratedArtifacts -**/*.DesktopClient/GeneratedArtifacts -**/*.DesktopClient/ModelManifest.xml -**/*.Server/GeneratedArtifacts -**/*.Server/ModelManifest.xml -_Pvt_Extensions - -# Paket dependency manager -.paket/paket.exe -paket-files/ - -# FAKE - F# Make -.fake/ - -# CodeRush personal settings -.cr/personal - -# Python Tools for Visual Studio (PTVS) -__pycache__/ -*.pyc - -# Cake - Uncomment if you are using it -# tools/** -# !tools/packages.config - -# Tabs Studio -*.tss - -# Telerik's JustMock configuration file -*.jmconfig - -# BizTalk build output -*.btp.cs -*.btm.cs -*.odx.cs -*.xsd.cs - -# OpenCover UI analysis results -OpenCover/ - -# Azure Stream Analytics local run output -ASALocalRun/ - -# MSBuild Binary and Structured Log -*.binlog - -# NVidia Nsight GPU debugger configuration file -*.nvuser - -# MFractors (Xamarin productivity tool) working folder -.mfractor/ - -# Local History for Visual Studio -.localhistory/ - -# BeatPulse healthcheck temp database -healthchecksdb - -# Backup folder for Package Reference Convert tool in Visual Studio 2017 -MigrationBackup/ - -# Ionide (cross platform F# VS Code tools) working folder -.ionide/ - -tessdata/ -/TelegramSearchBot/Properties/launchSettings.json - -example/ -.VsCodeCounter/ - -.sisyphus - -# Moder.Update Rust build artifacts -external/moder-update/**/target/ - -# Coding agent sidecar build artifacts -/TelegramSearchBot.CodingAgentSidecar/target/ - -# Moder.Update C# build artifacts -external/moder-update/**/bin/ -external/moder-update/**/obj/ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +# *.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +tessdata/ +/TelegramSearchBot/Properties/launchSettings.json + +example/ +.VsCodeCounter/ + +.sisyphus + +# Moder.Update Rust build artifacts +external/moder-update/**/target/ + +# Coding agent sidecar build artifacts +/TelegramSearchBot.CodingAgentSidecar/target/ + +# Moder.Update C# build artifacts +external/moder-update/**/bin/ +external/moder-update/**/obj/ + +# pi agent session state +.pi/ From fb29ba88bffa02a29f93b441d850f511141e3ed5 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 17:34:51 +0800 Subject: [PATCH 09/30] feat(llm): opencode go/zen presets; shared LlmHistoryQueryService skeleton + ExecWithHistoryAsync interface --- .../Interface/AI/LLM/ILLMService.cs | 13 ++ .../Service/AI/LLM/LlmHistoryQueryService.cs | 170 ++++++++++++++++++ .../Service/AI/LLM/LlmProviderCatalog.cs | 17 +- TelegramSearchBot/Model/AI/LLMConfState.cs | 3 + .../Service/Manage/EditLLMConfService.cs | 38 +++- 5 files changed, 234 insertions(+), 7 deletions(-) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs index 55732c73..54734d27 100644 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs @@ -59,6 +59,19 @@ public IAsyncEnumerable ExecAsync(Message message, long ChatId, string m return ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken); } + /// + /// Execute with caller-provided history rows (LLMAgent worker path). The rows already + /// include the input message; implementations must not touch the database for history. + /// Default falls back to the legacy self-loading path. + /// + public IAsyncEnumerable ExecWithHistoryAsync( + IReadOnlyList history, + Message message, long ChatId, string modelName, LLMChannel channel, + LLMApiBinding binding, LlmExecutionContext executionContext, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + return ExecAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken); + } + public IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, LLMApiBinding binding, LlmExecutionContext executionContext, diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs new file mode 100644 index 00000000..efb8f749 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs @@ -0,0 +1,170 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; +using StackExchange.Redis; +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; +using DataMessage = TelegramSearchBot.Model.Data.Message; + +namespace TelegramSearchBot.Service.AI.LLM { + /// + /// Loads recent chat history as neutral rows (batched + /// user-name and extension lookups). This is the single DB pipeline for LLM history; + /// per-provider projections convert rows to their native message types without touching + /// the database. Previously duplicated (with per-message N+1 lookups) in each provider. + /// + [Injectable(Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton)] + public class LlmHistoryQueryService : IService { + private readonly DataDbContext _dbContext; + private readonly LlmVisibilityService? _llmVisibilityService; + + public string ServiceName => nameof(LlmHistoryQueryService); + + public LlmHistoryQueryService(DataDbContext dbContext, LlmVisibilityService? llmVisibilityService = null) { + _dbContext = dbContext; + _llmVisibilityService = llmVisibilityService; + } + + /// Rows ordered chronologically; the input message is appended last (when visible), + /// matching the legacy per-provider GetChatHistory behavior. + public async Task> LoadAsync(long chatId, DataMessage? inputMessage = null, CancellationToken cancellationToken = default) { + var messages = await _dbContext.Messages.AsNoTracking() + .Where(m => m.GroupId == chatId && m.DateTime > DateTime.UtcNow.AddHours(-1)) + .OrderBy(m => m.DateTime) + .ToListAsync(cancellationToken); + + if (messages.Count < 10) { + messages = await _dbContext.Messages.AsNoTracking() + .Where(m => m.GroupId == chatId) + .OrderByDescending(m => m.DateTime) + .Take(10) + .OrderBy(m => m.DateTime) + .ToListAsync(cancellationToken); + } + + if (_llmVisibilityService != null) { + messages = await _llmVisibilityService.FilterVisibleMessagesAsync(chatId, messages, cancellationToken); + } + + var users = await ResolveUsersAsync(messages, cancellationToken); + var extensionsByMessageId = await ResolveExtensionsAsync(messages, cancellationToken); + + if (inputMessage != null && + ( _llmVisibilityService == null || + !await _llmVisibilityService.IsUserInvisibleAsync(chatId, inputMessage.FromUserId, cancellationToken) )) { + messages.Add(inputMessage); + var inputExtensions = await _dbContext.MessageExtensions.AsNoTracking() + .Where(x => x.MessageDataId == inputMessage.Id) + .ToListAsync(cancellationToken); + extensionsByMessageId[inputMessage.Id] = inputExtensions + .Select(e => new AgentMessageExtensionSnapshot { Name = e.Name, Value = e.Value }) + .ToList(); + if (!users.ContainsKey(inputMessage.FromUserId)) { + var inputUser = await _dbContext.UserData.AsNoTracking() + .FirstOrDefaultAsync(u => u.Id == inputMessage.FromUserId, cancellationToken); + if (inputUser != null) { + users[inputMessage.FromUserId] = inputUser; + } + } + } + + return messages.Select(message => { + users.TryGetValue(message.FromUserId, out var user); + extensionsByMessageId.TryGetValue(message.Id, out var messageExtensions); + return new AgentHistoryMessage { + DataId = message.Id, + DateTime = message.DateTime, + GroupId = message.GroupId, + MessageId = message.MessageId, + FromUserId = message.FromUserId, + ReplyToUserId = message.ReplyToUserId, + ReplyToMessageId = message.ReplyToMessageId, + Content = message.Content ?? string.Empty, + User = new AgentUserSnapshot { + UserId = user?.Id ?? message.FromUserId, + FirstName = user?.FirstName ?? string.Empty, + LastName = user?.LastName ?? string.Empty, + UserName = user?.UserName ?? string.Empty, + IsBot = user?.IsBot, + IsPremium = user?.IsPremium + }, + Extensions = messageExtensions ?? [] + }; + }).ToList(); + } + + private async Task> ResolveUsersAsync(List messages, CancellationToken cancellationToken) { + var userIds = messages.Select(x => x.FromUserId).Distinct().ToList(); + if (userIds.Count == 0) { + return new Dictionary(); + } + return await _dbContext.UserData.AsNoTracking() + .Where(x => userIds.Contains(x.Id)) + .ToDictionaryAsync(x => x.Id, cancellationToken); + } + + private async Task>> ResolveExtensionsAsync(List messages, CancellationToken cancellationToken) { + var messageIds = messages.Select(x => x.Id).ToList(); + if (messageIds.Count == 0) { + return new Dictionary>(); + } + var extensionRecords = await _dbContext.MessageExtensions.AsNoTracking() + .Where(x => messageIds.Contains(x.MessageDataId)) + .ToListAsync(cancellationToken); + return extensionRecords + .GroupBy(x => x.MessageDataId) + .ToDictionary( + x => x.Key, + x => x.Select(e => new AgentMessageExtensionSnapshot { + Name = e.Name, + Value = e.Value + }).ToList()); + } + } + + /// + /// Loads the photo attached to a message from the shared Photos directory as PNG bytes. + /// Single copy of the loader previously duplicated in every provider (filesystem-based, + /// so it works identically in the main process and LLMAgent worker processes). + /// + public static class LlmPhotoLoader { + public static byte[]? TryLoadMessagePhoto(long chatId, long messageId, Microsoft.Extensions.Logging.ILogger? logger = null) { + try { + var dirPath = System.IO.Path.Combine(Env.WorkDir, "Photos", $"{chatId}"); + if (!System.IO.Directory.Exists(dirPath)) return null; + + var files = System.IO.Directory.GetFiles(dirPath, $"{messageId}.*"); + if (files.Length == 0) return null; + + using var fileStream = System.IO.File.OpenRead(files[0]); + var bitmap = SkiaSharp.SKBitmap.Decode(fileStream); + if (bitmap == null) return null; + + var encoded = bitmap.Encode(SkiaSharp.SKEncodedImageFormat.Png, 90); + return encoded?.ToArray(); + } catch (Exception ex) { + logger?.LogDebug(ex, "无法加载消息图片: ChatId={ChatId}, MessageId={MessageId}", chatId, messageId); + return null; + } + } + } +} + +namespace TelegramSearchBot.Service.AI.LLM { + /// Shared sender-name formatting for history projections. + public static class LlmHistoryFormat { + public static string FormatSender(AgentUserSnapshot user, long fromUserId) { + if (fromUserId == 0) return "System/Unknown"; + var name = $"{user?.FirstName} {user?.LastName}".Trim(); + return string.IsNullOrEmpty(name) ? $"User({fromUserId})" : name; + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs index 49b56bfb..cbb68e6f 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs @@ -9,7 +9,8 @@ public sealed record LlmProviderPreset( string Id, string DisplayName, LLMProvider Provider, - string DefaultGateway, + /// Null = gateway must be entered during creation (e.g. user-owned OpenCode Go gateway). + string? DefaultGateway, string[] DefaultModels, bool RequiresApiKey, string? Notes = null); @@ -68,6 +69,18 @@ public static class LlmProviderCatalog { "https://api.moonshot.cn/v1", new[] { "kimi-k2-0711-preview", "moonshot-v1-128k" }, RequiresApiKey: true), + new LlmProviderPreset( + "opencode-zen", "OpenCode Zen (官方订阅目录)", LLMProvider.Anthropic, + "https://opencode.ai/zen", + Array.Empty(), + RequiresApiKey: true, + Notes: "Anthropic 兼容;订阅 token 作为 API Key;目录模型不自动创建,授权模型请用 添加模型 手工维护。"), + new LlmProviderPreset( + "opencode-go", "OpenCode Go (自建网关)", LLMProvider.Anthropic, + null, + Array.Empty(), + RequiresApiKey: true, + Notes: "创建时需输入你的 OpenCode Go 网关地址;Anthropic 兼容,自动携带 x-opencode-session 头;模型请用 添加模型 维护。"), }; public static LlmProviderPreset? FindById(string id) => @@ -78,7 +91,7 @@ public static string FormatMenu() { var sb = new System.Text.StringBuilder("请选择要创建的渠道预设:\n"); for (var i = 0; i < Presets.Count; i++) { var p = Presets[i]; - sb.AppendLine($"{i + 1}. {p.DisplayName} ({p.DefaultGateway})"); + sb.AppendLine($"{i + 1}. {p.DisplayName} ({p.DefaultGateway ?? "创建时输入"})"); if (p.DefaultModels.Length > 0) { sb.AppendLine($" 默认模型: {string.Join(", ", p.DefaultModels)}"); } diff --git a/TelegramSearchBot/Model/AI/LLMConfState.cs b/TelegramSearchBot/Model/AI/LLMConfState.cs index 6536e5e2..1734008a 100644 --- a/TelegramSearchBot/Model/AI/LLMConfState.cs +++ b/TelegramSearchBot/Model/AI/LLMConfState.cs @@ -20,6 +20,9 @@ public enum LLMConfState { [Description("awaiting_preset_selection")] AwaitingPresetSelection, + [Description("awaiting_preset_gateway")] + AwaitingPresetGateway, + [Description("awaiting_preset_apikey")] AwaitingPresetApiKey, diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs index cff017ba..16af20cb 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs @@ -51,6 +51,7 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService { { LLMConfState.AwaitingName.GetDescription(), HandleAwaitingNameAsync }, { LLMConfState.AwaitingPresetSelection.GetDescription(), HandleAwaitingPresetSelectionAsync }, + { LLMConfState.AwaitingPresetGateway.GetDescription(), HandleAwaitingPresetGatewayAsync }, { LLMConfState.AwaitingPresetApiKey.GetDescription(), HandleAwaitingPresetApiKeyAsync }, { LLMConfState.AwaitingGateway.GetDescription(), HandleAwaitingGatewayAsync }, { LLMConfState.AwaitingProvider.GetDescription(), HandleAwaitingProviderAsync }, @@ -105,8 +106,14 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService } var preset = LlmProviderCatalog.Presets[presetIndex - 1]; + if (preset.DefaultGateway == null) { + await redis.SetDataAsync(preset.Id); + await redis.SetStateAsync(LLMConfState.AwaitingPresetGateway.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的网关地址:"); + } + if (!preset.RequiresApiKey) { - return await CreatePresetChannelAsync(redis, preset, string.Empty); + return await CreatePresetChannelAsync(redis, preset, string.Empty, preset.DefaultGateway); } await redis.SetDataAsync(preset.Id); @@ -114,21 +121,42 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService return (true, $"请输入 {preset.DisplayName} 的 API Key(本地/免 Key 服务发送 - 跳过):"); } - private async Task<(bool, string)> HandleAwaitingPresetApiKeyAsync(EditLLMConfRedisHelper redis, string command) { + private async Task<(bool, string)> HandleAwaitingPresetGatewayAsync(EditLLMConfRedisHelper redis, string command) { var preset = LlmProviderCatalog.FindById(await redis.GetDataAsync()); if (preset == null) { await redis.DeleteKeysAsync(); return (false, "预设状态丢失,请重新发送 预制渠道"); } + var gateway = command.Trim(); + if (!Uri.TryCreate(gateway, UriKind.Absolute, out var uri) || + ( uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps )) { + return (false, "请输入有效的网关地址(以 http:// 或 https:// 开头)"); + } + + await redis.SetDataAsync($"{preset.Id}|{gateway}"); + await redis.SetStateAsync(LLMConfState.AwaitingPresetApiKey.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 API Key(订阅 token):"); + } + + private async Task<(bool, string)> HandleAwaitingPresetApiKeyAsync(EditLLMConfRedisHelper redis, string command) { + var data = await redis.GetDataAsync() ?? string.Empty; + var parts = data.Split('|', 2); + var preset = LlmProviderCatalog.FindById(parts[0]); + if (preset == null) { + await redis.DeleteKeysAsync(); + return (false, "预设状态丢失,请重新发送 预制渠道"); + } + var apiKey = command.Trim().Equals("-", StringComparison.OrdinalIgnoreCase) ? string.Empty : command.Trim(); - return await CreatePresetChannelAsync(redis, preset, apiKey); + var gateway = parts.Length > 1 ? parts[1] : preset.DefaultGateway!; + return await CreatePresetChannelAsync(redis, preset, apiKey, gateway); } - private async Task<(bool, string)> CreatePresetChannelAsync(EditLLMConfRedisHelper redis, LlmProviderPreset preset, string apiKey) { + private async Task<(bool, string)> CreatePresetChannelAsync(EditLLMConfRedisHelper redis, LlmProviderPreset preset, string apiKey, string gateway) { var channelId = await Helper.AddChannel( preset.DisplayName, - preset.DefaultGateway, + gateway, apiKey, preset.Provider, 1, From fad9c574d5c9c9495851d92eb7108b6e9c85821b Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 17:37:22 +0800 Subject: [PATCH 10/30] docs: rewrite strategy for LLM module refactor (big-bang, data migration mapping) --- Docs/Agent_Architecture_Refactor_Plan.md | 239 +++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 Docs/Agent_Architecture_Refactor_Plan.md diff --git a/Docs/Agent_Architecture_Refactor_Plan.md b/Docs/Agent_Architecture_Refactor_Plan.md new file mode 100644 index 00000000..556e7707 --- /dev/null +++ b/Docs/Agent_Architecture_Refactor_Plan.md @@ -0,0 +1,239 @@ +# LLM/Agent 架构重构方案 + +> 目标:把 2024 年式「每 provider 一个上帝类」的旧架构,**一次性整体重写**成现代 agent 架构(参考 pi / pi-ai 的分层),不在旧代码上打补丁。 +> 数据库数据必须正确迁移:表结构允许变,数据不能丢。 +> 本文档是后续工作的总蓝图(PR #390 已包含阶段 P1/P2,P3 改为整体重写的一部分)。 + +--- + +## 一、现状盘点(截至本文档) + +### 已完成(在 PR #390 分支上) + +| 阶段 | 内容 | 状态 | +|---|---|---| +| P1 共享循环 | `LlmToolLoop` + `ILlmTurnSource` + `LlmStreamEvent` 归一化事件;10 份工具循环 → 1 份 | ✅ 已提交,测试全过 | +| P2 渠道预设 | `LlmProviderCatalog` 11 家预置(含 OpenCode Zen/Go)+ `预制渠道` 指令 + 网关/Key 两步流 | ✅ 已提交 | +| P3 历史管线 | `LlmHistoryQueryService`(批量查询 → `AgentHistoryMessage` rows)+ `LlmPhotoLoader` + `ILLMService.ExecWithHistoryAsync` 接口默认实现 | ⚠️ 骨架已提交,**provider 未接入,worker 未切换** | + +### 遗留问题(重构动机) + +1. **5 个 provider 上帝类**(~7600 行):循环虽已共享,但每个 service 仍各自持有:投影、序列化/反序列化历史、工具转换、vision 检查、模型目录、DI 注册——接一个新渠道要动 8 处。 +2. **历史五道工序**:SQLite → Redis payload → worker 假 InMemory 库(`SeedTaskDataAsync`)→ 假库查询 → provider 类型。因为老接口契约是"给我消息对象,我自己查库"。 +3. **快照格式不统一**:SerializedChatMessage 丢 native tool_call id;OpenAI 用自己的序列化,其他用 tracked history。 +4. **双入口**:GeneralLLMController(旧直连)与 AgentChat(队列+worker)两套前端、两套"继续迭代"接线。 +5. **状态四存**:同一对话在 SQLite、Redis task-state、Redis snapshot、Redis chunks 四处各存一份、三种格式。 + +--- + +## 二、目标架构 + +``` +┌─────────────────────────────────────────────────────────────┐ +│ 宿主层(唯一入口) │ +│ Telegram 群消息 → AgentChat 触发(唯一入口,删旧直连路径) │ +│ 入队:LoadAsync 一次历史(纯 DTO)→ AgentExecutionTask │ +├─────────────────────────────────────────────────────────────┤ +│ Agent 层(一份循环,进程内) │ +│ AgentLoop.Run(request): │ +│ 归一化 LlmMessage 历史 → transport 流式回合 → 工具执行 │ +│ → 快照/续跑(schema v2 = 归一化历史)→ 迭代上限 │ +├─────────────────────────────────────────────────────────────┤ +│ Transport 层(每 API 方言一个适配器,纯转换,无状态) │ +│ ILlmTransport.StreamTurnAsync(LlmTurnRequest) → LlmStreamEvent*│ +│ openai-completions │ anthropic-messages │ openai-responses │ +│ │ google-generative-ai │ ollama │ +├─────────────────────────────────────────────────────────────┤ +│ Catalog 层(provider = 数据,非代码) │ +│ LlmProviderCatalog 预设(内置)+ LLMChannel/LLMApiBinding │ +│ (用户自定义,表结构不变) │ +├─────────────────────────────────────────────────────────────┤ +│ History 层(一次加载,纯 DTO) │ +│ LlmHistoryQueryService.LoadAsync → AgentHistoryMessage rows │ +│ 投影到归一化 LlmMessage:只发生一次,在入队侧 │ +└─────────────────────────────────────────────────────────────┘ +``` + +### 核心决策 + +| # | 决策 | 理由 | +|---|---|---| +| D1 | **归一化消息模型 `LlmMessage` 贯穿全程**(历史行投影一次 → 循环 → transport 请求 → 快照) | 根治 5×(Serialize/Deserialize)ProviderHistory 与快照丢 tool_call id;接新 provider 只写 transport | +| D2 | **provider service 类消亡**,变为 `LlmTransportRegistry` 按 `LlmProtocol` 分发 | 数据库表(LLMChannel/LLMApiBinding)本来就是 pi 的 Provider/Model 同构体,代码侧跟上 | +| D3 | **历史纯 DTO 贯通**:worker 不查库、不造假库,task.History 直接投影 | 删 `SeedTaskDataAsync`、删 4×投影重复、消灭"五道工序" | +| D4 | **vision 图片在投影时按行从共享磁盘加载**(`LlmPhotoLoader`),不进 Redis payload | 磁盘两进程共享;避免把图片字节塞进队列 | +| D5 | **单入口**:`GeneralLLMController` 的 LLM 执行路径并入 AgentChat 队列路径;`/llm` 类管理命令保留 | 删双份"继续迭代"接线 | +| D6 | **快照 schema v2**:`LlmContinuationSnapshot.ProviderHistory` → `List`(归一化),`SchemaVersion=2` | 旧快照 24h TTL 自然过期,无迁移负担 | +| D7 | **worker 进程保留但瘦身**(不取进程合并) | 崩溃隔离对无人值守群聊 bot 有真实价值;瘦身后再评估合并收益 | + +### 非目标(明确不做) + +- 不改数据库表结构(LLMChannel/LLMApiBinding/Message 均保持)。 +- 不做 pi 式 compaction(后续独立提案;当前每任务从 DB 重建历史的语义保留)。 +- 不动 OCR/ASR 多进程架构。 +- 不做 delta 级 Telegram 流式编辑(Telegram API 限制下全量快照重发是合理妥协)。 + +--- + +## 三、目标数据模型(Common 层) + +```csharp +// LlmMessage.cs —— 全程唯一的消息表示 +public sealed class LlmMessage { + public LlmRole Role; // System / User / Assistant / Tool + public string? Text; // 文本(Tool 角色 = 工具结果文本) + public string? Thinking; // reasoning(assistant) + public string? ToolCallId; // Tool 角色 + public List? ToolCalls; // assistant 发起的调用(归一化,含 id/name/args) + public byte[]? ImagePng; public string? ImageMediaType; // user 视觉输入 +} + +// LlmTurnRequest —— transport 单回合输入 +public sealed class LlmTurnRequest { + public string SystemPrompt; + public IReadOnlyList History; // 全量归一化历史 + public IReadOnlyList Tools; // null = 文本协议 + public LlmTransportConfig Config; // endpoint/apiKey/model/quirks(来自 channel+binding) +} +``` + +- `ILlmTransport` 保持 Wave 1 的 `ILlmTurnSource` 语义(StreamTurnAsync / Commit* / GetTrackedHistory → 改为 GetHistory → `List`)。 +- 快照 v2:`ProviderHistory: List` + `SchemaVersion = 2`;resume 时循环直接从归一化历史继续。 + +--- + +## 四、执行策略:整体重写,不共存 + +**不做「新旧接口并存、逐个迁移」的渐进路线。** 一次性新建全部新类型(模型 / 投影 / 循环 / transport / registry),切换所有调用点,然后**删除全部旧代码**(5 个 service 类、ILLMService 旧接口、LLMFactory、turn source 内嵌类)。保留的只有正交基础设施:McpToolHelper(工具注册/执行)、ModelCapabilityService(能力刷新)、OpencodeSessionHeaders、LlmBindingSupport、PromptCachingHelper、LlmVisibilityService。 + +### 数据迁移映射(表结构保持,语义即迁移) + +现有表结构与目标模型同构,无需改表、无需 EF 迁移,数据零丢失: + +| 现有数据 | 新架构语义 | 迁移方式 | +|---|---|---| +| `LLMChannel`(Name/Gateway/ApiKey/Provider/Parallel/Priority) | Provider(连接 + 凭证) | 原样读取 | +| `LLMApiBinding`(Endpoint/Protocol/AuthProfile/IsDefault) | Transport 选择(LlmProtocol → transport id)+ 认证方式 | 原样读取;`LlmProtocol` 枚举值不变 | +| `ChannelWithModel`(ModelName/AuthorizationSource/ApiBindingId) | 模型目录(授权集合) | 原样读取 | +| `ModelCapabilities`(CapabilityName/Value,如 vision=true) | 模型能力 | 原样读取(vision/函数调用等名字不变) | +| `Message`/`MessageExtension` | 聊天历史源 | 原样读取(LoadAsync 不变) | +| Redis in-flight 快照(旧 schema v1) | — | 24h TTL 自然过期,重启后作废;不迁移 | +| `GroupSettings.LLMModelName`(群默认模型) | 群 → 模型路由 | 原样读取 | + +迁移验证:对现有库跑「旧路径 vs 新路径」同输入投影对比(Phase 3 的投影单测兼做此职),再加一条渠道数据 round-trip 测试(现有 DB 样本 → 新 registry → 能解析出 transport + 模型列表)。 + +--- + +## 五、实施阶段(修订) + +### Phase 3:历史纯 DTO 贯通(进行中,本 PR 内完成) + +**范围** +1. `LlmHistoryQueryService.LoadAsync` 已就绪 → 4 个 provider 的 `GetChatHistory` 改为「LoadAsync + 投影」: + - 投影循环保留(分组规则/回复格式微差),但名字/扩展取自 row(消除 N+1 查询),图片走 `LlmPhotoLoader`。 +2. 每个服务新增 `ExecWithHistoryAsync(rows, message, ..., supportsVision, ...)`:即现 glue 但 `ProjectRows(rows)` 替代 `GetChatHistory` 调用。 +3. `LlmServiceProxy`:删 `SeedTaskDataAsync`;`task.History + 输入行` → 直接调 `ExecWithHistoryAsync`;`supportsVision` 从 `task.Channel.Capabilities` 计算。 +4. worker 的 `UseInMemoryDatabase` 保留注册(服务构造函数注入需要),但**零播种**。 + +**验收** +- 单元测试:`ProjectRows` 各 provider 快照比对(OpenAI/Anthropic/Gemini/Responses 各 1 例:给定 rows → 断言投影文本)。 +- worker 路径集成测试:构造 task(含 history rows)→ proxy → 断言不触碰 DbContext。 +- 全量测试绿 + solution 0 error。 + +**删除量**:`SeedTaskDataAsync` ~110 行;4×DB 查询管线 ~200 行;N+1 查询消除。 + +### Phase 4:归一化消息模型 + transport 独立(新 PR 或本 PR 追加,视评审节奏) + +**范围** +1. `LlmMessage`/`LlmToolCall`/`LlmToolSpec` 落地 Common;`AgentHistoryMessage` → `LlmMessage` 投影一次成型(`LlmHistoryProjector`,含 vision 图片加载)。 +2. Wave 1 的 turn source 类从 service 内嵌迁出为 `Transports/` 下独立类,输入改为 `LlmTurnRequest`(归一化历史),删除各自 `GetTrackedHistory`(快照直接用归一化历史)。 +3. `SerializeProviderHistory`/`DeserializeProviderHistory`/`EnsureAlternatingRoles`/`PrepareMessagesForPromptCaching` 收编进各自 transport(外部不再可见)。 +4. 快照 v2 切换:循环写入 `LlmMessage` 历史;`ResumeFromSnapshot` 统一为一个共享实现。 +5. `ILLMService` 瘦身为 `ExecAsync(LlmAgentRequest)` + 目录方法(GetAllModels/GetAllModelsWithCapabilities/GenerateEmbeddings/AnalyzeImage/IsHealthy);`LLMFactory` → `LlmTransportRegistry.GetByProtocol(LlmProtocol)`。 + +**验收** +- 快照 round-trip 单测:native 工具调用 → 快照 → resume,断言 tool_call id 不丢。 +- 5 transport 各 1 个「请求构造」单测(不触网,断言 SDK 请求对象字段)。 +- 每迁移一个 transport 跑全量测试。 + +**删除量预估**:序列化/投影重复 ~600 行;服务类 glue ~800 行。 + +### Phase 3+4 合并:一次重写(本 PR 内完成) + +按依赖顺序单次落地,旧代码在最后一步整体删除: + +**第 1 步 — 新核心模型(Common/Model/AI/Llm/)** +- `LlmMessage`(Role/Text/Thinking/ToolCalls/ToolCallId/ImagePng)、`LlmToolCall`、`LlmToolSpec` +- `LlmTurnRequest`(SystemPrompt + LlmMessage 历史 + Tools + `LlmTransportConfig`) +- `LlmAgentRequest`(顶层执行请求:chatId/userId/messageId/model/channel/binding/rows/input) + +**第 2 步 — 历史投影(一次成型)** +- `LlmHistoryProjector`: `AgentHistoryMessage rows → List`(vision 图片按行经 `LlmPhotoLoader` 加载) +- 替代 4×投影 + 4×Serialize/DeserializeProviderHistory + EnsureAlternatingRoles(归一化层处理 user/assistant 交替约束) + +**第 3 步 — 循环升级** +- `LlmToolLoop` 改为直接消费 `List`(快照 v2 = 归一化历史,`SchemaVersion=2`) +- resume 共享实现一份 + +**第 4 步 — Transport(5 个,从 Wave 1 turn source 移植)** +- `Transports/OpenAiChatTransport.cs`、`AnthropicMessagesTransport.cs`、`OpenAiResponsesTransport.cs`、`GoogleGenerativeAiTransport.cs`、`OllamaTransport.cs` +- 输入 `LlmTurnRequest`(归一化历史),prompt caching / SDK quirk 全部收编内部 +- 附加能力接口(可选实现):`ILlmModelCatalog`(GetAllModels/WithCapabilities)、`ILlmEmbeddings`、`ILlmImageAnalysis` + +**第 5 步 — Registry + 删旧** +- `LlmTransportRegistry.GetByProtocol(LlmProtocol)` / `GetByProvider(LLMProvider)`(替代 LLMFactory) +- 删除:5 个 service 类、`ILLMService`、`LLMFactory`、内嵌 turn source、`LlmContinuationSupport`、各序列化方法 +- 调用点切换(全部一次性):`GeneralLLMController`、`GeneralLLMService`、`CodingAgentReportConsumer`、`LlmServiceProxy`(删 SeedTaskDataAsync)、`FaissVectorService`(embeddings)、`RefreshService`/`ModelCapabilityService`(目录)、`LLMOCRService`/`AltPhotoController`(AnalyzeImage)、`LLMAgentProgram`(DI) + +**验收** +- 全量测试绿 + solution 0 error(旧类型全删后编译即证明无残留引用) +- 投影对比测试(现库数据 → 旧格式输出快照固化 vs 新投影) +- 快照 round-trip 测试(native tool_call id 不丢) +- worker 路径:task.History 直达 transport,零 DB 触碰(以 mock DbContext 断言) + +### Phase 5:单入口 + 状态收敛(独立 PR) + +**范围** +1. `GeneralLLMController` 的 LLM 执行路径改为走 AgentChat 队列(`EnqueueSyntheticMessageTaskAsync` 已具备同等能力);管理命令(`选择模型` 等)保留原位。 +2. 删 `LLMIterationCallbackController` 双份接线 → 一个 continuation 流程。 +3. Redis task-state hash 精简:不再重复存完整 payload(BRPOP 消费后状态只留 status/时间戳;恢复场景用 payload 单一来源)。 +4. `ChunkPollingService`/快照协议文档化(opencode 线协议已有测试,补充 agent 循环侧)。 + +**验收** +- `/llm` 与 Agent 聊天两条触发路径端到端手测清单。 +- Redis 状态断言测试(入队→消费→状态字段集)。 + +### Phase 6(可选,独立提案):worker 进程合并评估 + +Phase 4 完成后 worker 里只剩「BRPOP + 调 transport」~200 行,届时重新评估: +- 合并进主进程:删 `AgentRegistryService`/`GarnetRpcClient`/心跳/看门狗(~700 行),代价是失去进程隔离。 +- 或保留现状(推荐默认,隔离价值真实)。 + +--- + +## 五、兼容性与风险 + +| 风险 | 缓解 | +|---|---| +| 投影行为漂移(prompt 格式变化影响模型行为) | Phase 3 投影单测按 provider 固化现有格式;格式宏差(Gemini ASCII 冒号等)逐字保留 | +| 快照 v2 与线上 in-flight 快照冲突 | 旧快照 24h TTL 自然过期;`SchemaVersion` 检查不匹配 → 提示重新开始 | +| worker `supportsVision` 计算来源变化 | `task.Channel.Capabilities` 与 DB ModelCapabilities 同源(`LoadChannelAsync` 已带),断言测试覆盖 | +| OpenCode 特殊头/目录语义 | `IsOpenCodeBinding` 识别与「目录≠授权」规则在 Phase 3/4 均不触碰,仅迁移载体 | +| 大 PR 评审难 | 整体重写按第 1-5 步分批提交(每步编译+测试绿),单 PR 内可逐步 review | + +--- + +## 六、执行顺序 + +``` +[PR #390 - 本分支] + P1 共享循环(已完成) + P2 预设(已完成) + P3+4 整体重写:LlmMessage → 投影 → 循环 v2 → 5 transports → registry → 删旧 ← 下一步 + +[PR #391] + P5 单入口 + Redis 状态收敛 + +[PR #392?] + P6 worker 合并评估(默认不动) +``` From 0690d0d90eac9ae538696d790d898f66ad4c4621 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Thu, 10 Sep 2026 18:17:48 +0800 Subject: [PATCH 11/30] refactor(llm): normalized LlmMessage model, shared history projector+query, stateless transports, v2 snapshots, pure-DTO worker path - LlmMessage/LlmToolCall/LlmToolSpec/LlmTurnRequest/LlmStreamEvent: single normalized model across history, loop, transports, and v2 snapshots - LlmHistoryQueryService.LoadAsync: one batched DB pipeline replacing 4 per-provider N+1 query loops - LlmHistoryProjector: unified rows->LlmMessage projection (grouping by bot/user flip, unified reply formatting) - 5 transports rebuilt stateless per turn (OpenAI chat/Anthropic messages/OpenAI responses/Gemini/Ollama); provider history serialization pairs deleted - LlmToolLoop owns normalized history; v2 continuation snapshots (NormalizedHistory) preserve native tool-call ids; resume is shared - LlmServiceProxy: pure-DTO path, SeedTaskDataAsync fake InMemory DB seeding deleted; vision resolved from task capabilities --- .../Model/AI/Llm/LlmMessage.cs | 100 ++ .../Model/AI/Llm/LlmTurnModels.cs | 57 +- .../Model/AI/LlmContinuationSnapshot.cs | 11 +- .../Service/AI/LLM/LlmToolLoopTests.cs | 190 +-- ...OpenAIProviderHistorySerializationTests.cs | 308 ---- .../Interface/AI/LLM/ILLMService.cs | 1 + .../Interface/AI/LLM/ILlmTurnSource.cs | 45 - .../Service/AI/LLM/AnthropicService.cs | 443 ++++-- .../Service/AI/LLM/GeminiService.cs | 175 ++- .../Service/AI/LLM/LlmHistoryProjector.cs | 80 + .../Service/AI/LLM/LlmHistoryQueryService.cs | 32 +- .../Service/AI/LLM/LlmToolLoop.cs | 112 +- .../Service/AI/LLM/LlmTransportTypes.cs | 42 + .../Service/AI/LLM/McpToolHelper.cs | 14 +- .../Service/AI/LLM/OllamaService.cs | 150 +- .../Service/AI/LLM/OpenAIResponsesService.cs | 1286 ++++++++--------- .../Service/AI/LLM/OpenAIService.cs | 681 ++++----- .../Service/LlmServiceProxy.cs | 71 +- 18 files changed, 1941 insertions(+), 1857 deletions(-) create mode 100644 TelegramSearchBot.Common/Model/AI/Llm/LlmMessage.cs delete mode 100644 TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIProviderHistorySerializationTests.cs delete mode 100644 TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryProjector.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs diff --git a/TelegramSearchBot.Common/Model/AI/Llm/LlmMessage.cs b/TelegramSearchBot.Common/Model/AI/Llm/LlmMessage.cs new file mode 100644 index 00000000..fd1ca7c3 --- /dev/null +++ b/TelegramSearchBot.Common/Model/AI/Llm/LlmMessage.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; + +namespace TelegramSearchBot.Model.AI { + /// Normalized message role across all API dialects. + public enum LlmRole { + System = 0, + User = 1, + Assistant = 2, + Tool = 3 + } + + /// A normalized tool call initiated by an assistant message. + public sealed class LlmToolCall { + public string Id { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public string ArgumentsJson { get; set; } = "{}"; + + /// Arguments as string dict (the canonical form used by McpToolHelper). + public Dictionary ParseArguments() { + if (string.IsNullOrWhiteSpace(ArgumentsJson)) { + return new Dictionary(StringComparer.OrdinalIgnoreCase); + } + try { + return JsonSerializer.Deserialize>(ArgumentsJson) + ?.ToDictionary(kv => kv.Key, kv => kv.Value.ToString()) + ?? new Dictionary(StringComparer.OrdinalIgnoreCase); + } catch (Exception) { + return new Dictionary(StringComparer.OrdinalIgnoreCase); + } + } + } + + /// Tool result appended after a tool call (mirrors assistant tool_calls by id). + public sealed class LlmToolResult { + public string ToolCallId { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + public string Result { get; set; } = string.Empty; + public bool IsError { get; set; } + } + + /// + /// The single normalized message representation used end to end: history projection, + /// agent loop state, transport requests, and continuation snapshots (schema v2). + /// Replaces per-provider history types and their serialize/deserialize pairs. + /// + public sealed class LlmMessage { + public LlmRole Role { get; set; } + /// Text content. For Tool-role messages this is the tool result text. + public string? Text { get; set; } + /// Reasoning/thinking content for assistant messages (thinking-mode models). + public string? Thinking { get; set; } + /// Tool calls initiated by this assistant message (null otherwise). + public List? ToolCalls { get; set; } + /// Tool call id for Tool-role messages. + public string? ToolCallId { get; set; } + /// PNG image bytes for user visual input (vision-capable transports only). + public byte[]? ImagePng { get; set; } + public string? ImageMediaType { get; set; } + /// Tool-role only: whether the tool execution failed. + public bool IsToolError { get; set; } + + public static LlmMessage System(string text) => new() { Role = LlmRole.System, Text = text }; + public static LlmMessage User(string text, byte[]? imagePng = null, string? imageMediaType = null) + => new() { Role = LlmRole.User, Text = text, ImagePng = imagePng, ImageMediaType = imageMediaType }; + public static LlmMessage Assistant(string? text, string? thinking = null, List? toolCalls = null) + => new() { Role = LlmRole.Assistant, Text = text, Thinking = thinking, ToolCalls = toolCalls }; + public static LlmMessage ToolResult(string toolCallId, string name, string result, bool isError = false) + => new() { Role = LlmRole.Tool, ToolCallId = toolCallId, Thinking = name, Text = result, IsToolError = isError }; + } + + /// Normalized tool definition advertised to the model. + public sealed class LlmToolSpec { + public string Name { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + /// JSON schema for parameters (provider-neutral). + public string ParametersJson { get; set; } = "{}"; + public bool StrictSchema { get; set; } + } + + /// Result of one assistant turn after streaming completed. + public sealed class LlmTurnResult { + /// Full turn text (not trimmed). + public string Text { get; set; } = string.Empty; + /// Reasoning/thinking content, when the provider streams it. + public string Reasoning { get; set; } = string.Empty; + /// Native tool calls, empty for text-protocol sources and pure-text turns. + public List ToolCalls { get; set; } = new(); + /// + /// Native protocol: tool call metadata was malformed and the transport already appended a + /// self-correction message to its history; the loop must continue without executing tools. + /// + public bool MalformedToolCall { get; set; } + /// Provider-specific usage observation for prompt-caching telemetry (opaque). + public object? UsageObservation { get; set; } + /// True when at least one delta arrived during the turn. + public bool StreamedAny { get; set; } + } +} diff --git a/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs b/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs index da6b3ea6..e789fa4f 100644 --- a/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs +++ b/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs @@ -1,60 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Text.Json; +using TelegramSearchBot.Model.AI; namespace TelegramSearchBot.Model.AI { - /// - /// Normalized native tool call surfaced by a transport (native tool-calling protocol). - /// Arguments are raw JSON; each transport parses/normalizes them as needed. - /// - public sealed class LlmNativeToolCall { - public string Id { get; set; } = string.Empty; - public string Name { get; set; } = string.Empty; - public string ArgumentsJson { get; set; } = "{}"; - - public Dictionary ParseArguments() { - if (string.IsNullOrWhiteSpace(ArgumentsJson)) { - return new Dictionary(); - } - try { - return JsonSerializer.Deserialize>(ArgumentsJson) - ?.ToDictionary(kv => kv.Key, kv => kv.Value.ToString()) - ?? new Dictionary(); - } catch (Exception) { - return new Dictionary(); - } - } - } - - /// Result of executing one tool call, keyed by native call id. - public sealed class LlmToolResult { - public string ToolCallId { get; set; } = string.Empty; - public string Name { get; set; } = string.Empty; - public string ArgumentsJson { get; set; } = "{}"; - public string Result { get; set; } = string.Empty; - public bool IsError { get; set; } - } - - /// Outcome of one assistant turn (one LLM request), after streaming completed. - public sealed class LlmTurnResult { - /// Full turn text (not trimmed). - public string Text { get; set; } = string.Empty; - /// Reasoning/thinking content, when the provider streams it. - public string Reasoning { get; set; } = string.Empty; - /// Native tool calls, empty for text-protocol sources and pure-text turns. - public List ToolCalls { get; set; } = new(); - /// - /// Native protocol: tool call metadata was malformed and the source already appended a - /// self-correction message to its history; the loop must continue without executing tools. - /// - public bool MalformedToolCall { get; set; } - /// Provider-specific usage observation for prompt-caching telemetry (opaque). - public object? UsageObservation { get; set; } - /// True when at least one delta arrived (text or thinking) during the turn. - public bool StreamedAny { get; set; } - } - - /// Events streamed by a turn source. Always terminates with TurnCompleted. + /// Events streamed by a transport turn. Always terminates with TurnCompleted. public abstract record LlmStreamEvent { public sealed record TextDelta(string Delta) : LlmStreamEvent; public sealed record ThinkingDelta(string Delta) : LlmStreamEvent; diff --git a/TelegramSearchBot.Common/Model/AI/LlmContinuationSnapshot.cs b/TelegramSearchBot.Common/Model/AI/LlmContinuationSnapshot.cs index 21348a6a..0b831465 100644 --- a/TelegramSearchBot.Common/Model/AI/LlmContinuationSnapshot.cs +++ b/TelegramSearchBot.Common/Model/AI/LlmContinuationSnapshot.cs @@ -76,9 +76,16 @@ public string SchemaVersion { public int ChannelId { get; set; } /// - /// Serialized conversation history (all messages including system, user, assistant, tool results) + /// Schema v1 (legacy): per-provider serialized history. Kept for deserializing old snapshots; + /// obsolete snapshots expire via TTL and are not migrated. /// - public List ProviderHistory { get; set; } = []; + public List? ProviderHistory { get; set; } + + /// + /// Schema v2: normalized conversation history (all messages including system, user, + /// assistant with tool calls, tool results). The single snapshot format going forward. + /// + public List? NormalizedHistory { get; set; } /// /// The last accumulated content that was displayed to the user diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs index a355a021..c9794b43 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs @@ -12,8 +12,8 @@ namespace TelegramSearchBot.LLM.Test.Service.AI.LLM { /// - /// Unit tests for the shared agent tool-call loop (LlmToolLoop). Uses a scripted - /// ILlmTurnSource plus a mock external tool registered in McpToolHelper. + /// Unit tests for the shared agent tool-call loop (LlmToolLoop): the loop owns the + /// normalized LlmMessage history; a scripted ILlmTransport feeds turns. /// public class LlmToolLoopTests { private static ToolContext ToolCtx() => new ToolContext { ChatId = 1, UserId = 2, MessageId = 3 }; @@ -27,48 +27,40 @@ public class LlmToolLoopTests { ChannelId = 9 }; - /// Scripted source: each enqueued factory produces one turn's events. - private sealed class ScriptedTurnSource : ILlmTurnSource { - private readonly Queue>> _turns = new(); + /// Scripted transport: records per-turn requests, replays scripted turn factories. + private sealed class ScriptedTransport : ILlmTransport { + private readonly Queue>> _turns = new(); public bool SupportsNativeTools { get; set; } - public List ReceivedUserContents { get; } = new(); - public List> CommittedResults { get; } = new(); - public List TrackedHistory { get; } = new(); + public List Requests { get; } = new(); - public void EnqueueTurn(Func> turnFactory) => _turns.Enqueue(turnFactory); + public void EnqueueTurn(Func> turnFactory) => _turns.Enqueue(turnFactory); public async IAsyncEnumerable StreamTurnAsync( - string? userContent, + LlmTurnRequest request, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - ReceivedUserContents.Add(userContent); - var turn = _turns.Dequeue(); - foreach (var evt in turn(userContent)) { + Requests.Add(request); + foreach (var evt in _turns.Dequeue()(request)) { yield return evt; } await Task.CompletedTask; } - - public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { - TrackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); - } - - public void CommitToolResults(IReadOnlyList results) { - CommittedResults.Add(results.ToList()); - TrackedHistory.Add(new SerializedChatMessage { Role = "user", Content = "tool-results" }); - } - - public IReadOnlyList GetTrackedHistory() => TrackedHistory; } - private static Func, Task> EchoExecutor() => - async (server, tool, args) => { - await Task.CompletedTask; - return $"mock:{args.GetValueOrDefault("input", "")}"; + private static Func> Turn( + string text, List? toolCalls = null) { + return _ => new List { + new LlmStreamEvent.TextDelta(text), + new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = text, + ToolCalls = toolCalls ?? new List(), + StreamedAny = !string.IsNullOrEmpty(text) + }) }; + } [Fact] - public async Task TextProtocol_ParsesToolCall_Executes_SendsFeedback() { + public async Task TextProtocol_ParsesToolCall_AppendsAssistantAndFeedback() { McpToolHelper.RegisterExternalTools( new List<(string, McpToolHelper.ExternalToolInfo)> { ("test-server", new McpToolHelper.ExternalToolInfo { @@ -80,32 +72,37 @@ public async Task TextProtocol_ParsesToolCall_Executes_SendsFeedback() { } }) }, - EchoExecutor()); - + async (server, tool, args) => { await Task.CompletedTask; return "mock:v"; }); try { - var source = new ScriptedTurnSource(); const string xml = "v"; - source.EnqueueTurn(_ => new[] { - new LlmStreamEvent.TextDelta(xml), - ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = xml }) - }); - source.EnqueueTurn(_ => new[] { - new LlmStreamEvent.TextDelta("final answer text"), - ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = "final answer text" }) - }); + var transport = new ScriptedTransport(); + transport.EnqueueTurn(Turn(xml)); + transport.EnqueueTurn(Turn("final answer text")); + var history = new List { LlmMessage.User("hi") }; var yielded = new List(); - await foreach (var item in LlmToolLoop.RunAsync(source, "hi", ToolCtx(), Meta(), null, CancellationToken.None)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = "sys", + History = history, + Tools = null, + ToolContext = ToolCtx(), + Meta = Meta() + }; + await foreach (var item in LlmToolLoop.RunAsync(run, CancellationToken.None)) { yielded.Add(item); } - Assert.Equal(2, source.ReceivedUserContents.Count); - Assert.Equal("hi", source.ReceivedUserContents[0]); - Assert.Contains("[Executed Tool 'mcp_test-server_loopTool'. Result: mock:v]", source.ReceivedUserContents[1]); - // Cumulative snapshot semantics: one yield per text delta beyond 10 chars + one per tool display. - Assert.True(yielded.Count >= 2); + Assert.Equal(2, transport.Requests.Count); Assert.Contains("final answer text", yielded[^1]); - Assert.Empty(source.CommittedResults); + // History: user input, assistant tool call turn, tool feedback user message, final assistant. + Assert.Equal(4, history.Count); + Assert.Equal(LlmRole.User, history[0].Role); + Assert.Equal(LlmRole.Assistant, history[1].Role); + Assert.Contains(xml, history[1].Text); + Assert.Equal(LlmRole.User, history[2].Role); + Assert.Contains("[Executed Tool 'mcp_test-server_loopTool'. Result: mock:v]", history[2].Text); + Assert.Equal(LlmRole.Assistant, history[3].Role); } finally { McpToolHelper.RegisterExternalTools( new List<(string, McpToolHelper.ExternalToolInfo)>(), @@ -114,7 +111,7 @@ public async Task TextProtocol_ParsesToolCall_Executes_SendsFeedback() { } [Fact] - public async Task NativeProtocol_CommitsToolResultsAndPassesNullUserContent() { + public async Task NativeProtocol_LoopOwnsHistory_AppendsToolResults() { McpToolHelper.RegisterExternalTools( new List<(string, McpToolHelper.ExternalToolInfo)> { ("test-server", new McpToolHelper.ExternalToolInfo { @@ -125,31 +122,39 @@ public async Task NativeProtocol_CommitsToolResultsAndPassesNullUserContent() { }) }, async (server, tool, args) => { await Task.CompletedTask; return "native-ok"; }); - try { - var source = new ScriptedTurnSource { SupportsNativeTools = true }; - source.EnqueueTurn(_ => new[] { - ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = "calling tool", - ToolCalls = new List { - new() { Id = "call-1", Name = "mcp_test-server_nativeTool", ArgumentsJson = "{\"input\":\"v\"}" } - } - }) - }); - source.EnqueueTurn(_ => new[] { - ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = "all done now" }) - }); + var transport = new ScriptedTransport { SupportsNativeTools = true }; + transport.EnqueueTurn(Turn("calling tool", new List { + new() { Id = "call-1", Name = "mcp_test-server_nativeTool", ArgumentsJson = "{\"input\":\"v\"}" } + })); + transport.EnqueueTurn(Turn("all done now")); + var history = new List { LlmMessage.User("hi") }; var yielded = new List(); - await foreach (var item in LlmToolLoop.RunAsync(source, null, ToolCtx(), Meta(), null, CancellationToken.None)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = "sys", + History = history, + Tools = McpToolHelper.GetLlmToolSpecs(), + ToolContext = ToolCtx(), + Meta = Meta() + }; + await foreach (var item in LlmToolLoop.RunAsync(run, CancellationToken.None)) { yielded.Add(item); } - Assert.Equal(2, source.ReceivedUserContents.Count); - Assert.Null(source.ReceivedUserContents[1]); - Assert.Single(source.CommittedResults); - Assert.Equal("native-ok", source.CommittedResults[0][0].Result); - Assert.Equal("call-1", source.CommittedResults[0][0].ToolCallId); + Assert.Equal(2, transport.Requests.Count); + // Turn-2 request history must contain the assistant tool call + tool result. + var turn2 = transport.Requests[1]; + Assert.Equal(4, turn2.History.Count); + Assert.Equal(LlmRole.Assistant, turn2.History[1].Role); + Assert.NotNull(turn2.History[1].ToolCalls); + Assert.Equal("call-1", turn2.History[1].ToolCalls![0].Id); + Assert.Equal(LlmRole.Tool, turn2.History[2].Role); + Assert.Equal("call-1", turn2.History[2].ToolCallId); + Assert.Equal("native-ok", turn2.History[2].Text); + Assert.Equal(4, history.Count); + Assert.Contains("all done now", yielded[^1]); } finally { McpToolHelper.RegisterExternalTools( new List<(string, McpToolHelper.ExternalToolInfo)>(), @@ -158,7 +163,7 @@ public async Task NativeProtocol_CommitsToolResultsAndPassesNullUserContent() { } [Fact] - public async Task IterationLimit_BuildsContinuationSnapshot() { + public async Task IterationLimit_BuildsV2SnapshotWithNormalizedHistory() { McpToolHelper.RegisterExternalTools( new List<(string, McpToolHelper.ExternalToolInfo)> { ("test-server", new McpToolHelper.ExternalToolInfo { @@ -169,25 +174,29 @@ public async Task IterationLimit_BuildsContinuationSnapshot() { }) }, async (server, tool, args) => { await Task.CompletedTask; return "still running"; }); - var oldCycles = Env.MaxToolCycles; Env.MaxToolCycles = 2; try { - var source = new ScriptedTurnSource { SupportsNativeTools = true }; + var transport = new ScriptedTransport { SupportsNativeTools = true }; for (var i = 0; i < 3; i++) { var callId = $"call-{i}"; - source.EnqueueTurn(_ => new[] { - ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = "working", - ToolCalls = new List { - new() { Id = callId, Name = "mcp_test-server_endlessTool", ArgumentsJson = "{}" } - } - }) - }); + transport.EnqueueTurn(Turn("working", new List { + new() { Id = callId, Name = "mcp_test-server_endlessTool", ArgumentsJson = "{}" } + })); } var executionContext = new LlmExecutionContext(); - await foreach (var item in LlmToolLoop.RunAsync(source, null, ToolCtx(), Meta(), executionContext, CancellationToken.None)) { + var history = new List { LlmMessage.User("go") }; + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = "sys", + History = history, + Tools = McpToolHelper.GetLlmToolSpecs(), + ToolContext = ToolCtx(), + Meta = Meta(), + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, CancellationToken.None)) { } Assert.True(executionContext.IterationLimitReached); @@ -196,7 +205,10 @@ public async Task IterationLimit_BuildsContinuationSnapshot() { Assert.Equal(2, snapshot.CyclesSoFar); Assert.Equal("Test", snapshot.Provider); Assert.Equal(9, snapshot.ChannelId); - Assert.Equal(2, source.CommittedResults.Count); + Assert.NotNull(snapshot.NormalizedHistory); + Assert.Equal(history.Count, snapshot.NormalizedHistory!.Count); + Assert.Contains(snapshot.NormalizedHistory, m => m.Role == LlmRole.Tool && m.ToolCallId == "call-0"); + Assert.Null(snapshot.ProviderHistory); } finally { Env.MaxToolCycles = oldCycles; McpToolHelper.RegisterExternalTools( @@ -207,18 +219,24 @@ public async Task IterationLimit_BuildsContinuationSnapshot() { [Fact] public async Task ResumeMode_YieldsOnlyNewContentButSnapshotsFullContent() { - var source = new ScriptedTurnSource(); - source.EnqueueTurn(_ => new[] { new LlmStreamEvent.TextDelta(" continued text") }); - source.EnqueueTurn(_ => new[] { - ( LlmStreamEvent ) new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = "continued text" }) - }); + var transport = new ScriptedTransport(); + transport.EnqueueTurn(_ => new List { new LlmStreamEvent.TextDelta(" continued text") }); + transport.EnqueueTurn(Turn("continued text")); var meta = Meta(); meta.BaseCycles = 3; meta.InitialContent = "old content"; var executionContext = new LlmExecutionContext(); var yielded = new List(); - await foreach (var item in LlmToolLoop.RunAsync(source, null, ToolCtx(), meta, executionContext, CancellationToken.None)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = "sys", + History = new List { LlmMessage.User("prior") }, + ToolContext = ToolCtx(), + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, CancellationToken.None)) { yielded.Add(item); } diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIProviderHistorySerializationTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIProviderHistorySerializationTests.cs deleted file mode 100644 index 12cf1862..00000000 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIProviderHistorySerializationTests.cs +++ /dev/null @@ -1,308 +0,0 @@ -using System.Collections.Generic; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Net; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; -using OpenAI; -using OpenAI.Chat; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; -using TelegramSearchBot.Service.AI.LLM; -using Xunit; - -namespace TelegramSearchBot.Test.Service.AI.LLM { - public class OpenAIProviderHistorySerializationTests { - [Fact] - public void SerializeProviderHistory_BasicMessages_RoundTrips() { - // Arrange - var history = new List { - new SystemChatMessage("You are a helpful assistant."), - new UserChatMessage("Hello"), - new AssistantChatMessage("Hi there! How can I help?"), - new UserChatMessage("[Tool 'bash' result: success]"), - new AssistantChatMessage("The command executed successfully."), - }; - - // Act - var serialized = OpenAIService.SerializeProviderHistory(history); - var deserialized = OpenAIService.DeserializeProviderHistory(serialized); - - // Assert - Assert.Equal(5, serialized.Count); - Assert.Equal("system", serialized[0].Role); - Assert.Equal("You are a helpful assistant.", serialized[0].Content); - Assert.Equal("user", serialized[1].Role); - Assert.Equal("Hello", serialized[1].Content); - Assert.Equal("assistant", serialized[2].Role); - Assert.Equal("Hi there! How can I help?", serialized[2].Content); - Assert.Equal("user", serialized[3].Role); - Assert.Contains("Tool 'bash'", serialized[3].Content); - Assert.Equal("assistant", serialized[4].Role); - - // Deserialized should also have 5 entries with correct types - Assert.Equal(5, deserialized.Count); - Assert.IsType(deserialized[0]); - Assert.IsType(deserialized[1]); - Assert.IsType(deserialized[2]); - Assert.IsType(deserialized[3]); - Assert.IsType(deserialized[4]); - } - - [Fact] - public void SerializeProviderHistory_EmptyList_ReturnsEmpty() { - var serialized = OpenAIService.SerializeProviderHistory(new List()); - Assert.NotNull(serialized); - Assert.Empty(serialized); - } - - [Fact] - public void DeserializeProviderHistory_Null_ReturnsEmpty() { - var deserialized = OpenAIService.DeserializeProviderHistory(null); - Assert.NotNull(deserialized); - Assert.Empty(deserialized); - } - - [Fact] - public void DeserializeProviderHistory_UnknownRole_DefaultsToUser() { - var serialized = new List { - new SerializedChatMessage { Role = "unknown_role", Content = "test" } - }; - - var deserialized = OpenAIService.DeserializeProviderHistory(serialized); - Assert.Single(deserialized); - Assert.IsType(deserialized[0]); - } - - [Fact] - public void SerializeProviderHistory_WithToolCallHistory_PreservesContent() { - // Simulate a realistic tool-call history - var history = new List { - new SystemChatMessage("You are a helpful assistant with tool access."), - new UserChatMessage("List files in the current directory"), - new AssistantChatMessage("\n{\"name\": \"bash\", \"arguments\": {\"command\": \"ls -la\"}}\n"), - new UserChatMessage("[Executed Tool 'bash'. Result: file1.txt file2.txt]"), - new AssistantChatMessage("The current directory contains: file1.txt and file2.txt"), - }; - - var serialized = OpenAIService.SerializeProviderHistory(history); - - Assert.Equal(5, serialized.Count); - Assert.Contains("tool_call", serialized[2].Content); - Assert.Contains("bash", serialized[3].Content); - Assert.Contains("file1.txt", serialized[3].Content); - - // Round-trip - var deserialized = OpenAIService.DeserializeProviderHistory(serialized); - Assert.Equal(5, deserialized.Count); - } - - [Fact] - public void LlmExecutionContext_DefaultState_NotLimitReached() { - var ctx = new LlmExecutionContext(); - Assert.False(ctx.IterationLimitReached); - Assert.Null(ctx.SnapshotData); - } - - [Fact] - public void LlmContinuationSnapshot_Serialization_RoundTrips() { - var snapshot = new LlmContinuationSnapshot { - SnapshotId = "abc123", - ChatId = 12345, - OriginalMessageId = 100, - UserId = 9999, - ModelName = "gpt-4o", - Provider = "OpenAI", - ChannelId = 1, - LastAccumulatedContent = "Some accumulated text", - CyclesSoFar = 25, - ProviderHistory = new List { - new SerializedChatMessage { Role = "system", Content = "System prompt" }, - new SerializedChatMessage { Role = "user", Content = "User input" }, - new SerializedChatMessage { Role = "assistant", Content = "AI response" }, - } - }; - - var json = Newtonsoft.Json.JsonConvert.SerializeObject(snapshot); - var deserialized = Newtonsoft.Json.JsonConvert.DeserializeObject(json); - - Assert.NotNull(deserialized); - Assert.Equal("abc123", deserialized.SnapshotId); - Assert.Equal(12345, deserialized.ChatId); - Assert.Equal(100, deserialized.OriginalMessageId); - Assert.Equal("gpt-4o", deserialized.ModelName); - Assert.Equal("OpenAI", deserialized.Provider); - Assert.Equal(1, deserialized.ChannelId); - Assert.Equal("Some accumulated text", deserialized.LastAccumulatedContent); - Assert.Equal(25, deserialized.CyclesSoFar); - Assert.Equal(3, deserialized.ProviderHistory.Count); - } - - [Fact] - public void SerializeProviderHistory_WithNonEmptyReasoningContent_RoundTrips() { - // Arrange - create a SerializedChatMessage with ReasoningContent directly - // This simulates what would come from a real API response with thinking mode - var serialized = new List { - new SerializedChatMessage { Role = "system", Content = "You are helpful." }, - new SerializedChatMessage { Role = "user", Content = "What is 2+2?" }, - new SerializedChatMessage { Role = "assistant", Content = "Final answer text", ReasoningContent = "Step 1: analyze... Step 2: compute..." }, - }; - - // Act - deserialize and reserialize (round-trip) - var deserialized = OpenAIService.DeserializeProviderHistory(serialized); - var reserialized = OpenAIService.SerializeProviderHistory(deserialized); - - // Assert - reasoning content should be preserved through round-trip - Assert.Equal(3, serialized.Count); - Assert.Equal("assistant", serialized[2].Role); - Assert.Equal("Final answer text", serialized[2].Content); - Assert.NotNull(serialized[2].ReasoningContent); - Assert.Contains("Step 1", serialized[2].ReasoningContent); - - // Deserialize creates AssistantChatMessage objects (SetAssistantReasoningContent is called) - Assert.Equal(3, deserialized.Count); - Assert.IsType(deserialized[2]); - - // Reserialized should also have ReasoningContent preserved - Assert.Equal(3, reserialized.Count); - // Note: Due to SDK limitation where Reasoning property is read-only, - // GetAssistantReasoningContent may return null if Patch.Set wasn't used during streaming. - // This test verifies the deserialization path works correctly. - } - - [Fact] - public void SerializeProviderHistory_WithEmptyReasoningContent_Preserved() { - // Arrange - create assistant message with empty reasoning content - var serialized = new List { - new SerializedChatMessage { Role = "user", Content = "Hi" }, - new SerializedChatMessage { Role = "assistant", Content = "Quick answer.", ReasoningContent = "" }, - }; - - // Act - var deserialized = OpenAIService.DeserializeProviderHistory(serialized); - var reserialized = OpenAIService.SerializeProviderHistory(deserialized); - - // Assert - empty reasoning content should NOT be lost (null vs empty string) - Assert.Equal(2, serialized.Count); - Assert.Equal("assistant", serialized[1].Role); - // ReasoningContent may be null or empty string - both are acceptable - Assert.True(string.IsNullOrEmpty(serialized[1].ReasoningContent) || serialized[1].ReasoningContent == ""); - - Assert.Equal(2, deserialized.Count); - Assert.Equal(2, reserialized.Count); - } - - [Fact] - public void DeserializeProviderHistory_WithNullReasoningContent_Succeeds() { - // Arrange - serialized message with null ReasoningContent (old snapshot format) - var serialized = new List { - new SerializedChatMessage { Role = "system", Content = "System" }, - new SerializedChatMessage { Role = "user", Content = "Hello" }, - new SerializedChatMessage { Role = "assistant", Content = "Hi", ReasoningContent = null }, - }; - - // Act - deserialize without adding an empty reasoning_content patch. - var deserialized = OpenAIService.DeserializeProviderHistory(serialized); - - // Assert - deserialization succeeds even with null reasoning content - Assert.Equal(3, deserialized.Count); - Assert.IsType(deserialized[0]); - Assert.IsType(deserialized[1]); - Assert.IsType(deserialized[2]); - // The key thing: NO exception thrown, deserialization succeeds - } - - [Fact] - public void ShouldIncludeEmptyReasoningContent_UsesDeepSeekGatewayOrModel() { - Assert.True(OpenAIService.ShouldIncludeEmptyReasoningContent( - new LLMChannel { Gateway = "https://api.deepseek.com/v1" }, - "deepseek-chat")); - - Assert.False(OpenAIService.ShouldIncludeEmptyReasoningContent( - new LLMChannel { Gateway = "https://api.minimaxi.com/v1" }, - "abab6.5s")); - } - - [Fact] - public async Task DeserializeProviderHistory_WithEmptyReasoningContent_ForDeepSeekSerializesOpenAIRequest() { - var serialized = new List { - new SerializedChatMessage { Role = "user", Content = "Hi" }, - new SerializedChatMessage { Role = "assistant", Content = "Hello", ReasoningContent = "" }, - new SerializedChatMessage { Role = "user", Content = "Use the available context." }, - }; - var history = OpenAIService.DeserializeProviderHistory(serialized, includeEmptyReasoningContent: true); - var handler = new CapturingHandler(); - var httpClient = new HttpClient(handler); - var options = new OpenAIClientOptions { - Endpoint = new System.Uri("https://example.test/v1"), - Transport = new HttpClientPipelineTransport(httpClient) - }; - var chatClient = new ChatClient("test-model", new ApiKeyCredential("test-key"), options); - - await chatClient.CompleteChatAsync(history, new ChatCompletionOptions()); - - Assert.True(handler.RequestSent); - Assert.Contains("\"reasoning_content\":\"\"", handler.RequestBody); - - var reserialized = OpenAIService.SerializeProviderHistory(history); - Assert.Equal("", reserialized[1].ReasoningContent); - } - - [Fact] - public void DeserializeProviderHistory_DeepSeekToolCallChain_PreservesAllReasoningContent() { - // Simulate a DeepSeek tool call chain where reasoning_content must be preserved - var serialized = new List { - new SerializedChatMessage { Role = "system", Content = "You are a helpful assistant with tools." }, - new SerializedChatMessage { Role = "user", Content = "Get the weather in Beijing" }, - new SerializedChatMessage { Role = "assistant", Content = "Let me check...", ReasoningContent = "User wants weather. I need to call the weather tool." }, - new SerializedChatMessage { Role = "user", Content = "[Tool result: Sunny, 25C]" }, - new SerializedChatMessage { Role = "assistant", Content = "It's sunny in Beijing at 25C.", ReasoningContent = "Tool returned sunny weather. I can now answer." }, - }; - - var deserialized = OpenAIService.DeserializeProviderHistory(serialized); - - Assert.Equal(5, deserialized.Count); - Assert.IsType(deserialized[2]); - Assert.IsType(deserialized[4]); - } - - private sealed class CapturingHandler : HttpMessageHandler { - public bool RequestSent { get; private set; } - public string RequestBody { get; private set; } = string.Empty; - - protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - RequestSent = true; - RequestBody = request.Content is null - ? string.Empty - : await request.Content.ReadAsStringAsync(cancellationToken); - var response = new HttpResponseMessage(HttpStatusCode.OK) { - Content = new StringContent(""" - { - "id": "chatcmpl-test", - "object": "chat.completion", - "created": 1710000000, - "model": "test-model", - "choices": [ - { - "index": 0, - "message": { - "role": "assistant", - "content": "ok" - }, - "finish_reason": "stop" - } - ], - "usage": { - "prompt_tokens": 1, - "completion_tokens": 1, - "total_tokens": 2 - } - } - """) - }; - return response; - } - } - } -} diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs index 54734d27..e676036f 100644 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs @@ -68,6 +68,7 @@ public IAsyncEnumerable ExecWithHistoryAsync( IReadOnlyList history, Message message, long ChatId, string modelName, LLMChannel channel, LLMApiBinding binding, LlmExecutionContext executionContext, + bool supportsVision = false, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { return ExecAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken); } diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs deleted file mode 100644 index 957a40b2..00000000 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmTurnSource.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using TelegramSearchBot.Model.AI; - -namespace TelegramSearchBot.Interface.AI.LLM { - /// - /// A per-execution turn source: owns the provider-typed history for one agent run and - /// streams one assistant turn per call. The shared - /// drives the tool-call cycle; transports only translate normalized turns to/from the - /// provider API. One instance is created per ExecAsync/ResumeFromSnapshot run. - /// - public interface ILlmTurnSource { - /// True when the provider supports native tool-calling for this model/protocol. - bool SupportsNativeTools { get; } - - /// - /// Stream one assistant turn. - /// Text-protocol sources: is the user message for this turn - /// (initial input on the first turn, tool feedback afterwards); no tools are attached. - /// Native sources: is the initial input on the first turn - /// only; after the next call passes null. - /// Yields text/thinking deltas, then exactly one . - /// - IAsyncEnumerable StreamTurnAsync(string? userContent, CancellationToken cancellationToken); - - /// - /// Append the assistant turn to the source's history. Called once per turn after - /// . Text-protocol sources append the assistant - /// message (if non-empty) and update tracked history; native sources append only the - /// plain-text final assistant message (tool-call turns are committed by - /// ). - /// - void CommitAssistantTurn(string text, string reasoning, bool isFinal); - - /// - /// Native mode only: append the assistant tool-call blocks plus the executed results - /// to the source's history so the next turn sees them. - /// - void CommitToolResults(IReadOnlyList results); - - /// Serialized history for continuation snapshots. - IReadOnlyList GetTrackedHistory(); - } -} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs index d2ec7544..9d585d61 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs @@ -634,17 +634,64 @@ public async IAsyncEnumerable ExecAsync( yield break; } - // Try native tool calling first; fall back to XML prompt-based if it fails - bool useNativeToolCalling = true; + var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); + var supportsVision = await CheckVisionSupport(modelName, channel.Id); + var nativeTools = McpToolHelper.GetNativeToolDefinitions(); + var useNativeToolCalling = nativeTools is { Count: > 0 }; - if (nativeTools == null || !nativeTools.Any()) { - useNativeToolCalling = false; + if (useNativeToolCalling) { + bool nativeFailed = false; + var nativeEnumerator = ExecWithNativeToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); + await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); + bool hasFirst = false; + try { + hasFirst = await enumerator.MoveNextAsync(); + } catch (Exception ex) when (IsToolCallingNotSupportedError(ex)) { + _logger.LogInformation("{ServiceName}: Native tool calling not supported for model {Model}, falling back to XML prompt-based tool calling. Error: {Error}", ServiceName, modelName, ex.Message); + nativeFailed = true; + } + + if (!nativeFailed) { + if (hasFirst) { + yield return enumerator.Current; + while (await enumerator.MoveNextAsync()) { + yield return enumerator.Current; + } + } + yield break; + } } + // Fallback: XML prompt-based tool calling + await foreach (var item in ExecWithXmlToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { + yield return item; + } + } + + /// + public async IAsyncEnumerable ExecWithHistoryAsync( + IReadOnlyList history, + DataMessage message, long ChatId, string modelName, LLMChannel channel, + LLMApiBinding binding, LlmExecutionContext executionContext, + bool supportsVision, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (string.IsNullOrWhiteSpace(modelName)) modelName = "claude-sonnet-4-20250514"; + + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { + _logger.LogError("{ServiceName}: Channel or ApiKey is not configured.", ServiceName); + yield return $"Error: {ServiceName} channel/apikey is not configured."; + yield break; + } + + var nativeTools = McpToolHelper.GetNativeToolDefinitions(); + var useNativeToolCalling = nativeTools is { Count: > 0 }; + if (useNativeToolCalling) { bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); + var nativeEnumerator = ExecWithNativeToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); bool hasFirst = false; try { @@ -665,8 +712,7 @@ public async IAsyncEnumerable ExecAsync( } } - // Fallback: XML prompt-based tool calling - await foreach (var item in ExecWithXmlToolCallingAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { + await foreach (var item in ExecWithXmlToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { yield return item; } } @@ -678,11 +724,12 @@ private static bool IsToolCallingNotSupportedError(Exception ex) { message.Contains("unsupported", StringComparison.OrdinalIgnoreCase) || message.Contains("invalid", StringComparison.OrdinalIgnoreCase) ); } - /// /// Execute LLM with native Anthropic tool calling API. /// - private async IAsyncEnumerable ExecWithNativeToolCallingAsync( + public async IAsyncEnumerable ExecWithNativeToolCallingAsync( + IReadOnlyList rows, + bool supportsVision, DataMessage message, long ChatId, string modelName, LLMChannel channel, LLMApiBinding binding, LlmExecutionContext executionContext, @@ -692,28 +739,13 @@ private async IAsyncEnumerable ExecWithNativeToolCallingAsync( var botName = await GetBotNameAsync(); string systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId); - bool supportsVision = await CheckVisionSupport(modelName, channel.Id); - var (_, providerHistory) = await GetChatHistory(ChatId, systemPrompt, message, supportsVision); var promptCachingEnabled = await IsPromptCachingEnabledAsync(); - var stableHistory = providerHistory.Count > 0 - ? providerHistory.Take(providerHistory.Count - 1).ToList() - : new List(); - var (toolDefinitionHash, stablePrefixHash) = BuildPromptCachingContext( - "anthropic-native", - systemPrompt, - SerializeProviderHistory(systemPrompt, stableHistory)); - providerHistory = PrepareMessagesForPromptCaching(providerHistory, promptCachingEnabled, excludeDynamicTail: true, out var cacheBreakpointInserted); - using var client = CreateClient(channel, binding); - var anthropicTools = ConvertToAnthropicTools(nativeTools, promptCachingEnabled); - var trackedHistory = new List { - new SerializedChatMessage { Role = "system", Content = systemPrompt } - }; - var source = new AnthropicTurnSource( - this, client, systemPrompt, providerHistory, trackedHistory, anthropicTools, - promptCachingEnabled, toolDefinitionHash, stablePrefixHash, cacheBreakpointInserted, - modelName, channel); + var transport = new AnthropicMessagesTransport(this, client, systemPrompt, modelName, channel, + nativeTools: true, promptCachingEnabled); + var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); + var meta = new LlmToolLoopMeta { ChatId = ChatId, OriginalMessageId = message.MessageId, @@ -723,15 +755,33 @@ private async IAsyncEnumerable ExecWithNativeToolCallingAsync( ChannelId = channel.Id }; var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = history, + Tools = McpToolHelper.GetLlmToolSpecs(), + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding), + ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision, + PromptCachingEnabled = promptCachingEnabled + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } - /// - /// Execute LLM with XML prompt-based tool calling (fallback). - /// private async IAsyncEnumerable ExecWithXmlToolCallingAsync( + IReadOnlyList rows, + bool supportsVision, DataMessage message, long ChatId, string modelName, LLMChannel channel, LLMApiBinding binding, LlmExecutionContext executionContext, @@ -740,27 +790,13 @@ private async IAsyncEnumerable ExecWithXmlToolCallingAsync( var botName = await GetBotNameAsync(); string systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - bool supportsVision = await CheckVisionSupport(modelName, channel.Id); - var (_, providerHistory) = await GetChatHistory(ChatId, systemPrompt, message, supportsVision); var promptCachingEnabled = await IsPromptCachingEnabledAsync(); - var stableHistory = providerHistory.Count > 0 - ? providerHistory.Take(providerHistory.Count - 1).ToList() - : new List(); - var (toolDefinitionHash, stablePrefixHash) = BuildPromptCachingContext( - "anthropic-xml", - systemPrompt, - SerializeProviderHistory(systemPrompt, stableHistory)); - providerHistory = PrepareMessagesForPromptCaching(providerHistory, promptCachingEnabled, excludeDynamicTail: true, out var cacheBreakpointInserted); - using var client = CreateClient(channel, binding); - var trackedHistory = new List { - new SerializedChatMessage { Role = "system", Content = systemPrompt } - }; - var source = new AnthropicTurnSource( - this, client, systemPrompt, providerHistory, trackedHistory, null, - promptCachingEnabled, toolDefinitionHash, stablePrefixHash, cacheBreakpointInserted, - modelName, channel); + var transport = new AnthropicMessagesTransport(this, client, systemPrompt, modelName, channel, + nativeTools: false, promptCachingEnabled); + var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); + var meta = new LlmToolLoopMeta { ChatId = ChatId, OriginalMessageId = message.MessageId, @@ -770,7 +806,26 @@ private async IAsyncEnumerable ExecWithXmlToolCallingAsync( ChannelId = channel.Id }; var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = history, + Tools = null, + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding), + ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision, + PromptCachingEnabled = promptCachingEnabled + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } @@ -798,6 +853,10 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( _logger.LogError("{ServiceName}: Cannot resume from null snapshot.", ServiceName); yield break; } + if (snapshot.NormalizedHistory is not { Count: > 0 } savedHistory) { + _logger.LogError("{ServiceName}: Snapshot {SnapshotId} has no v2 normalized history (legacy v1 snapshots expire via TTL).", ServiceName, snapshot.SnapshotId); + yield break; + } var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { @@ -809,23 +868,23 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( if (string.IsNullOrWhiteSpace(modelName)) modelName = "claude-sonnet-4-20250514"; _logger.LogInformation("{ServiceName}: Resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", - ServiceName, snapshot.SnapshotId, snapshot.ChatId, snapshot.ProviderHistory?.Count ?? 0); + ServiceName, snapshot.SnapshotId, snapshot.ChatId, savedHistory.Count); - var (systemPrompt, providerHistory) = DeserializeProviderHistory(snapshot.ProviderHistory); - var trackedHistory = snapshot.ProviderHistory ?? new List(); - var promptCachingEnabled = await IsPromptCachingEnabledAsync(); - var (toolDefinitionHash, stablePrefixHash) = BuildPromptCachingContext( - "anthropic-resume", - systemPrompt ?? string.Empty, - snapshot.ProviderHistory ?? []); - providerHistory = PrepareMessagesForPromptCaching(providerHistory, promptCachingEnabled, excludeDynamicTail: false, out var cacheBreakpointInserted); + string systemPrompt; + if (savedHistory[0].Role == LlmRole.System) { + systemPrompt = savedHistory[0].Text ?? string.Empty; + savedHistory = savedHistory.Skip(1).ToList(); + } else { + var botName = await GetBotNameAsync(); + systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, snapshot.ChatId); + } + var promptCachingEnabled = await IsPromptCachingEnabledAsync(); using var client = CreateClient(channel, binding); - var source = new AnthropicTurnSource( - this, client, systemPrompt ?? string.Empty, providerHistory, trackedHistory, null, - promptCachingEnabled, toolDefinitionHash, stablePrefixHash, cacheBreakpointInserted, - modelName, channel); + var transport = new AnthropicMessagesTransport(this, client, systemPrompt, modelName, channel, + nativeTools: false, promptCachingEnabled); + var meta = new LlmToolLoopMeta { ChatId = snapshot.ChatId, OriginalMessageId = snapshot.OriginalMessageId, @@ -837,7 +896,26 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( InitialContent = snapshot.LastAccumulatedContent ?? string.Empty }; var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = savedHistory, + Tools = null, + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding), + ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = false, + PromptCachingEnabled = promptCachingEnabled + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } @@ -845,69 +923,79 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( #endregion /// - /// Per-run turn source for the Anthropic Messages API. Streams one turn per call and - /// owns the provider-typed history plus the serialized history used for snapshots. - /// Supports both native tool-calling (tools attached) and the XML text protocol. + /// Stateless transport adapter for the Anthropic Messages API. Converts the normalized + /// history to MessageParam per turn (including tool_use/tool_result blocks and prompt + /// cache breakpoints) and streams one assistant turn. /// - private sealed class AnthropicTurnSource : ILlmTurnSource { + private sealed class AnthropicMessagesTransport : ILlmTransport { private readonly AnthropicService _svc; private readonly AnthropicClient _client; private readonly string _systemPrompt; - private readonly List _providerHistory; - private readonly List _trackedHistory; - private readonly List? _tools; - private readonly bool _promptCachingEnabled; - private readonly string _toolDefinitionHash; - private readonly string _stablePrefixHash; - private readonly bool _cacheBreakpointInserted; private readonly string _modelName; private readonly LLMChannel _channel; + private readonly bool _nativeTools; + private readonly bool _promptCachingEnabled; + private List? _preparedHistory; + private int _convertedCount; + private bool _cacheBreakpointInserted; - private List<(string id, string name, string inputJson)> _pendingToolCalls = new(); - private string _pendingTurnText = string.Empty; - - public AnthropicTurnSource( - AnthropicService svc, AnthropicClient client, string systemPrompt, - List providerHistory, List trackedHistory, - List? tools, bool promptCachingEnabled, - string toolDefinitionHash, string stablePrefixHash, bool cacheBreakpointInserted, - string modelName, LLMChannel channel) { + public AnthropicMessagesTransport(AnthropicService svc, AnthropicClient client, string systemPrompt, + string modelName, LLMChannel channel, bool nativeTools, bool promptCachingEnabled) { _svc = svc; _client = client; _systemPrompt = systemPrompt; - _providerHistory = providerHistory; - _trackedHistory = trackedHistory; - _tools = tools; - _promptCachingEnabled = promptCachingEnabled; - _toolDefinitionHash = toolDefinitionHash; - _stablePrefixHash = stablePrefixHash; - _cacheBreakpointInserted = cacheBreakpointInserted; _modelName = modelName; _channel = channel; + _nativeTools = nativeTools; + _promptCachingEnabled = promptCachingEnabled; } - public bool SupportsNativeTools => _tools != null && _tools.Count > 0; + private LlmTurnRequest? _lastRequest; + + public bool SupportsNativeTools => _nativeTools; public async IAsyncEnumerable StreamTurnAsync( - string? userContent, + LlmTurnRequest request, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + _lastRequest = request; + // Legacy wire semantics: prompt-cache preparation runs once per run (first turn); + // later turns append raw deltas so user messages stay string-typed on the wire. + if (_preparedHistory == null) { + _preparedHistory = PrepareMessagesForPromptCaching(ToProviderHistory(request.History), _promptCachingEnabled, excludeDynamicTail: true, out var cacheBreakpointInserted); + _cacheBreakpointInserted = cacheBreakpointInserted; + } else if (request.History.Count > _convertedCount) { + _preparedHistory.AddRange(ToProviderHistory(request.History.Skip(_convertedCount))); + } + _convertedCount = request.History.Count; + var providerHistory = _preparedHistory; + try { System.IO.File.AppendAllText("C:/temp/anthropic_dbg.log", "caching=" + _promptCachingEnabled + " " + System.Text.Json.JsonSerializer.Serialize(new { P = providerHistory }) + Environment.NewLine + "###" + Environment.NewLine); } catch { } + + var nativeToolSpecs = request.Tools; + var rawHistory = providerHistory.ToList(); + var (toolDefinitionHash, stablePrefixHash) = BuildPromptCachingContext( + _nativeTools ? "anthropic-native" : "anthropic-xml", + _systemPrompt, + SerializeProviderHistory(_systemPrompt, rawHistory)); var parameters = new MessageCreateParams { Model = _modelName, MaxTokens = 8192, System = BuildSystemPrompt(_systemPrompt, _promptCachingEnabled), - Messages = _providerHistory, - Tools = SupportsNativeTools ? _tools : null, + Messages = providerHistory, + Tools = _nativeTools && nativeToolSpecs is { Count: > 0 } + ? ConvertToAnthropicToolSpecs(nativeToolSpecs, _promptCachingEnabled) + : null, }; + long? cacheCreationInputTokens = null; + long? cacheReadInputTokens = null; + object usageObservation = null; + var turnText = new StringBuilder(); var toolUseBlocks = new List<(string id, string name, string inputJson)>(); var currentToolInputBuilder = new StringBuilder(); string currentToolId = null; string currentToolName = null; - long? cacheCreationInputTokens = null; - long? cacheReadInputTokens = null; - object usageObservation = null; await foreach (var rawEvent in _client.Messages.CreateStreaming(parameters, cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); @@ -958,16 +1046,12 @@ public async IAsyncEnumerable StreamTurnAsync( _svc.LogPromptCachingObservation( _channel, "Anthropic", _modelName, _promptCachingEnabled, - _toolDefinitionHash, _stablePrefixHash, _cacheBreakpointInserted, + toolDefinitionHash, stablePrefixHash, _cacheBreakpointInserted, cacheCreationInputTokens, cacheReadInputTokens, usageObservation); - var responseText = turnText.ToString().Trim(); - _pendingTurnText = responseText; - _pendingToolCalls = toolUseBlocks; - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = responseText, - ToolCalls = toolUseBlocks.Select(b => new LlmNativeToolCall { + Text = turnText.ToString().Trim(), + ToolCalls = toolUseBlocks.Select(b => new LlmToolCall { Id = b.id, Name = b.name, ArgumentsJson = string.IsNullOrWhiteSpace(b.inputJson) ? "{}" : b.inputJson @@ -977,59 +1061,118 @@ public async IAsyncEnumerable StreamTurnAsync( }); } - public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { - if (SupportsNativeTools) { - // Native final-text turn: provider history is not extended (matches legacy behavior). - if (!string.IsNullOrWhiteSpace(text)) { - _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); + private List ToProviderHistory(IEnumerable messages) { + var request = _lastRequest!; + + var result = new List(); + var pendingToolResults = new List(); + + void FlushToolResults() { + if (pendingToolResults.Count > 0) { + result.Add(new MessageParam { Role = Role.User, Content = pendingToolResults.ToList() }); + pendingToolResults.Clear(); } - return; } - if (!string.IsNullOrWhiteSpace(text)) { - _providerHistory.Add(new MessageParam { Role = Role.Assistant, Content = text }); + + foreach (var m in request.History) { + switch (m.Role) { + case LlmRole.User: { + FlushToolResults(); + if (m.ImagePng != null && request.Config.SupportsVision) { + var blocks = new List(); + if (!string.IsNullOrEmpty(m.Text)) { + blocks.Add(new TextBlockParam(m.Text)); + } + blocks.Add(new ImageBlockParam(new Base64ImageSource { + Data = Convert.ToBase64String(m.ImagePng), + MediaType = MediaType.ImagePng + })); + result.Add(new MessageParam { Role = Role.User, Content = blocks }); + } else { + // Plain-text user messages use string content (wire-compatible with legacy). + result.Add(new MessageParam { Role = Role.User, Content = m.Text ?? string.Empty }); + } + break; + } + case LlmRole.Assistant: { + FlushToolResults(); + var blocks = new List(); + if (!string.IsNullOrEmpty(m.Text)) { + blocks.Add(new TextBlockParam(m.Text)); + } + if (m.ToolCalls is { Count: > 0 }) { + foreach (var tc in m.ToolCalls) { + Dictionary parsedInput; + try { + parsedInput = string.IsNullOrWhiteSpace(tc.ArgumentsJson) + ? new Dictionary() + : System.Text.Json.JsonSerializer.Deserialize>(tc.ArgumentsJson) + ?? new Dictionary(); + } catch (Exception) { + parsedInput = new Dictionary(); + } + blocks.Add(new ToolUseBlockParam { ID = tc.Id, Name = tc.Name, Input = parsedInput }); + } + } + if (blocks.Count > 0) { + result.Add(new MessageParam { Role = Role.Assistant, Content = blocks }); + } + break; + } + case LlmRole.Tool: + pendingToolResults.Add(new ToolResultBlockParam(m.ToolCallId ?? string.Empty) { + Content = m.Text ?? string.Empty, + IsError = m.IsToolError + }); + break; + } } - _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); + FlushToolResults(); + + // Anthropic requires strictly alternating user/assistant starting with user. + return EnsureAlternatingRoles(result); } - public void CommitToolResults(IReadOnlyList results) { - // Assistant message with text + tool_use blocks, then a user message with the results. - var assistantContentBlocks = new List(); - if (!string.IsNullOrWhiteSpace(_pendingTurnText)) { - assistantContentBlocks.Add(new TextBlockParam(_pendingTurnText)); - } - foreach (var (id, name, inputJson) in _pendingToolCalls) { - Dictionary parsedInput; + private static List ConvertToAnthropicToolSpecs(IReadOnlyList specs, bool enablePromptCaching) { + var tools = new List(); + for (int index = 0; index < specs.Count; index++) { + var spec = specs[index]; try { - parsedInput = string.IsNullOrWhiteSpace(inputJson) - ? new Dictionary() - : System.Text.Json.JsonSerializer.Deserialize>(inputJson); - } catch (Exception ex) { - _svc._logger.LogError( - ex, - "{ServiceName}: Failed to deserialize Anthropic native tool input for assistant history. ToolUseId={ToolUseId}, ToolName={ToolName}, InputJson={InputJson}, ErrorSummary={ErrorSummary}", - _svc.ServiceName, id, name, inputJson, ex.GetLogSummary()); - parsedInput = new Dictionary(); + var schemaDoc = System.Text.Json.JsonDocument.Parse(spec.ParametersJson); + var root = schemaDoc.RootElement; + + var properties = new Dictionary(); + var required = new List(); + + if (root.TryGetProperty("properties", out var propsEl) && propsEl.ValueKind == JsonValueKind.Object) { + foreach (var prop in propsEl.EnumerateObject()) { + properties[prop.Name] = prop.Value.Clone(); + } + } + + if (root.TryGetProperty("required", out var reqEl) && reqEl.ValueKind == JsonValueKind.Array) { + foreach (var item in reqEl.EnumerateArray()) { + required.Add(item.GetString()); + } + } + + var inputSchema = new InputSchema { + Type = System.Text.Json.JsonDocument.Parse("\"object\"").RootElement, + Properties = properties, + Required = required + }; + + tools.Add(new ToolUnion(new Tool { + Name = spec.Name, + Description = spec.Description, + InputSchema = inputSchema, + CacheControl = enablePromptCaching && index == specs.Count - 1 ? CreateCacheControl() : null, + }, null)); + } catch (Exception) { } - assistantContentBlocks.Add(new ToolUseBlockParam { - ID = id, - Name = name, - Input = parsedInput - }); } - _providerHistory.Add(new MessageParam { Role = Role.Assistant, Content = assistantContentBlocks }); - _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = _pendingTurnText }); - - var toolResultBlocks = new List(); - foreach (var result in results) { - toolResultBlocks.Add(new ToolResultBlockParam(result.ToolCallId) { - Content = result.Result, - IsError = result.IsError, - }); - } - _providerHistory.Add(new MessageParam { Role = Role.User, Content = toolResultBlocks }); + return tools; } - - public IReadOnlyList GetTrackedHistory() => _trackedHistory; } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs index 9ec5c7d0..be88838f 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs @@ -391,37 +391,98 @@ public async IAsyncEnumerable ExecAsync( LLMChannel channel, LlmExecutionContext executionContext, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + await foreach (var item in ExecAsync(message, ChatId, modelName, channel, null, executionContext, cancellationToken)) { + yield return item; + } + } + + public async IAsyncEnumerable ExecAsync( + Message message, + long ChatId, + string modelName, + LLMChannel channel, + LLMApiBinding binding, + LlmExecutionContext executionContext, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); if (string.IsNullOrWhiteSpace(modelName)) modelName = "gemini-1.5-flash"; var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); var model = googleAI.CreateGenerativeModel("models/" + modelName); - var fullResponse = new StringBuilder(); if (_llmVisibilityService != null && message != null && await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, cancellationToken)) { - _chatSessions.Remove(ChatId); yield break; } - var history = await GetChatHistory(ChatId, message, await CheckVisionSupport(modelName, channel.Id)); - if (_llmVisibilityService != null && - ( await _llmVisibilityService.GetInvisibleUserIdsAsync(ChatId, cancellationToken) ).Count > 0) { - _chatSessions.Remove(ChatId); + var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); + var supportsVision = await CheckVisionSupport(modelName, channel.Id); + + var botName = await GetBotNameAsync(); + var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); + + var transport = new GeminiTransport(this, model, supportsVision); + var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); + + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "Gemini", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = history, + Tools = null, + Config = new LlmTransportConfig { + ModelName = modelName, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + yield return item; } + } - if (!_chatSessions.TryGetValue(ChatId, out var chatSession)) { - chatSession = model.StartChat(history: history); - _chatSessions[ChatId] = chatSession; + /// + public async IAsyncEnumerable ExecWithHistoryAsync( + IReadOnlyList history, + Message message, + long ChatId, + string modelName, + LLMChannel channel, + LLMApiBinding binding, LlmExecutionContext executionContext, + bool supportsVision, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); + if (string.IsNullOrWhiteSpace(modelName)) modelName = "gemini-1.5-flash"; + + var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); + var model = googleAI.CreateGenerativeModel("models/" + modelName); + + if (_llmVisibilityService != null && + message != null && + await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, cancellationToken)) { + yield break; } - var botName = await GetBotNameAsync(); - var trackedHistory = new List { - new SerializedChatMessage { Role = "system", Content = McpToolHelper.FormatSystemPrompt(botName, ChatId) } - }; + var botName = await GetBotNameAsync(); + var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); + + var transport = new GeminiTransport(this, model, supportsVision); + var projected = LlmHistoryProjector.Project(history, supportsVision, _logger); - var source = new GeminiTurnSource(this, chatSession, trackedHistory); var meta = new LlmToolLoopMeta { ChatId = ChatId, OriginalMessageId = message.MessageId, @@ -431,57 +492,95 @@ await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, can ChannelId = channel.Id }; var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, message.Content, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = projected, + Tools = null, + Config = new LlmTransportConfig { + ModelName = modelName, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } /// - /// Session-based text-protocol turn source for Gemini (ChatSession owns history). - /// Snapshot history is tracked manually alongside the session. + /// Text-protocol transport for Gemini (GenerativeAI session). Seeds the session from the + /// normalized history and streams the newest user message each turn. /// - private sealed class GeminiTurnSource : ILlmTurnSource { + private sealed class GeminiTransport : ILlmTransport { private readonly GeminiService _svc; - private readonly ChatSession _chatSession; - private readonly List _trackedHistory; + private readonly GenerativeModel _model; + private readonly bool _supportsVision; + private ChatSession? _chatSession; - public GeminiTurnSource(GeminiService svc, ChatSession chatSession, List trackedHistory) { + public GeminiTransport(GeminiService svc, GenerativeModel model, bool supportsVision) { _svc = svc; - _chatSession = chatSession; - _trackedHistory = trackedHistory; + _model = model; + _supportsVision = supportsVision; } public bool SupportsNativeTools => false; public async IAsyncEnumerable StreamTurnAsync( - string? userContent, + LlmTurnRequest request, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (_chatSession == null) { + var seedHistory = ToContents(request.History.Take(request.History.Count - 1).ToList()); + _chatSession = _model.StartChat(history: seedHistory); + } + + var lastUser = request.History.LastOrDefault(m => m.Role == LlmRole.User); + var prompt = lastUser?.Text ?? string.Empty; + var turnText = new StringBuilder(); - await foreach (var chunk in _chatSession.StreamContentAsync(userContent ?? string.Empty).WithCancellation(cancellationToken)) { + await foreach (var chunk in _chatSession.StreamContentAsync(prompt).WithCancellation(cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); turnText.Append(chunk.Text); yield return new LlmStreamEvent.TextDelta(chunk.Text); } - var response = turnText.ToString().Trim(); - _trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = userContent ?? string.Empty }); - _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = response }); - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = response, + Text = turnText.ToString().Trim(), StreamedAny = turnText.Length > 0 }); } - public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { - // Session tracks history; tracked snapshot history already updated in StreamTurnAsync. - } - - public void CommitToolResults(IReadOnlyList results) { - // Text protocol: the shared loop sends tool feedback as the next user message. + private List ToContents(List messages) { + var contents = new List(); + foreach (var m in messages) { + if (m.Role == LlmRole.User) { + var role = Roles.User; + if (m.ImagePng != null && _supportsVision) { + var parts = new List(); + if (!string.IsNullOrWhiteSpace(m.Text)) { + parts.Add(new Part { Text = m.Text.Trim() }); + } + parts.Add(new Part { + InlineData = new GenerativeAI.Types.Blob { + MimeType = m.ImageMediaType ?? "image/png", + Data = Convert.ToBase64String(m.ImagePng) + } + }); + contents.Add(new Content { Parts = parts, Role = role }); + } else { + contents.Add(new Content(m.Text?.Trim() ?? string.Empty, role)); + } + } else if (m.Role == LlmRole.Assistant) { + contents.Add(new Content(m.Text?.Trim() ?? string.Empty, Roles.Model)); + } + } + return contents; } - - public IReadOnlyList GetTrackedHistory() => _trackedHistory; } public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryProjector.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryProjector.cs new file mode 100644 index 00000000..29d46923 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryProjector.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Microsoft.Extensions.Logging; +using TelegramSearchBot.Common; +using TelegramSearchBot.Model.AI; + +namespace TelegramSearchBot.Service.AI.LLM { + /// + /// Projects neutral rows into normalized + /// history. Replaces the four per-provider projection loops + /// (which differed only cosmetically in reply/colon punctuation). + /// Grouping rule shared by all legacy implementations: consecutive messages from the + /// same side (bot vs user) accumulate into one message; leading bot messages are skipped. + /// + public static class LlmHistoryProjector { + public static List Project( + IReadOnlyList rows, + bool supportsVision, + ILogger? logger = null) { + var result = new List(); + var str = new StringBuilder(); + var pendingImages = new List(); + AgentHistoryMessage? previous = null; + + void Flush() { + if (previous == null || str.Length == 0) { + str.Clear(); + pendingImages.Clear(); + return; + } + byte[]? image = null; + if (supportsVision && pendingImages.Count > 0) { + image = pendingImages[0]; + } + result.Add(LlmMessage.User(str.ToString(), image, image != null ? "image/png" : null)); + str.Clear(); + pendingImages.Clear(); + } + + foreach (var row in rows) { + if (previous == null && str.Length == 0 && row.FromUserId == Env.BotId) { + previous = row; + continue; + } + + if (previous != null && ( previous.FromUserId == Env.BotId ) != ( row.FromUserId == Env.BotId )) { + Flush(); + } + + str.Append($"[{row.DateTime:yyyy-MM-dd HH:mm:ss zzz}]"); + str.Append(LlmHistoryFormat.FormatSender(row.User, row.FromUserId)); + if (row.ReplyToMessageId != 0) { + str.Append($"(Reply to msg {row.ReplyToMessageId})"); + } + str.Append(':').Append(row.Content ?? string.Empty).Append('\n'); + + if (row.Extensions is { Count: > 0 }) { + str.Append("[扩展信息:"); + foreach (var ext in row.Extensions) { + str.Append($"{ext.Name}={ext.Value}; "); + } + str.Append("]\n"); + } + + if (supportsVision && row.FromUserId != Env.BotId) { + var imageBytes = LlmPhotoLoader.TryLoadMessagePhoto(row.GroupId, row.MessageId, logger); + if (imageBytes != null) { + pendingImages.Add(imageBytes); + } + } + + previous = row; + } + + Flush(); + return result; + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs index efb8f749..be397354 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs @@ -21,21 +21,15 @@ namespace TelegramSearchBot.Service.AI.LLM { /// per-provider projections convert rows to their native message types without touching /// the database. Previously duplicated (with per-message N+1 lookups) in each provider. /// - [Injectable(Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton)] - public class LlmHistoryQueryService : IService { - private readonly DataDbContext _dbContext; - private readonly LlmVisibilityService? _llmVisibilityService; - - public string ServiceName => nameof(LlmHistoryQueryService); - - public LlmHistoryQueryService(DataDbContext dbContext, LlmVisibilityService? llmVisibilityService = null) { - _dbContext = dbContext; - _llmVisibilityService = llmVisibilityService; - } - + public static class LlmHistoryQueryService { /// Rows ordered chronologically; the input message is appended last (when visible), /// matching the legacy per-provider GetChatHistory behavior. - public async Task> LoadAsync(long chatId, DataMessage? inputMessage = null, CancellationToken cancellationToken = default) { + public static async Task> LoadAsync( + DataDbContext _dbContext, + LlmVisibilityService? _llmVisibilityService, + long chatId, + DataMessage? inputMessage = null, + CancellationToken cancellationToken = default) { var messages = await _dbContext.Messages.AsNoTracking() .Where(m => m.GroupId == chatId && m.DateTime > DateTime.UtcNow.AddHours(-1)) .OrderBy(m => m.DateTime) @@ -54,8 +48,8 @@ public async Task> LoadAsync(long chatId, DataMessage? messages = await _llmVisibilityService.FilterVisibleMessagesAsync(chatId, messages, cancellationToken); } - var users = await ResolveUsersAsync(messages, cancellationToken); - var extensionsByMessageId = await ResolveExtensionsAsync(messages, cancellationToken); + var users = await ResolveUsersAsync(_dbContext, messages, cancellationToken); + var extensionsByMessageId = await ResolveExtensionsAsync(_dbContext, messages, cancellationToken); if (inputMessage != null && ( _llmVisibilityService == null || @@ -101,22 +95,22 @@ public async Task> LoadAsync(long chatId, DataMessage? }).ToList(); } - private async Task> ResolveUsersAsync(List messages, CancellationToken cancellationToken) { + private static async Task> ResolveUsersAsync(DataDbContext dbContext, List messages, CancellationToken cancellationToken) { var userIds = messages.Select(x => x.FromUserId).Distinct().ToList(); if (userIds.Count == 0) { return new Dictionary(); } - return await _dbContext.UserData.AsNoTracking() + return await dbContext.UserData.AsNoTracking() .Where(x => userIds.Contains(x.Id)) .ToDictionaryAsync(x => x.Id, cancellationToken); } - private async Task>> ResolveExtensionsAsync(List messages, CancellationToken cancellationToken) { + private static async Task>> ResolveExtensionsAsync(DataDbContext dbContext, List messages, CancellationToken cancellationToken) { var messageIds = messages.Select(x => x.Id).ToList(); if (messageIds.Count == 0) { return new Dictionary>(); } - var extensionRecords = await _dbContext.MessageExtensions.AsNoTracking() + var extensionRecords = await dbContext.MessageExtensions.AsNoTracking() .Where(x => messageIds.Contains(x.MessageDataId)) .ToListAsync(cancellationToken); return extensionRecords diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs index f6770552..3122b3ad 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs @@ -10,7 +10,7 @@ using TelegramSearchBot.Model.Tools; namespace TelegramSearchBot.Service.AI.LLM { - /// Identity data captured into iteration-limit continuation snapshots. + /// Identity + resume data captured into iteration-limit continuation snapshots. public sealed class LlmToolLoopMeta { public long ChatId { get; set; } public long OriginalMessageId { get; set; } @@ -25,34 +25,50 @@ public sealed class LlmToolLoopMeta { public string InitialContent { get; set; } = string.Empty; } + /// Everything one agent run needs. + public sealed class LlmAgentRunRequest { + public ILlmTransport Transport { get; set; } = null!; + public string SystemPrompt { get; set; } = string.Empty; + /// Normalized chat history INCLUDING the input user message (no system entry). + public List History { get; set; } = []; + public IReadOnlyList? Tools { get; set; } + public LlmTransportConfig Config { get; set; } = new(); + public ToolContext ToolContext { get; set; } = new(); + public LlmToolLoopMeta Meta { get; set; } = new(); + public LlmExecutionContext? ExecutionContext { get; set; } + } + /// - /// The single shared agent tool-call loop. Drives turns, - /// executes tools via , yields accumulated-content snapshots, - /// and builds the iteration-limit continuation snapshot. Replaces the per-provider - /// copies of this cycle. + /// The single shared agent tool-call loop. Owns the normalized + /// history, drives the transport turn by turn, executes tools via , + /// yields accumulated-content snapshots, and builds v2 continuation snapshots. /// public static class LlmToolLoop { + private const string MalformedToolCallCorrection = + "Tool call failed before execution due to malformed tool metadata. Please verify the tool name and parameters, then try again."; + public static async IAsyncEnumerable RunAsync( - ILlmTurnSource source, - string? initialUserContent, - ToolContext toolContext, - LlmToolLoopMeta meta, - LlmExecutionContext? executionContext, + LlmAgentRunRequest run, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { var maxToolCycles = Env.MaxToolCycles; - var contentBuilder = new StringBuilder(meta.InitialContent ?? string.Empty); + var contentBuilder = new StringBuilder(run.Meta.InitialContent ?? string.Empty); var baseline = contentBuilder.Length; string NewContent() => contentBuilder.ToString(baseline, contentBuilder.Length - baseline); - - var userContent = initialUserContent; + bool IsNative() => run.Transport.SupportsNativeTools && run.Tools != null && run.Tools.Count > 0; for (int cycle = 0; cycle < maxToolCycles; cycle++) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); LlmTurnResult? turn = null; + var request = new LlmTurnRequest { + SystemPrompt = run.SystemPrompt, + History = run.History, + Tools = IsNative() ? run.Tools : null, + Config = run.Config + }; - await foreach (var evt in source.StreamTurnAsync(userContent, cancellationToken).WithCancellation(cancellationToken)) { + await foreach (var evt in run.Transport.StreamTurnAsync(request, cancellationToken).WithCancellation(cancellationToken)) { switch (evt) { case LlmStreamEvent.TextDelta textDelta: contentBuilder.Append(textDelta.Delta); @@ -72,83 +88,73 @@ public static async IAsyncEnumerable RunAsync( turn ??= new LlmTurnResult(); var turnText = (turn.Text ?? string.Empty).Trim(); - if (source.SupportsNativeTools) { + if (IsNative()) { if (turn.ToolCalls.Count > 0) { - var results = new List(); + run.History.Add(LlmMessage.Assistant(turnText, turn.Reasoning, turn.ToolCalls)); var indicators = new StringBuilder(); foreach (var call in turn.ToolCalls) { var argsDict = call.ParseArguments(); - var (resultString, isError) = await ExecuteToolAsync(call.Name, argsDict, toolContext); - results.Add(new LlmToolResult { - ToolCallId = call.Id, - Name = call.Name, - ArgumentsJson = call.ArgumentsJson, - Result = resultString, - IsError = isError - }); + var (resultString, isError) = await ExecuteToolAsync(call.Name, argsDict, run.ToolContext); + run.History.Add(LlmMessage.ToolResult(call.Id, call.Name, resultString, isError)); indicators.Append(McpToolHelper.FormatToolCallDisplay(call.Name, argsDict)); } contentBuilder.Append(indicators.ToString()); yield return NewContent(); - source.CommitToolResults(results); - userContent = null; continue; } if (turn.MalformedToolCall) { - // The source appended a self-correction message to its own history. - userContent = null; + // Transport reported malformed metadata; ask the model to self-correct. + run.History.Add(LlmMessage.User(MalformedToolCallCorrection)); continue; } - source.CommitAssistantTurn(turnText, turn.Reasoning, turn.StreamedAny); + run.History.Add(LlmMessage.Assistant(turnText, turn.Reasoning)); yield break; } - // Text-embedded tool protocol. - source.CommitAssistantTurn(turnText, turn.Reasoning, turn.StreamedAny); + // Text-embedded (XML) tool protocol. + if (!string.IsNullOrWhiteSpace(turnText)) { + run.History.Add(LlmMessage.Assistant(turnText, turn.Reasoning)); + } if (McpToolHelper.TryParseToolCalls(turnText, out var parsedToolCalls) && parsedToolCalls.Count > 0) { var first = parsedToolCalls[0]; if (parsedToolCalls.Count > 1) { - LogMultipleToolCalls(first.toolName, parsedToolCalls.Count); + Serilog.Log.Warning( + "LLM returned multiple tool calls ({Count}). Only the first one ('{FirstToolName}') will be executed.", + parsedToolCalls.Count, first.toolName); } - var (resultString, isError) = await ExecuteToolAsync(first.toolName, first.arguments, toolContext); + var (resultString, isError) = await ExecuteToolAsync(first.toolName, first.arguments, run.ToolContext); contentBuilder.Append(McpToolHelper.FormatToolCallDisplay(first.toolName, first.arguments)); yield return NewContent(); - userContent = isError + run.History.Add(LlmMessage.User(isError ? $"[Tool '{first.toolName}' Execution Failed. Error: {resultString}]" - : $"[Executed Tool '{first.toolName}'. Result: {resultString}]"; + : $"[Executed Tool '{first.toolName}'. Result: {resultString}]")); continue; } yield break; } - // Iteration limit reached — persist continuation snapshot for user confirmation. - if (executionContext != null) { - executionContext.IterationLimitReached = true; - executionContext.SnapshotData = new LlmContinuationSnapshot { + // Iteration limit reached — persist v2 continuation snapshot for user confirmation. + if (run.ExecutionContext != null) { + run.ExecutionContext.IterationLimitReached = true; + run.ExecutionContext.SnapshotData = new LlmContinuationSnapshot { SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = meta.ChatId, - OriginalMessageId = meta.OriginalMessageId, - UserId = meta.UserId, - ModelName = meta.ModelName, - Provider = meta.Provider, - ChannelId = meta.ChannelId, + ChatId = run.Meta.ChatId, + OriginalMessageId = run.Meta.OriginalMessageId, + UserId = run.Meta.UserId, + ModelName = run.Meta.ModelName, + Provider = run.Meta.Provider, + ChannelId = run.Meta.ChannelId, LastAccumulatedContent = contentBuilder.ToString(), - CyclesSoFar = meta.BaseCycles + maxToolCycles, - ProviderHistory = new List(source.GetTrackedHistory() ?? []), + CyclesSoFar = run.Meta.BaseCycles + maxToolCycles, + NormalizedHistory = new List(run.History) }; } } - private static void LogMultipleToolCalls(string toolName, int count) { - Serilog.Log.Warning( - "LLM returned multiple tool calls ({Count}). Only the first one ('{FirstToolName}') will be executed.", - count, toolName); - } - private static async Task<(string Result, bool IsError)> ExecuteToolAsync( string toolName, Dictionary argsDict, ToolContext toolContext) { try { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs new file mode 100644 index 00000000..87de423e --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs @@ -0,0 +1,42 @@ +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM { + /// Transport identity + credentials + quirks, resolved from LLMChannel + LLMApiBinding. + public sealed class LlmTransportConfig { + public string ModelName { get; set; } = string.Empty; + public string Endpoint { get; set; } = string.Empty; + public string? ApiKey { get; set; } + public LLMProvider Provider { get; set; } + public LlmProtocol? Protocol { get; set; } + public LLMApiBinding? Binding { get; set; } + public LLMChannel? Channel { get; set; } + /// Text-embedded (XML) tool protocol instead of native tool calling. + public bool UseTextToolProtocol { get; set; } + /// Whether vision inputs may be sent (from model capabilities). + public bool SupportsVision { get; set; } + /// OpenAI-family prompt caching enabled (provider-specific). + public bool PromptCachingEnabled { get; set; } + /// OpenAI reasoning_content passthrough quirk (Kimi/DeepSeek thinking models). + public bool IncludeEmptyReasoningContent { get; set; } + } + + /// One transport turn request: normalized history + tools + config. + public sealed class LlmTurnRequest { + public string SystemPrompt { get; set; } = string.Empty; + public IReadOnlyList History { get; set; } = []; + public IReadOnlyList? Tools { get; set; } + public LlmTransportConfig Config { get; set; } = new(); + } + + /// + /// Stateless per-turn transport adapter for one API dialect. Converts the normalized + /// history/tools/config into a provider request, streams one assistant turn, and maps + /// deltas to . One instance per execution run (may hold + /// provider clients and prompt-caching state). + /// + public interface ILlmTransport { + bool SupportsNativeTools { get; } + IAsyncEnumerable StreamTurnAsync(LlmTurnRequest request, CancellationToken cancellationToken = default); + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/McpToolHelper.cs b/TelegramSearchBot.LLM/Service/AI/LLM/McpToolHelper.cs index d2c62cbd..b7857927 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/McpToolHelper.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/McpToolHelper.cs @@ -258,7 +258,19 @@ private static void AppendToolXml(StringBuilder sb, string toolName, MethodInfo /// Generates OpenAI-compatible ChatTool definitions for all registered tools (built-in and external). /// Used for native function/tool calling API instead of XML prompt-based tool calling. /// - public static List GetNativeToolDefinitions() { + /// + /// Native tool definitions in the provider-neutral form consumed by ILlmTransport. + /// + public static List GetLlmToolSpecs() { + return GetNativeToolDefinitions().Select(t => new LlmToolSpec { + Name = t.FunctionName ?? string.Empty, + Description = t.FunctionDescription ?? string.Empty, + ParametersJson = t.FunctionParameters?.ToString() ?? "{}", + StrictSchema = t.FunctionSchemaIsStrict == true + }).ToList(); + } + +public static List GetNativeToolDefinitions() { var tools = new List(); // Built-in tools diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs index d5815574..cdb8049c 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs @@ -115,8 +115,7 @@ public async Task CheckAndPullModelAsync(OllamaApiClient ollama, string mo } } - // --- Main Execution Logic (Using OllamaSharp.Chat helper) --- - public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, + public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { var executionContext = new LlmExecutionContext(); await foreach (var item in ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken)) { @@ -161,18 +160,81 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long } ollama.SelectedModel = modelName; - // --- History and Prompt Setup --- - // NOTE: History context is limited as OllamaSharp.Chat manages it. + var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, null, ChatId, message, cancellationToken); var botName = await GetBotNameAsync(); var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - var chat = new OllamaSharp.Chat(ollama, systemPrompt); + var transport = new OllamaTransport(this, ollama, systemPrompt); + var history = LlmHistoryProjector.Project(rows, supportsVision: false, _logger); - // Track history explicitly for snapshot serialization - var trackedHistory = new List(); - trackedHistory.Add(new SerializedChatMessage { Role = "system", Content = systemPrompt }); + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "Ollama", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = history, + Tools = null, + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = false + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + yield return item; + } + } + + /// + public async IAsyncEnumerable ExecWithHistoryAsync( + IReadOnlyList history, + Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, + LLMApiBinding binding, LlmExecutionContext executionContext, + bool supportsVision, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); + modelName = modelName ?? Env.OllamaModelName; + if (string.IsNullOrWhiteSpace(modelName)) { + _logger.LogError("{ServiceName}: Model name is not configured.", ServiceName); + yield return $"Error: {ServiceName} model name is not configured."; + yield break; + } + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint)) { + _logger.LogError("{ServiceName}: Channel or Gateway is not configured.", ServiceName); + yield return $"Error: {ServiceName} channel/gateway is not configured."; + yield break; + } + + HttpClient httpClient = _httpClientFactory?.CreateClient("OllamaClient") ?? new HttpClient(); + httpClient.BaseAddress = new Uri(endpoint); + var ollama = new OllamaApiClient(httpClient, modelName); + + if (!await CheckAndPullModelAsync(ollama, modelName)) { + yield return $"Error: Could not check or pull Ollama model '{modelName}'."; + yield break; + } + ollama.SelectedModel = modelName; + + var botName = await GetBotNameAsync(); + var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); + + var transport = new OllamaTransport(this, ollama, systemPrompt); + var projected = LlmHistoryProjector.Project(history, supportsVision: false, _logger); - var source = new OllamaTurnSource(this, chat, trackedHistory); var meta = new LlmToolLoopMeta { ChatId = ChatId, OriginalMessageId = message.MessageId, @@ -182,36 +244,68 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChannelId = channel.Id }; var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, message.Content, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = projected, + Tools = null, + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = false + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } /// - /// Session-based text-protocol turn source for Ollama (OllamaSharp.Chat owns history). - /// Snapshot history is tracked manually alongside the session. + /// Text-protocol transport for Ollama (OllamaSharp session). Seeds the session from the + /// normalized history and streams the newest user message each turn. /// - private sealed class OllamaTurnSource : ILlmTurnSource { + private sealed class OllamaTransport : ILlmTransport { private readonly OllamaService _svc; - private readonly OllamaSharp.Chat _chat; - private readonly List _trackedHistory; + private readonly OllamaApiClient _ollama; + private readonly string _systemPrompt; + private OllamaSharp.Chat? _chat; - public OllamaTurnSource(OllamaService svc, OllamaSharp.Chat chat, List trackedHistory) { + public OllamaTransport(OllamaService svc, OllamaApiClient ollama, string systemPrompt) { _svc = svc; - _chat = chat; - _trackedHistory = trackedHistory; + _ollama = ollama; + _systemPrompt = systemPrompt; } public bool SupportsNativeTools => false; public async IAsyncEnumerable StreamTurnAsync( - string? userContent, + LlmTurnRequest request, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - _trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = userContent ?? string.Empty }); + if (_chat == null) { + _chat = new OllamaSharp.Chat(_ollama, _systemPrompt); + // Seed the session with all history except the newest user message. + for (int i = 0; i < request.History.Count - 1; i++) { + var m = request.History[i]; + if (m.Role == LlmRole.User) { + _chat.Messages.Add(new OllamaSharp.Models.Chat.Message { Role = ChatRole.User, Content = m.Text ?? string.Empty }); + } else if (m.Role == LlmRole.Assistant) { + _chat.Messages.Add(new OllamaSharp.Models.Chat.Message { Role = ChatRole.Assistant, Content = m.Text ?? string.Empty }); + } + } + } + + var lastUser = request.History.LastOrDefault(m => m.Role == LlmRole.User); + var prompt = lastUser?.Text ?? string.Empty; var turnText = new StringBuilder(); var streamedAny = false; - await foreach (var token in _chat.SendAsync(userContent ?? string.Empty, cancellationToken).WithCancellation(cancellationToken)) { + await foreach (var token in _chat.SendAsync(prompt, cancellationToken).WithCancellation(cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); turnText.Append(token); streamedAny = true; @@ -227,22 +321,8 @@ public async IAsyncEnumerable StreamTurnAsync( StreamedAny = streamedAny }); } - - public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { - if (streamedAny) { - _trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = text }); - } - } - - public void CommitToolResults(IReadOnlyList results) { - // Text protocol: the shared loop sends tool feedback as the next user message. - } - - public IReadOnlyList GetTrackedHistory() => _trackedHistory; } - // ConvertToolResultToString has been moved to McpToolHelper - public virtual async Task> GetAllModels(LLMChannel channel) { if (channel == null || string.IsNullOrWhiteSpace(channel.Gateway)) { return Enumerable.Empty(); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs index 36a61d7f..313064a6 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs @@ -202,12 +202,8 @@ public async IAsyncEnumerable ExecAsync( LLMApiBinding binding, LlmExecutionContext executionContext, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; - if (string.IsNullOrWhiteSpace(modelName)) { - _logger.LogError("{ServiceName}: Model name is not configured.", ServiceName); - yield return $"Error: {ServiceName} model name is not configured."; - yield break; - } + using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { @@ -216,68 +212,70 @@ public async IAsyncEnumerable ExecAsync( yield break; } - await foreach (var item in ExecWithResponsesApiAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { + var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); + var supportsVision = await CheckVisionSupport(modelName, channel.Id); + var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); + + var transport = new ResponsesTransport(this, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); + var botName = await GetBotNameAsync(); + var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); + + var meta = new LlmToolLoopMeta { + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "OpenAIResponses", + ChannelId = channel.Id + }; + var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId), + History = history, + Tools = McpToolHelper.GetLlmToolSpecs(), + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision, + PromptCachingEnabled = promptCachingEnabled + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } - // ======================================================================== - // Core Responses API Execution - // ======================================================================== - - private async IAsyncEnumerable ExecWithResponsesApiAsync( + /// + public async IAsyncEnumerable ExecWithHistoryAsync( + IReadOnlyList history, Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, + LLMApiBinding binding, LlmExecutionContext executionContext, + bool supportsVision, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - // --- Build system instructions --- - var botName = await GetBotNameAsync(); - string instructions = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId); - - // --- Get native tool definitions and convert to ResponseTool format --- - var nativeToolDefs = McpToolHelper.GetNativeToolDefinitions(); - var responseTools = new List(); - if (nativeToolDefs != null) { - foreach (var chatTool in nativeToolDefs) { - var funcTool = new FunctionTool( - chatTool.FunctionName, - chatTool.FunctionParameters, - chatTool.FunctionSchemaIsStrict - ) { - FunctionDescription = chatTool.FunctionDescription - }; - responseTools.Add(funcTool); - } + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { + _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); + yield return $"Error: {ServiceName} channel/gateway/apikey is not configured."; + yield break; } - bool supportsVision = await CheckVisionSupport(modelName, channel.Id); - var inputItems = await BuildResponseInputItemsAsync(ChatId, message, supportsVision); - var shouldObservePromptCaching = channel.Provider == LLMProvider.ResponsesAPI; - var promptCachingEnabled = shouldObservePromptCaching && await IsPromptCachingEnabledAsync(); - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = BuildPromptCachingContext( - "OpenAIResponses", - modelName, - "responses", - instructions, - inputItems, - excludeDynamicTail: true); - - // --- Create Responses client --- - using var httpClient = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, channel, binding, $"tsb-{ChatId}"); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(LlmBindingSupport.ResolveEndpoint(channel, binding)), - Transport = new HttpClientPipelineTransport(httpClient), - }; - var apiKey = new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)); - var responsesClient = new ResponsesClient(apiKey, clientOptions); + var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); + + var transport = new ResponsesTransport(this, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); + var botName = await GetBotNameAsync(); + var projected = LlmHistoryProjector.Project(history, supportsVision, _logger); - var source = new ResponsesTurnSource( - this, responsesClient, responseTools, instructions, inputItems, - shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, - modelName, channel); var meta = new LlmToolLoopMeta { ChatId = ChatId, OriginalMessageId = message.MessageId, @@ -287,7 +285,26 @@ private async IAsyncEnumerable ExecWithResponsesApiAsync( ChannelId = channel.Id }; var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId), + History = projected, + Tools = McpToolHelper.GetLlmToolSpecs(), + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision, + PromptCachingEnabled = promptCachingEnabled + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } @@ -315,6 +332,10 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( _logger.LogError("{ServiceName}: Cannot resume from null snapshot.", ServiceName); yield break; } + if (snapshot.NormalizedHistory is not { Count: > 0 } savedHistory) { + _logger.LogError("{ServiceName}: Snapshot {SnapshotId} has no v2 normalized history (legacy v1 snapshots expire via TTL).", ServiceName, snapshot.SnapshotId); + yield break; + } var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); var resolvedApiKey = LlmBindingSupport.ResolveApiKey(channel, binding); if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(resolvedApiKey))) { @@ -326,50 +347,20 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; _logger.LogInformation("{ServiceName}: Resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", - ServiceName, snapshot.SnapshotId, snapshot.ChatId, snapshot.ProviderHistory?.Count ?? 0); + ServiceName, snapshot.SnapshotId, snapshot.ChatId, savedHistory.Count); - var inputItems = DeserializeResponseItemsFromSnapshot(snapshot.ProviderHistory); - var botName = await GetBotNameAsync(); - string instructions = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, snapshot.ChatId); - var shouldObservePromptCaching = channel.Provider == LLMProvider.ResponsesAPI; - var promptCachingEnabled = shouldObservePromptCaching && await IsPromptCachingEnabledAsync(); - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = BuildPromptCachingContext( - "OpenAIResponses", - modelName, - "responses-resume", - instructions, - inputItems, - excludeDynamicTail: false); - - // Get tools - var nativeToolDefs = McpToolHelper.GetNativeToolDefinitions(); - var responseTools = new List(); - if (nativeToolDefs != null) { - foreach (var chatTool in nativeToolDefs) { - var funcTool = new FunctionTool( - chatTool.FunctionName, - chatTool.FunctionParameters, - chatTool.FunctionSchemaIsStrict - ) { - FunctionDescription = chatTool.FunctionDescription - }; - responseTools.Add(funcTool); - } + string systemPrompt; + if (savedHistory[0].Role == LlmRole.System) { + systemPrompt = savedHistory[0].Text ?? string.Empty; + savedHistory = savedHistory.Skip(1).ToList(); + } else { + var botName = await GetBotNameAsync(); + systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, snapshot.ChatId); } - using var httpClient = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, channel, binding, $"tsb-{snapshot.ChatId}"); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(LlmBindingSupport.ResolveEndpoint(channel, binding)), - Transport = new HttpClientPipelineTransport(httpClient), - }; - var apiKey = new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)); - var responsesClient = new ResponsesClient(apiKey, clientOptions); + var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); + var transport = new ResponsesTransport(this, endpoint, resolvedApiKey, binding, channel, false, promptCachingEnabled); - var source = new ResponsesTurnSource( - this, responsesClient, responseTools, instructions, inputItems, - shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, - modelName, channel); var meta = new LlmToolLoopMeta { ChatId = snapshot.ChatId, OriginalMessageId = snapshot.OriginalMessageId, @@ -381,68 +372,105 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( InitialContent = snapshot.LastAccumulatedContent ?? string.Empty }; var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = savedHistory, + Tools = McpToolHelper.GetLlmToolSpecs(), + Config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = resolvedApiKey, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = false, + PromptCachingEnabled = promptCachingEnabled + }, + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } /// - /// Per-run turn source for the OpenAI Responses API. Owns the input item list, - /// streams one turn per call, and surfaces completed function calls to the shared loop. + /// Native transport for the OpenAI Responses API: converts the normalized history to + /// input items per turn and streams one assistant turn (text + function calls). /// - private sealed class ResponsesTurnSource : ILlmTurnSource { + private sealed class ResponsesTransport : ILlmTransport { private readonly OpenAIResponsesService _svc; - private readonly ResponsesClient _responsesClient; - private readonly List _responseTools; - private readonly string _instructions; - private readonly List _inputItems; - private readonly bool _shouldObservePromptCaching; - private readonly bool _promptCachingEnabled; - private readonly string _toolDefinitionHash; - private readonly string _stablePrefixHash; - private readonly string _promptCacheKey; - private readonly string _modelName; + private readonly string _endpoint; + private readonly string _apiKey; + private readonly LLMApiBinding? _binding; private readonly LLMChannel _channel; + private readonly bool _supportsVision; + private readonly bool _promptCachingEnabled; + private ResponsesClient? _client; - public ResponsesTurnSource( - OpenAIResponsesService svc, ResponsesClient responsesClient, List responseTools, - string instructions, List inputItems, - bool shouldObservePromptCaching, bool promptCachingEnabled, - string toolDefinitionHash, string stablePrefixHash, string promptCacheKey, - string modelName, LLMChannel channel) { + public ResponsesTransport(OpenAIResponsesService svc, string endpoint, string apiKey, + LLMApiBinding? binding, LLMChannel channel, bool supportsVision, bool promptCachingEnabled) { _svc = svc; - _responsesClient = responsesClient; - _responseTools = responseTools; - _instructions = instructions; - _inputItems = inputItems; - _shouldObservePromptCaching = shouldObservePromptCaching; - _promptCachingEnabled = promptCachingEnabled; - _toolDefinitionHash = toolDefinitionHash; - _stablePrefixHash = stablePrefixHash; - _promptCacheKey = promptCacheKey; - _modelName = modelName; + _endpoint = endpoint; + _apiKey = apiKey; + _binding = binding; _channel = channel; + _supportsVision = supportsVision; + _promptCachingEnabled = promptCachingEnabled; } public bool SupportsNativeTools => true; public async IAsyncEnumerable StreamTurnAsync( - string? userContent, + LlmTurnRequest request, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (_client == null) { + // ponytail: HttpClient outlives this scope (transport persists per run); factory-managed. + var httpClient = _svc._httpClientFactory.CreateClient(); + OpencodeSessionHeaders.Apply(httpClient, _channel, _binding, $"tsb-{_channel?.Id}"); + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(_endpoint), + Transport = new HttpClientPipelineTransport(httpClient), + }; + _client = new ResponsesClient(new ApiKeyCredential(_apiKey), clientOptions); + } + + var inputItems = ToInputItems(request); + var instructions = request.SystemPrompt; + + var shouldObservePromptCaching = _channel.Provider == LLMProvider.ResponsesAPI; + var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; + var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIResponsesService.BuildPromptCachingContext( + "OpenAIResponses", + request.Config.ModelName, + "responses", + instructions, + inputItems, + excludeDynamicTail: true); + var options = new CreateResponseOptions { - Model = _modelName, - Instructions = _instructions, + Model = request.Config.ModelName, + Instructions = instructions, StreamingEnabled = true, }; var cacheKeyAttached = false; - if (_promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(options, _promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); + if (promptCachingEnabled) { + PromptCachingHelper.ApplyOpenAiPromptCaching(options, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); cacheKeyAttached = true; } - foreach (var tool in _responseTools) { - options.Tools.Add(tool); + if (request.Tools is { Count: > 0 }) { + foreach (var spec in request.Tools) { + options.Tools.Add(new FunctionTool( + spec.Name, + BinaryData.FromString(spec.ParametersJson), + spec.StrictSchema) { + FunctionDescription = spec.Description + }); + } } - foreach (var item in _inputItems) { + foreach (var item in inputItems) { options.InputItems.Add(item); } @@ -453,22 +481,11 @@ public async IAsyncEnumerable StreamTurnAsync( ResponseResult completedResult = null; var lastUiLength = 0; var streamedAny = false; - var streamingError = false; - string streamingErrorMessage = null; - await foreach (var update in _responsesClient.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { + await foreach (var update in _client.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - try { - OpenAIResponsesService.ProcessStreamingUpdate(update, textBuilder, uiBuilder, reasoningBuilder, toolCallAccums, ref completedResult); - } catch (OperationCanceledException) { - throw; - } catch (Exception ex) { - _svc._logger.LogError(ex, "{ServiceName}: Error during streaming update", _svc.ServiceName); - streamingError = true; - streamingErrorMessage = ex.Message; - break; - } + OpenAIResponsesService.ProcessStreamingUpdate(update, textBuilder, uiBuilder, reasoningBuilder, toolCallAccums, ref completedResult); if (uiBuilder.Length > lastUiLength) { var delta = uiBuilder.ToString(lastUiLength, uiBuilder.Length - lastUiLength); @@ -478,62 +495,37 @@ public async IAsyncEnumerable StreamTurnAsync( } } - if (_shouldObservePromptCaching) { + if (shouldObservePromptCaching) { _svc.LogPromptCachingObservation( "OpenAIResponses", _channel, - _modelName, - _promptCachingEnabled, - _toolDefinitionHash, - _stablePrefixHash, - _promptCacheKey, + request.Config.ModelName, + promptCachingEnabled, + toolDefinitionHash, + stablePrefixHash, + promptCacheKey, completedResult?.Usage, cacheKeyAttached); } var responseText = textBuilder.ToString().Trim(); var reasoningContent = reasoningBuilder.ToString().Trim(); - var toolCalls = new List(); - - if (streamingError) { - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = $"{responseText}\n[Error: {streamingErrorMessage}]", - StreamedAny = streamedAny - }); - yield break; - } + var toolCalls = new List(); - var completedFuncCalls = new List(); if (completedResult?.OutputItems != null) { foreach (var outputItem in completedResult.OutputItems) { if (outputItem is FunctionCallResponseItem fcItem && !string.IsNullOrWhiteSpace(fcItem.CallId) && !string.IsNullOrWhiteSpace(fcItem.FunctionName)) { - completedFuncCalls.Add(fcItem); + toolCalls.Add(new LlmToolCall { + Id = OpenAIService.NormalizeToolCallId(fcItem.CallId), + Name = OpenAIService.NormalizeToolCallName(fcItem.FunctionName), + ArgumentsJson = OpenAIService.NormalizeToolCallArguments(fcItem.FunctionArguments?.ToString() ?? "{}") + }); } } } - if (completedFuncCalls.Any()) { - // Assistant text (if any) + function call items enter history at commit time. - foreach (var funcCall in completedFuncCalls) { - var callId = OpenAIService.NormalizeToolCallId(funcCall.CallId); - var name = OpenAIService.NormalizeToolCallName(funcCall.FunctionName); - var argsJson = OpenAIService.NormalizeToolCallArguments(funcCall.FunctionArguments?.ToString() ?? "{}"); - toolCalls.Add(new LlmNativeToolCall { - Id = callId, - Name = name, - ArgumentsJson = argsJson - }); - } - _pendingAssistantText = responseText; - } else { - _pendingAssistantText = null; - if (!string.IsNullOrWhiteSpace(responseText)) { - _inputItems.Add(ResponseItem.CreateAssistantMessageItem(responseText)); - } - } - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = responseText, Reasoning = reasoningContent, @@ -542,178 +534,397 @@ public async IAsyncEnumerable StreamTurnAsync( }); } - private string _pendingAssistantText; + private List ToInputItems(LlmTurnRequest request) { + var inputItems = new List(); + foreach (var m in request.History) { + switch (m.Role) { + case LlmRole.User: { + var images = m.ImagePng != null && request.Config.SupportsVision + ? new List { m.ImagePng } + : null; + _svc.AddResponseItemFromAccumulated(inputItems, 1, m.Text ?? string.Empty, images); + break; + } + case LlmRole.Assistant: + if (!string.IsNullOrWhiteSpace(m.Text)) { + inputItems.Add(ResponseItem.CreateAssistantMessageItem(m.Text)); + } + if (m.ToolCalls is { Count: > 0 }) { + foreach (var tc in m.ToolCalls) { + inputItems.Add(ResponseItem.CreateFunctionCallItem( + tc.Id, tc.Name, BinaryData.FromString(tc.ArgumentsJson))); + } + } + break; + case LlmRole.Tool: + inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(m.ToolCallId ?? string.Empty, m.Text ?? string.Empty)); + break; + } + } + return inputItems; + } + } - public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { - // Final text was already appended to input items during StreamTurnAsync. + private void AddResponseItemFromAccumulated( + List inputItems, long fromUserId, string content, List images) { + if (string.IsNullOrWhiteSpace(content) && (images == null || images.Count == 0)) return; + if (!string.IsNullOrWhiteSpace(content)) { + content = System.Text.RegularExpressions.Regex.Replace(content.Trim(), @"\n{3,}", "\n\n"); } - public void CommitToolResults(IReadOnlyList results) { - if (!string.IsNullOrWhiteSpace(_pendingAssistantText)) { - _inputItems.Add(ResponseItem.CreateAssistantMessageItem(_pendingAssistantText)); + if (fromUserId == Env.BotId) { + // Assistant message + if (!string.IsNullOrWhiteSpace(content)) { + inputItems.Add(ResponseItem.CreateAssistantMessageItem(content)); } - foreach (var result in results) { - _inputItems.Add(ResponseItem.CreateFunctionCallItem( - result.ToolCallId, - result.Name, - BinaryData.FromString("{}"))); - _inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(result.ToolCallId, result.Result)); + } else { + // User message (possibly with images) + if (images != null && images.Count > 0) { + var parts = new List(); + if (!string.IsNullOrWhiteSpace(content)) { + parts.Add(ResponseContentPart.CreateInputTextPart(content.Trim())); + } + foreach (var imageBytes in images) { + parts.Add(ResponseContentPart.CreateInputImagePart( + BinaryData.FromBytes(imageBytes), null)); + } + inputItems.Add(ResponseItem.CreateUserMessageItem((IEnumerable)parts)); + } else { + inputItems.Add(ResponseItem.CreateUserMessageItem(content.Trim())); } - _pendingAssistantText = null; - } - - public IReadOnlyList GetTrackedHistory() { - return OpenAIResponsesService.SerializeInputItems(_inputItems); } } // ======================================================================== - // Embeddings (same approach as OpenAIService) + // Helper: Vision support check // ======================================================================== - public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { - return await GenerateEmbeddingsAsync(text, modelName, channel, null); - } - - public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel, LLMApiBinding binding) { - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); - throw new InvalidOperationException($"Error: {ServiceName} channel/gateway/apikey is not configured."); - } - - using var httpClient = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, endpoint); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(endpoint), - Transport = new HttpClientPipelineTransport(httpClient), - }; - var credential = new ApiKeyCredential(apiKey); - OpenAIClient client = new(credential, clientOptions); + private async Task CheckVisionSupport(string modelName, int channelId) { try { - var embeddingClient = client.GetEmbeddingClient(modelName); - var response = await embeddingClient.GenerateEmbeddingsAsync(new[] { text }); - - if (response?.Value != null && response.Value.Any()) { - var embedding = response.Value.First(); - - // Try reflection to extract float array - var embeddingProp = embedding.GetType().GetProperty("Embedding") - ?? embedding.GetType().GetProperty("EmbeddingVector") - ?? embedding.GetType().GetProperty("Vector") - ?? embedding.GetType().GetProperty("EmbeddingData") - ?? embedding.GetType().GetProperty("Data"); - - if (embeddingProp != null) { - var embeddingValue = embeddingProp.GetValue(embedding); - if (embeddingValue is float[] floatArray) { - return floatArray; - } else if (embeddingValue is IEnumerable floatEnumerable) { - return floatEnumerable.ToArray(); - } else if (embeddingValue is IReadOnlyList floatList) { - return floatList.ToArray(); - } - } - - // Last resort - find any float[] property - var floatArrayProps = embedding.GetType().GetProperties() - .Where(p => p.PropertyType == typeof(float[]) || p.PropertyType == typeof(IEnumerable)) - .ToList(); - if (floatArrayProps.Any()) { - foreach (var prop in floatArrayProps) { - var value = prop.GetValue(embedding); - if (value is float[] floats) { - return floats; - } else if (value is IEnumerable floatEnumerable) { - return floatEnumerable.ToArray(); - } - } - } + var channelWithModel = await _dbContext.ChannelsWithModel + .Include(c => c.Capabilities) + .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); - _logger.LogError("Failed to extract embedding data. Available properties: {Props}", - string.Join(", ", embedding.GetType().GetProperties().Select(p => $"{p.Name}:{p.PropertyType.Name}"))); + if (channelWithModel?.Capabilities != null) { + return channelWithModel.Capabilities.Any(c => + c.CapabilityName == "vision" && c.CapabilityValue == "true"); } - - _logger.LogError("OpenAI Embeddings API returned null or empty response"); - throw new Exception("OpenAI Embeddings API returned null or empty response"); + return false; } catch (Exception ex) { - _logger.LogError(ex, "Error calling OpenAI Embeddings API"); - throw; + _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); + return false; } } // ======================================================================== - // Model Listing (reuses OpenAI SDK model client) + // Helper: IsSameSender // ======================================================================== - public async Task> GetAllModels(LLMChannel channel) { - if (channel.Provider == LLMProvider.Ollama) { - return new List(); - } - - try { - var handler = new HttpClientHandler { - Proxy = WebRequest.DefaultWebProxy, - UseProxy = true - }; - using var httpClient = new HttpClient(handler); - OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); - - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(channel.Gateway), - Transport = new HttpClientPipelineTransport(httpClient), - }; - var apiKey = new ApiKeyCredential(channel.ApiKey); - OpenAIClient client = new(apiKey, clientOptions); - var model = client.GetOpenAIModelClient(); - var models = await model.GetModelsAsync(); - return models.Value.Select(s => s.Id); - } catch (Exception ex) { - _logger.LogError(ex, "Error getting OpenAI model list (Gateway: {Gateway})", channel.Gateway); - return new List(); - } + public bool IsSameSender(Message message1, Message message2) { + if (message1 == null || message2 == null) return false; + bool msg1IsUser = message1.FromUserId != Env.BotId; + bool msg2IsUser = message2.FromUserId != Env.BotId; + return msg1IsUser == msg2IsUser; } - public async Task> GetAllModels(LLMChannel channel, LLMApiBinding binding) { - if (channel == null) return new List(); - if (binding == null) return await GetAllModels(channel); - if (channel.Provider == LLMProvider.Ollama) { - return new List(); - } + // ======================================================================== + // Helper: Load message photo + // ======================================================================== - // binding 路由:确定性 endpoint,不做品牌/URL 猜测(blueprint §六.7) + private byte[] TryLoadMessagePhoto(long chatId, long messageId) { try { - var handler = new HttpClientHandler { - Proxy = WebRequest.DefaultWebProxy, - UseProxy = true - }; - using var httpClient = new HttpClient(handler); - OpencodeSessionHeaders.Apply(httpClient, channel, binding); + var dirPath = Path.Combine(Env.WorkDir, "Photos", $"{chatId}"); + if (!Directory.Exists(dirPath)) return null; - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(LlmBindingSupport.ResolveEndpoint(channel, binding)), - Transport = new HttpClientPipelineTransport(httpClient), - }; - var apiKey = new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)); - OpenAIClient client = new(apiKey, clientOptions); - var model = client.GetOpenAIModelClient(); - var models = await model.GetModelsAsync(); - return models.Value.Select(s => s.Id); + var files = Directory.GetFiles(dirPath, $"{messageId}.*"); + if (files.Length == 0) return null; + + var filePath = files[0]; + using var fileStream = File.OpenRead(filePath); + var bitmap = SKBitmap.Decode(fileStream); + if (bitmap == null) return null; + + var encoded = bitmap.Encode(SKEncodedImageFormat.Png, 90); + return encoded?.ToArray(); } catch (Exception ex) { - _logger.LogError(ex, "Error getting OpenAI model list (Gateway: {Gateway})", LlmBindingSupport.ResolveEndpoint(channel, binding)); - return new List(); + _logger.LogDebug(ex, "无法加载消息图片: ChatId={ChatId}, MessageId={MessageId}", chatId, messageId); + return null; } } - public async Task IsHealthyAsync(LLMChannel channel, LLMApiBinding binding) { - var models = await GetAllModels(channel, binding); - return models.Any(); - } + // ======================================================================== + // Helper: Model capability parsing (from OpenAIService) + // ======================================================================== - public async Task> GetAllModelsWithCapabilities(LLMChannel channel) { - using var httpClient = _httpClientFactory.CreateClient(); + + private static void ProcessStreamingUpdate( + StreamingResponseUpdate update, + StringBuilder textBuilder, + StringBuilder contentBuilder, + StringBuilder reasoningBuilder, + Dictionary toolCallAccums, + ref ResponseResult completedResult) { + + switch (update) { + case StreamingResponseOutputTextDeltaUpdate textDelta: + textBuilder.Append(textDelta.Delta); + contentBuilder.Append(textDelta.Delta); + break; + + case StreamingResponseReasoningTextDeltaUpdate reasoningDelta: + reasoningBuilder.Append(reasoningDelta.Delta); + break; + + case StreamingResponseRefusalDeltaUpdate refusalDelta: + textBuilder.Append(refusalDelta.Delta); + contentBuilder.Append(refusalDelta.Delta); + break; + + case StreamingResponseFunctionCallArgumentsDeltaUpdate funcDelta: + int idx = funcDelta.OutputIndex; + if (!toolCallAccums.ContainsKey(idx)) { + toolCallAccums[idx] = new ResponsesToolCallAccumulator(); + } + var deltaStr = funcDelta.Delta?.ToString(); + if (!string.IsNullOrEmpty(deltaStr)) { + toolCallAccums[idx].Arguments.Append(deltaStr); + } + break; + + case StreamingResponseOutputItemAddedUpdate itemAdded: + if (itemAdded.Item is FunctionCallResponseItem funcItem) { + int addIdx = itemAdded.OutputIndex; + if (!toolCallAccums.ContainsKey(addIdx)) { + toolCallAccums[addIdx] = new ResponsesToolCallAccumulator(); + } + toolCallAccums[addIdx].CallId ??= funcItem.CallId; + toolCallAccums[addIdx].Name ??= funcItem.FunctionName; + } + break; + + case StreamingResponseCompletedUpdate completed: + completedResult = completed.Response; + break; + } + } + + + private static void ProcessResumeStreamingUpdate( + StreamingResponseUpdate update, + StringBuilder textBuilder, + StringBuilder fullContentBuilder, + StringBuilder newContentBuilder, + Dictionary toolCallAccums, + ref ResponseResult completedResult) { + + switch (update) { + case StreamingResponseOutputTextDeltaUpdate textDelta: + textBuilder.Append(textDelta.Delta); + fullContentBuilder.Append(textDelta.Delta); + newContentBuilder.Append(textDelta.Delta); + break; + + case StreamingResponseReasoningTextDeltaUpdate: + // Reasoning content was already shown in the original stream; + // no need to append to newContentBuilder during resume. + break; + + case StreamingResponseRefusalDeltaUpdate refusalDelta: + textBuilder.Append(refusalDelta.Delta); + fullContentBuilder.Append(refusalDelta.Delta); + newContentBuilder.Append(refusalDelta.Delta); + break; + + case StreamingResponseFunctionCallArgumentsDeltaUpdate funcDelta: + int idx = funcDelta.OutputIndex; + if (!toolCallAccums.ContainsKey(idx)) { + toolCallAccums[idx] = new ResponsesToolCallAccumulator(); + } + var deltaStr = funcDelta.Delta?.ToString(); + if (!string.IsNullOrEmpty(deltaStr)) { + toolCallAccums[idx].Arguments.Append(deltaStr); + } + break; + + case StreamingResponseOutputItemAddedUpdate itemAdded: + if (itemAdded.Item is FunctionCallResponseItem funcItem) { + int addIdx = itemAdded.OutputIndex; + if (!toolCallAccums.ContainsKey(addIdx)) { + toolCallAccums[addIdx] = new ResponsesToolCallAccumulator(); + } + toolCallAccums[addIdx].CallId ??= funcItem.CallId; + toolCallAccums[addIdx].Name ??= funcItem.FunctionName; + } + break; + + case StreamingResponseCompletedUpdate completed: + completedResult = completed.Response; + break; + } + } + + + // ======================================================================== + // Snapshot Serialization + // ======================================================================== + + private const string FuncCallMarker = "__FUNC_CALL__||"; + private const string FuncOutputMarker = "__FUNC_OUTPUT__||"; + + /// + /// Serialize ResponseItem list to portable format for snapshot persistence. + /// Preserves function call structure via marker prefixes. + /// + private static List SerializeInputItems(List inputItems) { + var result = new List(); + foreach (var item in inputItems) { + string role; + string content = ""; + + if (item is MessageResponseItem msgItem) { + switch (msgItem.Role) { + case MessageRole.User: + role = "user"; + break; + case MessageRole.Assistant: + role = "assistant"; + break; + default: + role = "user"; + break; + } + content = string.Join("", msgItem.Content?.Select(p => p.Text) ?? Enumerable.Empty()); + } else if (item is FunctionCallResponseItem funcCallItem) { + role = "__func_call__"; + // Format: __FUNC_CALL__||callId||name||argsJson + content = $"{FuncCallMarker}{funcCallItem.CallId ?? ""}||{funcCallItem.FunctionName ?? ""}||{funcCallItem.FunctionArguments?.ToString() ?? "{}"}"; + } else if (item is FunctionCallOutputResponseItem funcOutputItem) { + role = "__func_output__"; + // Format: __FUNC_OUTPUT__||callId||output + content = $"{FuncOutputMarker}{funcOutputItem.CallId ?? ""}||{funcOutputItem.FunctionOutput ?? ""}"; + } else { + role = "user"; + content = item.ToString(); + } + + result.Add(new SerializedChatMessage { Role = role, Content = content }); + } + return result; + } + + /// + /// Deserialize portable format back to ResponseItem list. + /// Reconstructs function call items from marker-prefixed content. + /// + private static List DeserializeResponseItemsFromSnapshot(List serialized) { + var result = new List(); + if (serialized == null) return result; + + var lastResolvedCallId = string.Empty; + foreach (var msg in serialized) { + string content = msg.Content ?? ""; + + // Check for function call markers first + if (msg.Role == "__func_call__" || content.StartsWith(FuncCallMarker)) { + // Format: __FUNC_CALL__||callId||name||argsJson + var payload = content.StartsWith(FuncCallMarker) + ? content.Substring(FuncCallMarker.Length) + : content; + var parts = payload.Split(new[] { "||" }, 3, StringSplitOptions.None); + string rawCallId = parts.Length > 0 ? parts[0] : ""; + string callId = OpenAIService.NormalizeToolCallId(rawCallId); + lastResolvedCallId = callId; + string name = OpenAIService.NormalizeToolCallName(parts.Length > 1 ? parts[1] : "unknown"); + string argsJson = OpenAIService.NormalizeToolCallArguments(parts.Length > 2 ? parts[2] : "{}"); + result.Add(ResponseItem.CreateFunctionCallItem( + callId, + name, + BinaryData.FromString(argsJson))); + } else if (msg.Role == "__func_output__" || content.StartsWith(FuncOutputMarker)) { + // Format: __FUNC_OUTPUT__||callId||output + var payload = content.StartsWith(FuncOutputMarker) + ? content.Substring(FuncOutputMarker.Length) + : content; + var parts = payload.Split(new[] { "||" }, 2, StringSplitOptions.None); + string rawCallId = parts.Length > 0 ? parts[0] : ""; + string callId = string.IsNullOrWhiteSpace(rawCallId) && !string.IsNullOrWhiteSpace(lastResolvedCallId) + ? lastResolvedCallId + : OpenAIService.NormalizeToolCallId(rawCallId); + lastResolvedCallId = callId; + string output = parts.Length > 1 ? parts[1] : ""; + result.Add(ResponseItem.CreateFunctionCallOutputItem(callId, output)); + } else { + switch (msg.Role?.ToLowerInvariant()) { + case "assistant": + result.Add(ResponseItem.CreateAssistantMessageItem(content)); + break; + case "user": + default: + result.Add(ResponseItem.CreateUserMessageItem(content)); + break; + } + } + } + return result; + } + + /// + /// Build a snapshot when max tool cycles is reached. + /// + private static LlmContinuationSnapshot BuildSnapshot( + long ChatId, Message message, string modelName, LLMChannel channel, + string accumulatedContent, int cyclesSoFar, List inputItems) { + return new LlmContinuationSnapshot { + SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, + ChatId = ChatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = "OpenAIResponses", + ChannelId = channel.Id, + LastAccumulatedContent = accumulatedContent, + CyclesSoFar = cyclesSoFar, + ProviderHistory = SerializeInputItems(inputItems), + }; + } + + public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { + return await GenerateEmbeddingsAsync(text, modelName, channel, null); + } + + + public async Task> GetAllModels(LLMChannel channel) { + if (channel.Provider == LLMProvider.Ollama) { + return new List(); + } + + try { + var handler = new HttpClientHandler { + Proxy = WebRequest.DefaultWebProxy, + UseProxy = true + }; + using var httpClient = new HttpClient(handler); + OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); + + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(channel.Gateway), + Transport = new HttpClientPipelineTransport(httpClient), + }; + var apiKey = new ApiKeyCredential(channel.ApiKey); + OpenAIClient client = new(apiKey, clientOptions); + var model = client.GetOpenAIModelClient(); + var models = await model.GetModelsAsync(); + return models.Value.Select(s => s.Id); + } catch (Exception ex) { + _logger.LogError(ex, "Error getting OpenAI model list (Gateway: {Gateway})", channel.Gateway); + return new List(); + } + } + + + public async Task> GetAllModelsWithCapabilities(LLMChannel channel) { + using var httpClient = _httpClientFactory.CreateClient(); OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); try { @@ -748,10 +959,86 @@ public async Task> GetAllModelsWithCapabiliti // Image Analysis // ======================================================================== + public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null) { return await AnalyzeImageAsync(photoPath, modelName, channel, null, prompt); } + + public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel, LLMApiBinding binding) { + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { + _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); + throw new InvalidOperationException($"Error: {ServiceName} channel/gateway/apikey is not configured."); + } + + using var httpClient = _httpClientFactory.CreateClient(); + OpencodeSessionHeaders.Apply(httpClient, endpoint); + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(endpoint), + Transport = new HttpClientPipelineTransport(httpClient), + }; + var credential = new ApiKeyCredential(apiKey); + OpenAIClient client = new(credential, clientOptions); + + try { + var embeddingClient = client.GetEmbeddingClient(modelName); + var response = await embeddingClient.GenerateEmbeddingsAsync(new[] { text }); + + if (response?.Value != null && response.Value.Any()) { + var embedding = response.Value.First(); + + // Try reflection to extract float array + var embeddingProp = embedding.GetType().GetProperty("Embedding") + ?? embedding.GetType().GetProperty("EmbeddingVector") + ?? embedding.GetType().GetProperty("Vector") + ?? embedding.GetType().GetProperty("EmbeddingData") + ?? embedding.GetType().GetProperty("Data"); + + if (embeddingProp != null) { + var embeddingValue = embeddingProp.GetValue(embedding); + if (embeddingValue is float[] floatArray) { + return floatArray; + } else if (embeddingValue is IEnumerable floatEnumerable) { + return floatEnumerable.ToArray(); + } else if (embeddingValue is IReadOnlyList floatList) { + return floatList.ToArray(); + } + } + + // Last resort - find any float[] property + var floatArrayProps = embedding.GetType().GetProperties() + .Where(p => p.PropertyType == typeof(float[]) || p.PropertyType == typeof(IEnumerable)) + .ToList(); + if (floatArrayProps.Any()) { + foreach (var prop in floatArrayProps) { + var value = prop.GetValue(embedding); + if (value is float[] floats) { + return floats; + } else if (value is IEnumerable floatEnumerable) { + return floatEnumerable.ToArray(); + } + } + } + + _logger.LogError("Failed to extract embedding data. Available properties: {Props}", + string.Join(", ", embedding.GetType().GetProperties().Select(p => $"{p.Name}:{p.PropertyType.Name}"))); + } + + _logger.LogError("OpenAI Embeddings API returned null or empty response"); + throw new Exception("OpenAI Embeddings API returned null or empty response"); + } catch (Exception ex) { + _logger.LogError(ex, "Error calling OpenAI Embeddings API"); + throw; + } + } + + // ======================================================================== + // Model Listing (reuses OpenAI SDK model client) + // ======================================================================== + + public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) { if (string.IsNullOrWhiteSpace(modelName)) { modelName = "gpt-4o"; @@ -888,109 +1175,19 @@ private async Task> BuildResponseInputItemsAsync( var imageBytes = TryLoadMessagePhoto(message.GroupId, message.MessageId); if (imageBytes != null) { pendingImages.Add(imageBytes); - } - } - - previous = message; - } - - if (previous != null && str.Length > 0) { - AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - } - - return inputItems; - } - - private void AddResponseItemFromAccumulated( - List inputItems, long fromUserId, string content, List images) { - if (string.IsNullOrWhiteSpace(content) && (images == null || images.Count == 0)) return; - if (!string.IsNullOrWhiteSpace(content)) { - content = System.Text.RegularExpressions.Regex.Replace(content.Trim(), @"\n{3,}", "\n\n"); - } - - if (fromUserId == Env.BotId) { - // Assistant message - if (!string.IsNullOrWhiteSpace(content)) { - inputItems.Add(ResponseItem.CreateAssistantMessageItem(content)); - } - } else { - // User message (possibly with images) - if (images != null && images.Count > 0) { - var parts = new List(); - if (!string.IsNullOrWhiteSpace(content)) { - parts.Add(ResponseContentPart.CreateInputTextPart(content.Trim())); - } - foreach (var imageBytes in images) { - parts.Add(ResponseContentPart.CreateInputImagePart( - BinaryData.FromBytes(imageBytes), null)); - } - inputItems.Add(ResponseItem.CreateUserMessageItem((IEnumerable)parts)); - } else { - inputItems.Add(ResponseItem.CreateUserMessageItem(content.Trim())); - } - } - } - - // ======================================================================== - // Helper: Vision support check - // ======================================================================== - - private async Task CheckVisionSupport(string modelName, int channelId) { - try { - var channelWithModel = await _dbContext.ChannelsWithModel - .Include(c => c.Capabilities) - .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); - - if (channelWithModel?.Capabilities != null) { - return channelWithModel.Capabilities.Any(c => - c.CapabilityName == "vision" && c.CapabilityValue == "true"); - } - return false; - } catch (Exception ex) { - _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); - return false; - } - } - - // ======================================================================== - // Helper: IsSameSender - // ======================================================================== - - public bool IsSameSender(Message message1, Message message2) { - if (message1 == null || message2 == null) return false; - bool msg1IsUser = message1.FromUserId != Env.BotId; - bool msg2IsUser = message2.FromUserId != Env.BotId; - return msg1IsUser == msg2IsUser; - } - - // ======================================================================== - // Helper: Load message photo - // ======================================================================== - - private byte[] TryLoadMessagePhoto(long chatId, long messageId) { - try { - var dirPath = Path.Combine(Env.WorkDir, "Photos", $"{chatId}"); - if (!Directory.Exists(dirPath)) return null; - - var files = Directory.GetFiles(dirPath, $"{messageId}.*"); - if (files.Length == 0) return null; + } + } - var filePath = files[0]; - using var fileStream = File.OpenRead(filePath); - var bitmap = SKBitmap.Decode(fileStream); - if (bitmap == null) return null; + previous = message; + } - var encoded = bitmap.Encode(SKEncodedImageFormat.Png, 90); - return encoded?.ToArray(); - } catch (Exception ex) { - _logger.LogDebug(ex, "无法加载消息图片: ChatId={ChatId}, MessageId={MessageId}", chatId, messageId); - return null; + if (previous != null && str.Length > 0) { + AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); } + + return inputItems; } - // ======================================================================== - // Helper: Model capability parsing (from OpenAIService) - // ======================================================================== private IEnumerable ParseOpenAIModelsWithCapabilities(string jsonContent) { try { @@ -1080,247 +1277,6 @@ private ModelWithCapabilities InferOpenAIModelCapabilities(string modelName) { return model; } - // ======================================================================== - // Streaming Update Processing Helpers - // ======================================================================== - - /// - /// Process a single streaming update for the main execution path. - /// Does NOT contain yield return, so it's safe inside try-catch. - /// - private static void ProcessStreamingUpdate( - StreamingResponseUpdate update, - StringBuilder textBuilder, - StringBuilder contentBuilder, - StringBuilder reasoningBuilder, - Dictionary toolCallAccums, - ref ResponseResult completedResult) { - - switch (update) { - case StreamingResponseOutputTextDeltaUpdate textDelta: - textBuilder.Append(textDelta.Delta); - contentBuilder.Append(textDelta.Delta); - break; - - case StreamingResponseReasoningTextDeltaUpdate reasoningDelta: - reasoningBuilder.Append(reasoningDelta.Delta); - break; - - case StreamingResponseRefusalDeltaUpdate refusalDelta: - textBuilder.Append(refusalDelta.Delta); - contentBuilder.Append(refusalDelta.Delta); - break; - - case StreamingResponseFunctionCallArgumentsDeltaUpdate funcDelta: - int idx = funcDelta.OutputIndex; - if (!toolCallAccums.ContainsKey(idx)) { - toolCallAccums[idx] = new ResponsesToolCallAccumulator(); - } - var deltaStr = funcDelta.Delta?.ToString(); - if (!string.IsNullOrEmpty(deltaStr)) { - toolCallAccums[idx].Arguments.Append(deltaStr); - } - break; - - case StreamingResponseOutputItemAddedUpdate itemAdded: - if (itemAdded.Item is FunctionCallResponseItem funcItem) { - int addIdx = itemAdded.OutputIndex; - if (!toolCallAccums.ContainsKey(addIdx)) { - toolCallAccums[addIdx] = new ResponsesToolCallAccumulator(); - } - toolCallAccums[addIdx].CallId ??= funcItem.CallId; - toolCallAccums[addIdx].Name ??= funcItem.FunctionName; - } - break; - - case StreamingResponseCompletedUpdate completed: - completedResult = completed.Response; - break; - } - } - - /// - /// Process a single streaming update for the resume execution path. - /// Does NOT contain yield return, so it's safe inside try-catch. - /// - private static void ProcessResumeStreamingUpdate( - StreamingResponseUpdate update, - StringBuilder textBuilder, - StringBuilder fullContentBuilder, - StringBuilder newContentBuilder, - Dictionary toolCallAccums, - ref ResponseResult completedResult) { - - switch (update) { - case StreamingResponseOutputTextDeltaUpdate textDelta: - textBuilder.Append(textDelta.Delta); - fullContentBuilder.Append(textDelta.Delta); - newContentBuilder.Append(textDelta.Delta); - break; - - case StreamingResponseReasoningTextDeltaUpdate: - // Reasoning content was already shown in the original stream; - // no need to append to newContentBuilder during resume. - break; - - case StreamingResponseRefusalDeltaUpdate refusalDelta: - textBuilder.Append(refusalDelta.Delta); - fullContentBuilder.Append(refusalDelta.Delta); - newContentBuilder.Append(refusalDelta.Delta); - break; - - case StreamingResponseFunctionCallArgumentsDeltaUpdate funcDelta: - int idx = funcDelta.OutputIndex; - if (!toolCallAccums.ContainsKey(idx)) { - toolCallAccums[idx] = new ResponsesToolCallAccumulator(); - } - var deltaStr = funcDelta.Delta?.ToString(); - if (!string.IsNullOrEmpty(deltaStr)) { - toolCallAccums[idx].Arguments.Append(deltaStr); - } - break; - - case StreamingResponseOutputItemAddedUpdate itemAdded: - if (itemAdded.Item is FunctionCallResponseItem funcItem) { - int addIdx = itemAdded.OutputIndex; - if (!toolCallAccums.ContainsKey(addIdx)) { - toolCallAccums[addIdx] = new ResponsesToolCallAccumulator(); - } - toolCallAccums[addIdx].CallId ??= funcItem.CallId; - toolCallAccums[addIdx].Name ??= funcItem.FunctionName; - } - break; - - case StreamingResponseCompletedUpdate completed: - completedResult = completed.Response; - break; - } - } - - // ======================================================================== - // Snapshot Serialization - // ======================================================================== - - private const string FuncCallMarker = "__FUNC_CALL__||"; - private const string FuncOutputMarker = "__FUNC_OUTPUT__||"; - - /// - /// Serialize ResponseItem list to portable format for snapshot persistence. - /// Preserves function call structure via marker prefixes. - /// - private static List SerializeInputItems(List inputItems) { - var result = new List(); - foreach (var item in inputItems) { - string role; - string content = ""; - - if (item is MessageResponseItem msgItem) { - switch (msgItem.Role) { - case MessageRole.User: - role = "user"; - break; - case MessageRole.Assistant: - role = "assistant"; - break; - default: - role = "user"; - break; - } - content = string.Join("", msgItem.Content?.Select(p => p.Text) ?? Enumerable.Empty()); - } else if (item is FunctionCallResponseItem funcCallItem) { - role = "__func_call__"; - // Format: __FUNC_CALL__||callId||name||argsJson - content = $"{FuncCallMarker}{funcCallItem.CallId ?? ""}||{funcCallItem.FunctionName ?? ""}||{funcCallItem.FunctionArguments?.ToString() ?? "{}"}"; - } else if (item is FunctionCallOutputResponseItem funcOutputItem) { - role = "__func_output__"; - // Format: __FUNC_OUTPUT__||callId||output - content = $"{FuncOutputMarker}{funcOutputItem.CallId ?? ""}||{funcOutputItem.FunctionOutput ?? ""}"; - } else { - role = "user"; - content = item.ToString(); - } - - result.Add(new SerializedChatMessage { Role = role, Content = content }); - } - return result; - } - - /// - /// Deserialize portable format back to ResponseItem list. - /// Reconstructs function call items from marker-prefixed content. - /// - private static List DeserializeResponseItemsFromSnapshot(List serialized) { - var result = new List(); - if (serialized == null) return result; - - var lastResolvedCallId = string.Empty; - foreach (var msg in serialized) { - string content = msg.Content ?? ""; - - // Check for function call markers first - if (msg.Role == "__func_call__" || content.StartsWith(FuncCallMarker)) { - // Format: __FUNC_CALL__||callId||name||argsJson - var payload = content.StartsWith(FuncCallMarker) - ? content.Substring(FuncCallMarker.Length) - : content; - var parts = payload.Split(new[] { "||" }, 3, StringSplitOptions.None); - string rawCallId = parts.Length > 0 ? parts[0] : ""; - string callId = OpenAIService.NormalizeToolCallId(rawCallId); - lastResolvedCallId = callId; - string name = OpenAIService.NormalizeToolCallName(parts.Length > 1 ? parts[1] : "unknown"); - string argsJson = OpenAIService.NormalizeToolCallArguments(parts.Length > 2 ? parts[2] : "{}"); - result.Add(ResponseItem.CreateFunctionCallItem( - callId, - name, - BinaryData.FromString(argsJson))); - } else if (msg.Role == "__func_output__" || content.StartsWith(FuncOutputMarker)) { - // Format: __FUNC_OUTPUT__||callId||output - var payload = content.StartsWith(FuncOutputMarker) - ? content.Substring(FuncOutputMarker.Length) - : content; - var parts = payload.Split(new[] { "||" }, 2, StringSplitOptions.None); - string rawCallId = parts.Length > 0 ? parts[0] : ""; - string callId = string.IsNullOrWhiteSpace(rawCallId) && !string.IsNullOrWhiteSpace(lastResolvedCallId) - ? lastResolvedCallId - : OpenAIService.NormalizeToolCallId(rawCallId); - lastResolvedCallId = callId; - string output = parts.Length > 1 ? parts[1] : ""; - result.Add(ResponseItem.CreateFunctionCallOutputItem(callId, output)); - } else { - switch (msg.Role?.ToLowerInvariant()) { - case "assistant": - result.Add(ResponseItem.CreateAssistantMessageItem(content)); - break; - case "user": - default: - result.Add(ResponseItem.CreateUserMessageItem(content)); - break; - } - } - } - return result; - } - - /// - /// Build a snapshot when max tool cycles is reached. - /// - private static LlmContinuationSnapshot BuildSnapshot( - long ChatId, Message message, string modelName, LLMChannel channel, - string accumulatedContent, int cyclesSoFar, List inputItems) { - return new LlmContinuationSnapshot { - SchemaVersion = LlmContinuationSnapshot.CurrentSchemaVersion, - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "OpenAIResponses", - ChannelId = channel.Id, - LastAccumulatedContent = accumulatedContent, - CyclesSoFar = cyclesSoFar, - ProviderHistory = SerializeInputItems(inputItems), - }; - } - Task ILLMService.AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt) { return AnalyzeImageAsync(photoPath, modelName, channel, prompt); } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs index 04e914fa..98ae7ad4 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs @@ -190,6 +190,30 @@ private static List GetStablePrefixMessages(List provi return providerHistory.Take(providerHistory.Count - 1).ToList(); } + /// Compact, deterministic history serialization for prompt-cache key hashing. + private static List SerializeProviderHistory(List history) { + var result = new List(); + foreach (var msg in history) { + string role; + string content; + if (msg is SystemChatMessage systemMsg) { + role = "system"; + content = string.Join("", systemMsg.Content?.Select(p => p.Text) ?? Enumerable.Empty()); + } else if (msg is AssistantChatMessage assistantMsg) { + role = "assistant"; + content = string.Join("", assistantMsg.Content?.Select(p => p.Text) ?? Enumerable.Empty()); + } else if (msg is UserChatMessage userMsg) { + role = "user"; + content = string.Join("", userMsg.Content?.Select(p => p.Text) ?? Enumerable.Empty()); + } else { + role = "user"; + content = msg.ToString() ?? string.Empty; + } + result.Add(new SerializedChatMessage { Role = role, Content = content }); + } + return result; + } + private static (string toolDefinitionHash, string stablePrefixHash, string promptCacheKey) BuildPromptCachingContext( string providerName, string modelName, @@ -1076,6 +1100,24 @@ private byte[] TryLoadMessagePhoto(long chatId, long messageId) { // ConvertToolResultToString is now in McpToolHelper // --- Main Execution Logic --- + private async Task CheckVisionSupport(string modelName, int channelId) { + try { + var channelWithModel = await _dbContext.ChannelsWithModel + .Include(c => c.Capabilities) + .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); + + if (channelWithModel?.Capabilities != null) { + return channelWithModel.Capabilities.Any(c => + c.CapabilityName == "vision" && c.CapabilityValue == "true"); + } + + return false; + } catch (Exception ex) { + _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); + return false; + } + } + public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { var executionContext = new LlmExecutionContext(); @@ -1111,28 +1153,70 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long yield break; } + var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); + var supportsVision = await CheckVisionSupport(modelName, channel.Id); + // Try native tool calling first; fall back to XML prompt-based if it fails - bool useNativeToolCalling = true; var nativeTools = McpToolHelper.GetNativeToolDefinitions(); + var useNativeToolCalling = nativeTools is { Count: > 0 }; - if (nativeTools == null || !nativeTools.Any()) { - useNativeToolCalling = false; + if (useNativeToolCalling) { + bool nativeFailed = false; + var nativeEnumerator = ExecWithNativeToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); + await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); + bool hasFirst = false; + try { + hasFirst = await enumerator.MoveNextAsync(); + } catch (Exception ex) when (IsToolCallingNotSupportedError(ex)) { + _logger.LogInformation("{ServiceName}: Native tool calling not supported for model {Model}, falling back to XML prompt-based tool calling. Error: {Error}", ServiceName, modelName, ex.Message); + nativeFailed = true; + } + + if (!nativeFailed) { + if (hasFirst) { + yield return enumerator.Current; + while (await enumerator.MoveNextAsync()) { + yield return enumerator.Current; + } + } + yield break; + } } - var isMiniMaxCompatibleEndpoint = binding == null && IsMiniMaxCompatibleEndpoint(channel, modelName); - _logger.LogInformation( - "{ServiceName}: Tool calling setup for model {Model}. UseNative={UseNative}, NativeToolCount={NativeToolCount}, Provider={Provider}, Gateway={Gateway}, IsMiniMaxCompatible={IsMiniMaxCompatible}", - ServiceName, - modelName, - useNativeToolCalling, - nativeTools?.Count ?? 0, - channel.Provider, - channel.Gateway, - isMiniMaxCompatibleEndpoint); + // Fallback: XML prompt-based tool calling + await foreach (var item in ExecWithXmlToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { + yield return item; + } + } + + /// + public async IAsyncEnumerable ExecWithHistoryAsync( + IReadOnlyList history, + Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, + LLMApiBinding binding, LlmExecutionContext executionContext, + bool supportsVision, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; + + if (string.IsNullOrWhiteSpace(modelName)) { + _logger.LogError("{ServiceName}: Model name is not configured.", ServiceName); + yield return $"Error: {ServiceName} model name is not configured."; + yield break; + } + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { + _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); + yield return $"Error: {ServiceName} channel/gateway/apikey is not configured."; + yield break; + } + + var nativeTools = McpToolHelper.GetNativeToolDefinitions(); + var useNativeToolCalling = nativeTools is { Count: > 0 }; if (useNativeToolCalling) { bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); + var nativeEnumerator = ExecWithNativeToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); bool hasFirst = false; try { @@ -1153,8 +1237,7 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long } } - // Fallback: XML prompt-based tool calling - await foreach (var item in ExecWithXmlToolCallingAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { + await foreach (var item in ExecWithXmlToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { yield return item; } } @@ -1176,31 +1259,9 @@ private static bool IsToolCallingNotSupportedError(Exception ex) { message.Contains("unrecognized request argument", StringComparison.OrdinalIgnoreCase); } - /// - /// 检查模型是否支持视觉能力 - /// - private async Task CheckVisionSupport(string modelName, int channelId) { - try { - var channelWithModel = await _dbContext.ChannelsWithModel - .Include(c => c.Capabilities) - .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); - - if (channelWithModel?.Capabilities != null) { - return channelWithModel.Capabilities.Any(c => - c.CapabilityName == "vision" && c.CapabilityValue == "true"); - } - - return false; - } catch (Exception ex) { - _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); - return false; - } - } - - /// - /// Execute LLM with native OpenAI tool calling API. - /// private async IAsyncEnumerable ExecWithNativeToolCallingAsync( + IReadOnlyList rows, + bool supportsVision, Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, LLMApiBinding binding, LlmExecutionContext executionContext, @@ -1208,51 +1269,16 @@ private async IAsyncEnumerable ExecWithNativeToolCallingAsync( [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - // --- History and Prompt Setup (simplified - no XML tool instructions) --- var botName = await GetBotNameAsync(); string systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId); - List providerHistory = new List() { new SystemChatMessage(systemPrompt) }; - bool supportsVision = await CheckVisionSupport(modelName, channel.Id); - providerHistory = await GetChatHistory(ChatId, providerHistory, message, supportsVision); - var shouldObservePromptCaching = channel.Provider == LLMProvider.OpenAI; - var promptCachingEnabled = shouldObservePromptCaching && await IsPromptCachingEnabledAsync(); - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = BuildPromptCachingContext( - "OpenAI", - modelName, - "chat-native", - providerHistory, - excludeDynamicTail: true); - - using var client = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(client, channel, binding, $"tsb-{ChatId}"); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding))), - Transport = new HttpClientPipelineTransport(client), - }; - var chatClient = new ChatClient(model: modelName, credential: new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); - - var completionOptions = new ChatCompletionOptions(); - foreach (var tool in nativeTools) { - completionOptions.Tools.Add(tool); - } - - var cacheKeyAttached = false; - if (promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(completionOptions, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); - cacheKeyAttached = true; - } - _logger.LogInformation( - "{ServiceName}: Starting native tool call cycle. Model={Model}, ToolCount={ToolCount}, ToolNames={ToolNames}", - ServiceName, - modelName, - nativeTools.Count, - string.Join(",", nativeTools.Select(t => t.FunctionName).Take(80))); + var promptCachingEnabled = channel.Provider == LLMProvider.OpenAI && await IsPromptCachingEnabledAsync(); var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); - var source = new OpenAiChatTurnSource( - this, chatClient, completionOptions, providerHistory, - shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, cacheKeyAttached, - includeEmptyReasoningContent, nativeTools: true, modelName, channel); + var clientParts = BuildClientParts(channel, binding, ChatId); + var transport = new OpenAiChatTransport(this, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, + nativeTools: true, promptCachingEnabled, includeEmptyReasoningContent); + var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); + var meta = new LlmToolLoopMeta { ChatId = ChatId, OriginalMessageId = message.MessageId, @@ -1262,54 +1288,40 @@ private async IAsyncEnumerable ExecWithNativeToolCallingAsync( ChannelId = channel.Id }; var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = history, + Tools = McpToolHelper.GetLlmToolSpecs(), + Config = BuildConfig(modelName, channel, binding, supportsVision, promptCachingEnabled, includeEmptyReasoningContent), + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } - /// - /// Execute LLM with XML prompt-based tool calling (fallback for models that don't support native tool calling). - /// private async IAsyncEnumerable ExecWithXmlToolCallingAsync( + IReadOnlyList rows, + bool supportsVision, Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, LLMApiBinding binding, LlmExecutionContext executionContext, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - // --- History and Prompt Setup --- var botName = await GetBotNameAsync(); string systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - List providerHistory = new List() { new SystemChatMessage(systemPrompt) }; - bool supportsVision = await CheckVisionSupport(modelName, channel.Id); - providerHistory = await GetChatHistory(ChatId, providerHistory, message, supportsVision); - var shouldObservePromptCaching = channel.Provider == LLMProvider.OpenAI; - var promptCachingEnabled = shouldObservePromptCaching && await IsPromptCachingEnabledAsync(); - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = BuildPromptCachingContext( - "OpenAI", - modelName, - "chat-xml", - providerHistory, - excludeDynamicTail: true); - - using var client = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(client, channel, binding, $"tsb-{ChatId}"); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding))), - Transport = new HttpClientPipelineTransport(client), - }; - var chatClient = new ChatClient(model: modelName, credential: new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); - var completionOptions = new ChatCompletionOptions(); - var cacheKeyAttached = false; - if (promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(completionOptions, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); - cacheKeyAttached = true; - } + var promptCachingEnabled = channel.Provider == LLMProvider.OpenAI && await IsPromptCachingEnabledAsync(); + var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); + + var clientParts = BuildClientParts(channel, binding, ChatId); + var transport = new OpenAiChatTransport(this, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, + nativeTools: false, promptCachingEnabled, includeEmptyReasoningContent); + var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); - var source = new OpenAiChatTurnSource( - this, chatClient, completionOptions, providerHistory, - shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, cacheKeyAttached, - includeEmptyReasoningContent: false, nativeTools: false, modelName, channel); var meta = new LlmToolLoopMeta { ChatId = ChatId, OriginalMessageId = message.MessageId, @@ -1319,62 +1331,186 @@ private async IAsyncEnumerable ExecWithXmlToolCallingAsync( ChannelId = channel.Id }; var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = history, + Tools = null, + Config = BuildConfig(modelName, channel, binding, supportsVision, promptCachingEnabled, includeEmptyReasoningContent), + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + yield return item; + } + } + + private LlmTransportConfig BuildConfig(string modelName, LLMChannel channel, LLMApiBinding binding, + bool supportsVision, bool promptCachingEnabled, bool includeEmptyReasoningContent) { + return new LlmTransportConfig { + ModelName = modelName, + Endpoint = NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)), + ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision, + PromptCachingEnabled = promptCachingEnabled, + IncludeEmptyReasoningContent = includeEmptyReasoningContent + }; + } + + private (HttpClientPipelineTransport Transport, ApiKeyCredential Credential, OpenAIClientOptions Options) BuildClientParts(LLMChannel channel, LLMApiBinding binding, long chatId) { + // ponytail: HttpClient must outlive this method (transport persists per run); factory-managed, not disposed here. + var httpClient = _httpClientFactory.CreateClient(); + OpencodeSessionHeaders.Apply(httpClient, channel, binding, $"tsb-{chatId}"); + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding))), + Transport = new HttpClientPipelineTransport(httpClient), + }; + return (new HttpClientPipelineTransport(httpClient), new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); + } + + /// + /// Resume LLM execution from a saved v2 (normalized) snapshot. + /// + public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, + LlmExecutionContext executionContext, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + await foreach (var item in ResumeFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken)) { + yield return item; + } + } + + public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, + LLMApiBinding binding, + LlmExecutionContext executionContext, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); + if (snapshot == null) { + _logger.LogError("{ServiceName}: Cannot resume from null snapshot.", ServiceName); + yield break; + } + if (snapshot.NormalizedHistory is not { Count: > 0 } savedHistory) { + _logger.LogError("{ServiceName}: Snapshot {SnapshotId} has no v2 normalized history (legacy v1 snapshots expire via TTL).", ServiceName, snapshot.SnapshotId); + yield break; + } + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { + _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured for resume.", ServiceName); + yield break; + } + + var modelName = snapshot.ModelName; + if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; + + _logger.LogInformation("{ServiceName}: Resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", + ServiceName, snapshot.SnapshotId, snapshot.ChatId, savedHistory.Count); + + // Peel the stored system prompt off the normalized history. + string systemPrompt; + if (savedHistory[0].Role == LlmRole.System) { + systemPrompt = savedHistory[0].Text ?? string.Empty; + savedHistory = savedHistory.Skip(1).ToList(); + } else { + var botName = await GetBotNameAsync(); + systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, snapshot.ChatId); + } + + var promptCachingEnabled = channel.Provider == LLMProvider.OpenAI && await IsPromptCachingEnabledAsync(); + var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); + var clientParts = BuildClientParts(channel, binding, snapshot.ChatId); + var transport = new OpenAiChatTransport(this, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, + nativeTools: false, promptCachingEnabled, includeEmptyReasoningContent); + + var meta = new LlmToolLoopMeta { + ChatId = snapshot.ChatId, + OriginalMessageId = snapshot.OriginalMessageId, + UserId = snapshot.UserId, + ModelName = modelName, + Provider = "OpenAI", + ChannelId = channel.Id, + BaseCycles = snapshot.CyclesSoFar, + InitialContent = snapshot.LastAccumulatedContent ?? string.Empty + }; + var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; + var run = new LlmAgentRunRequest { + Transport = transport, + SystemPrompt = systemPrompt, + History = savedHistory, + Tools = null, + Config = BuildConfig(modelName, channel, binding, false, promptCachingEnabled, includeEmptyReasoningContent), + ToolContext = toolContext, + Meta = meta, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { yield return item; } } /// - /// Per-run turn source for the OpenAI Chat Completions API. Owns the provider history, - /// streams one turn per call, and supports both native tool-calling and the XML text - /// protocol. Snapshot history is produced by . + /// Stateless transport adapter for the OpenAI Chat Completions API: converts the + /// normalized history into SDK messages per turn and streams one assistant turn. /// - private sealed class OpenAiChatTurnSource : ILlmTurnSource { + private sealed class OpenAiChatTransport : ILlmTransport { private readonly OpenAIService _svc; - private readonly ChatClient _chatClient; - private readonly ChatCompletionOptions _completionOptions; - private readonly List _providerHistory; - private readonly bool _shouldObservePromptCaching; - private readonly bool _promptCachingEnabled; - private readonly string _toolDefinitionHash; - private readonly string _stablePrefixHash; - private readonly string _promptCacheKey; - private readonly bool _cacheKeyAttached; - private readonly bool _includeEmptyReasoningContent; - private readonly bool _nativeTools; + private readonly HttpClientPipelineTransport _pipelineTransport; + private readonly ApiKeyCredential _credential; + private readonly OpenAIClientOptions _clientOptions; private readonly string _modelName; private readonly LLMChannel _channel; + private readonly bool _nativeTools; + private readonly bool _promptCachingEnabled; + private readonly bool _includeEmptyReasoningContent; - private AssistantChatMessage _pendingAssistantMessage; - private bool _pendingMalformed; - - public OpenAiChatTurnSource( - OpenAIService svc, ChatClient chatClient, ChatCompletionOptions completionOptions, - List providerHistory, - bool shouldObservePromptCaching, bool promptCachingEnabled, - string toolDefinitionHash, string stablePrefixHash, string promptCacheKey, bool cacheKeyAttached, - bool includeEmptyReasoningContent, bool nativeTools, string modelName, LLMChannel channel) { + public OpenAiChatTransport(OpenAIService svc, HttpClientPipelineTransport pipelineTransport, + ApiKeyCredential credential, OpenAIClientOptions clientOptions, string modelName, LLMChannel channel, + bool nativeTools, bool promptCachingEnabled, bool includeEmptyReasoningContent) { _svc = svc; - _chatClient = chatClient; - _completionOptions = completionOptions; - _providerHistory = providerHistory; - _shouldObservePromptCaching = shouldObservePromptCaching; - _promptCachingEnabled = promptCachingEnabled; - _toolDefinitionHash = toolDefinitionHash; - _stablePrefixHash = stablePrefixHash; - _promptCacheKey = promptCacheKey; - _cacheKeyAttached = cacheKeyAttached; - _includeEmptyReasoningContent = includeEmptyReasoningContent; - _nativeTools = nativeTools; + _pipelineTransport = pipelineTransport; + _credential = credential; + _clientOptions = clientOptions; _modelName = modelName; _channel = channel; + _nativeTools = nativeTools; + _promptCachingEnabled = promptCachingEnabled; + _includeEmptyReasoningContent = includeEmptyReasoningContent; } public bool SupportsNativeTools => _nativeTools; public async IAsyncEnumerable StreamTurnAsync( - string? userContent, + LlmTurnRequest request, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + var chatClient = new ChatClient(model: request.Config.ModelName, credential: _credential, _clientOptions); + var providerHistory = ToProviderHistory(request); + var completionOptions = new ChatCompletionOptions(); + if (_nativeTools && request.Tools is { Count: > 0 }) { + foreach (var spec in request.Tools) { + completionOptions.Tools.Add(ChatTool.CreateFunctionTool( + spec.Name, + spec.Description, + BinaryData.FromString(spec.ParametersJson))); + } + } + + var shouldObservePromptCaching = _channel.Provider == LLMProvider.OpenAI; + var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; + var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIService.BuildPromptCachingContext( + "OpenAI", + _modelName, + _nativeTools ? "chat-native" : "chat-xml", + providerHistory, + excludeDynamicTail: true); + var cacheKeyAttached = false; + if (promptCachingEnabled) { + PromptCachingHelper.ApplyOpenAiPromptCaching(completionOptions, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); + cacheKeyAttached = true; + } + var contentBuilder = new StringBuilder(); var reasoningContentBuilder = new StringBuilder(); var toolCallAccumulators = new Dictionary(); @@ -1382,7 +1518,7 @@ public async IAsyncEnumerable StreamTurnAsync( ChatTokenUsage latestUsage = null; var streamedAny = false; - await foreach (var update in _chatClient.CompleteChatStreamingAsync(_providerHistory, _completionOptions, cancellationToken).WithCancellation(cancellationToken)) { + await foreach (var update in chatClient.CompleteChatStreamingAsync(providerHistory, completionOptions, cancellationToken).WithCancellation(cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { @@ -1423,58 +1559,28 @@ public async IAsyncEnumerable StreamTurnAsync( } } - if (_shouldObservePromptCaching) { + if (shouldObservePromptCaching) { _svc.LogOpenAiPromptCachingObservation( - "OpenAI", - _channel, - _modelName, - _promptCachingEnabled, - _toolDefinitionHash, - _stablePrefixHash, - _promptCacheKey, - latestUsage, - _cacheKeyAttached); + "OpenAI", _channel, _modelName, promptCachingEnabled, + toolDefinitionHash, stablePrefixHash, promptCacheKey, + latestUsage, cacheKeyAttached); } var responseText = contentBuilder.ToString().Trim(); var reasoningContent = reasoningContentBuilder.ToString().Trim(); - _pendingAssistantMessage = null; - _pendingMalformed = false; + var toolCalls = new List(); + var malformed = false; - var toolCalls = new List(); if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCallAccumulators.Any()) { - try { - var chatToolCalls = new List(); - foreach (var (index, acc) in toolCallAccumulators) { - var toolCallId = OpenAIService.NormalizeToolCallId(acc.Id); - var toolName = OpenAIService.NormalizeToolCallName(acc.Name); - var argumentsJson = OpenAIService.NormalizeToolCallArguments(acc.Arguments.ToString()); - chatToolCalls.Add(ChatToolCall.CreateFunctionToolCall( - toolCallId, - toolName, - BinaryData.FromString(argumentsJson))); + foreach (var (index, acc) in toolCallAccumulators) { + if (string.IsNullOrWhiteSpace(acc.Id)) { + _svc._logger.LogWarning("{ServiceName}: Tool call at index {Index} has no ID, generating fallback.", _svc.ServiceName, index); } - - var assistantMessage = new AssistantChatMessage(chatToolCalls); - if (!string.IsNullOrWhiteSpace(responseText)) { - assistantMessage = new AssistantChatMessage(chatToolCalls) { Content = { ChatMessageContentPart.CreateTextPart(responseText) } }; - } - OpenAIService.SetAssistantReasoningContent(assistantMessage, reasoningContent, _includeEmptyReasoningContent); - _pendingAssistantMessage = assistantMessage; - - toolCalls = chatToolCalls.Select(tc => new LlmNativeToolCall { - Id = tc.Id, - Name = tc.FunctionName, - ArgumentsJson = tc.FunctionArguments?.ToString() ?? "{}" - }).ToList(); - } catch (Exception ex) { - _svc._logger.LogError( - ex, - "{ServiceName}: Error building native tool calls, returning error to LLM for self-correction. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}, ErrorSummary={ErrorSummary}", - _svc.ServiceName, finishReason, toolCallAccumulators.Count, ex.GetLogSummary()); - const string errorMsg = "Tool call failed before execution due to malformed tool metadata. Please verify the tool name and parameters, then try again."; - _providerHistory.Add(new UserChatMessage(errorMsg)); - _pendingMalformed = true; + toolCalls.Add(new LlmToolCall { + Id = NormalizeToolCallId(acc.Id), + Name = NormalizeToolCallName(acc.Name), + ArgumentsJson = NormalizeToolCallArguments(acc.Arguments.ToString()) + }); } } else if (toolCallAccumulators.Any()) { _svc._logger.LogWarning( @@ -1482,156 +1588,61 @@ public async IAsyncEnumerable StreamTurnAsync( _svc.ServiceName, finishReason, toolCallAccumulators.Count); } + // Reasoning metadata sanity check (legacy behavior preserved): tool-call turns + // whose metadata could not be parsed at all are flagged for self-correction. + if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCalls.Count == 0) { + malformed = true; + } + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { Text = responseText, Reasoning = reasoningContent, ToolCalls = toolCalls, - MalformedToolCall = _pendingMalformed, + MalformedToolCall = malformed, StreamedAny = streamedAny }); } - public void CommitAssistantTurn(string text, string reasoning, bool streamedAny) { - if (SupportsNativeTools) { - if (!string.IsNullOrWhiteSpace(text)) { - var assistantMsg = new AssistantChatMessage(text); - OpenAIService.SetAssistantReasoningContent(assistantMsg, reasoning, _includeEmptyReasoningContent); - _providerHistory.Add(assistantMsg); - } else { - _svc._logger.LogWarning( - "{ServiceName}: Native cycle produced empty final text and no native tool calls.", - _svc.ServiceName); + private List ToProviderHistory(LlmTurnRequest request) { + var messages = new List { new SystemChatMessage(request.SystemPrompt) }; + foreach (var m in request.History) { + switch (m.Role) { + case LlmRole.User: { + var parts = new List(); + if (!string.IsNullOrEmpty(m.Text)) { + parts.Add(ChatMessageContentPart.CreateTextPart(m.Text)); + } + if (m.ImagePng != null && request.Config.SupportsVision) { + parts.Add(ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(m.ImagePng), m.ImageMediaType ?? "image/png")); + } + messages.Add(parts.Count > 0 ? new UserChatMessage(parts) : new UserChatMessage(m.Text ?? string.Empty)); + break; + } + case LlmRole.Assistant: { + if (m.ToolCalls is { Count: > 0 }) { + var calls = m.ToolCalls.Select(tc => ChatToolCall.CreateFunctionToolCall( + tc.Id, tc.Name, BinaryData.FromString(tc.ArgumentsJson))).ToList(); + var assistant = new AssistantChatMessage(calls); + if (!string.IsNullOrWhiteSpace(m.Text)) { + assistant = new AssistantChatMessage(calls) { Content = { ChatMessageContentPart.CreateTextPart(m.Text) } }; + } + SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); + messages.Add(assistant); + } else { + var assistant = new AssistantChatMessage(m.Text ?? string.Empty); + SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); + messages.Add(assistant); + } + break; + } + case LlmRole.Tool: + messages.Add(new ToolChatMessage(m.ToolCallId ?? string.Empty, m.Text ?? string.Empty)); + break; } - return; - } - if (!string.IsNullOrWhiteSpace(text)) { - _providerHistory.Add(new AssistantChatMessage(text)); - } - } - - public void CommitToolResults(IReadOnlyList results) { - if (_pendingAssistantMessage != null) { - _providerHistory.Add(_pendingAssistantMessage); - } - foreach (var result in results) { - _providerHistory.Add(new ToolChatMessage(result.ToolCallId, result.Result)); } - } - - public IReadOnlyList GetTrackedHistory() { - return OpenAIService.SerializeProviderHistory(_providerHistory); + return messages; } } - - /// - /// Resume LLM execution from a saved snapshot, restoring the full provider history. - /// - public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ResumeFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - if (snapshot == null) { - _logger.LogError("{ServiceName}: Cannot resume from null snapshot.", ServiceName); - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured for resume.", ServiceName); - yield break; - } - - var modelName = snapshot.ModelName; - if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; - - _logger.LogInformation("{ServiceName}: Resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", - ServiceName, snapshot.SnapshotId, snapshot.ChatId, snapshot.ProviderHistory?.Count ?? 0); - - var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); - List providerHistory = DeserializeProviderHistory(snapshot.ProviderHistory, includeEmptyReasoningContent); - var shouldObservePromptCaching = channel.Provider == LLMProvider.OpenAI; - var promptCachingEnabled = shouldObservePromptCaching && await IsPromptCachingEnabledAsync(); - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = BuildPromptCachingContext( - "OpenAI", - modelName, - "chat-resume", - providerHistory, - excludeDynamicTail: false); - - using var client = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(client, channel, binding, $"tsb-{snapshot.ChatId}"); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding))), - Transport = new HttpClientPipelineTransport(client), - }; - var chatClient = new ChatClient(model: modelName, credential: new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); - var completionOptions = new ChatCompletionOptions(); - var cacheKeyAttached = false; - if (promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(completionOptions, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); - cacheKeyAttached = true; - } - - var source = new OpenAiChatTurnSource( - this, chatClient, completionOptions, providerHistory, - shouldObservePromptCaching, promptCachingEnabled, toolDefinitionHash, stablePrefixHash, promptCacheKey, cacheKeyAttached, - includeEmptyReasoningContent, nativeTools: false, modelName, channel); - var meta = new LlmToolLoopMeta { - ChatId = snapshot.ChatId, - OriginalMessageId = snapshot.OriginalMessageId, - UserId = snapshot.UserId, - ModelName = modelName, - Provider = "OpenAI", - ChannelId = channel.Id, - BaseCycles = snapshot.CyclesSoFar, - InitialContent = snapshot.LastAccumulatedContent ?? string.Empty - }; - var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - await foreach (var item in LlmToolLoop.RunAsync(source, null, toolContext, meta, executionContext, cancellationToken)) { - yield return item; - } - } - /// - /// Serialize OpenAI ChatMessage list to portable format for snapshot persistence. - /// - public static List SerializeProviderHistory(List history) { - var result = new List(); - foreach (var msg in history) { - string role; - string content = ""; - string? reasoningContent = null; - - if (msg is SystemChatMessage systemMsg) { - role = "system"; - content = string.Join("", systemMsg.Content?.Select(p => p.Text) ?? Enumerable.Empty()); - } else if (msg is AssistantChatMessage assistantMsg) { - role = "assistant"; - content = string.Join("", assistantMsg.Content?.Select(p => p.Text) ?? Enumerable.Empty()); - // Try to get reasoning content from the assistant message - // OpenAI SDK stores reasoning content in a separate property - reasoningContent = GetAssistantReasoningContent(assistantMsg); - } else if (msg is UserChatMessage userMsg) { - role = "user"; - content = string.Join("", userMsg.Content?.Select(p => p.Text) ?? Enumerable.Empty()); - } else { - role = "user"; - content = msg.ToString(); - } - - result.Add(new SerializedChatMessage { Role = role, Content = content, ReasoningContent = reasoningContent }); - } - return result; - } - /// /// Extract reasoning_content from AssistantChatMessage if available. /// For thinking mode models, the reasoning process is returned separately. diff --git a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs index eeeb21ee..5acf3fd8 100644 --- a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs +++ b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs @@ -22,7 +22,7 @@ public async IAsyncEnumerable CallAsync( AgentExecutionTask task, LlmExecutionContext executionContext, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken) { - await SeedTaskDataAsync(task, cancellationToken); + List history = task.History; var binding = ToBinding(task.Channel); var service = binding != null ? ResolveService(binding.Protocol) : ResolveService(task.Channel.Provider); @@ -48,7 +48,11 @@ public async IAsyncEnumerable CallAsync( DateTime = task.CreatedAtUtc }; - await foreach (var chunk in service.ExecAsync(message, task.ChatId, task.ModelName, channel, binding, executionContext, cancellationToken) + var supportsVision = task.Channel.Capabilities.Any(c => + c.Name.Equals("vision", StringComparison.OrdinalIgnoreCase) && + c.Value.Equals("true", StringComparison.OrdinalIgnoreCase)); + + await foreach (var chunk in service.ExecWithHistoryAsync(history, message, task.ChatId, task.ModelName, channel, binding, executionContext, supportsVision, cancellationToken) .WithCancellation(cancellationToken)) { yield return chunk; } @@ -107,69 +111,6 @@ private void ApplyBotIdentity(string botName, long botUserId) { } } - private async Task SeedTaskDataAsync(AgentExecutionTask task, CancellationToken cancellationToken) { - var dbContext = _serviceProvider.GetRequiredService(); - await dbContext.Database.EnsureDeletedAsync(cancellationToken); - await dbContext.Database.EnsureCreatedAsync(cancellationToken); - - dbContext.LLMChannels.Add(ToEntity(task.Channel)); - var channelWithModel = new ChannelWithModel { - Id = 1, - LLMChannelId = task.Channel.ChannelId, - ModelName = task.ModelName, - IsDeleted = false - }; - dbContext.ChannelsWithModel.Add(channelWithModel); - dbContext.GroupSettings.Add(new GroupSettings { - GroupId = task.ChatId, - LLMModelName = task.ModelName - }); - - foreach (var capability in task.Channel.Capabilities) { - dbContext.ModelCapabilities.Add(new ModelCapability { - ChannelWithModelId = channelWithModel.Id, - CapabilityName = capability.Name, - CapabilityValue = capability.Value, - Description = capability.Description - }); - } - - var seededUsers = new HashSet(); - foreach (var historyMessage in task.History) { - dbContext.Messages.Add(new Message { - Id = historyMessage.DataId, - DateTime = historyMessage.DateTime, - GroupId = historyMessage.GroupId, - MessageId = historyMessage.MessageId, - FromUserId = historyMessage.FromUserId, - ReplyToUserId = historyMessage.ReplyToUserId, - ReplyToMessageId = historyMessage.ReplyToMessageId, - Content = historyMessage.Content - }); - - if (seededUsers.Add(historyMessage.User.UserId)) { - dbContext.UserData.Add(new UserData { - Id = historyMessage.User.UserId, - FirstName = historyMessage.User.FirstName, - LastName = historyMessage.User.LastName, - UserName = historyMessage.User.UserName, - IsBot = historyMessage.User.IsBot, - IsPremium = historyMessage.User.IsPremium - }); - } - - foreach (var extension in historyMessage.Extensions) { - dbContext.MessageExtensions.Add(new MessageExtension { - MessageDataId = historyMessage.DataId, - Name = extension.Name, - Value = extension.Value - }); - } - } - - await dbContext.SaveChangesAsync(cancellationToken); - } - private static LLMChannel ToEntity(AgentChannelConfig config) { return new LLMChannel { Id = config.ChannelId, From a9e254782da7af69257f16c927432550db4799da Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 09:09:06 +0800 Subject: [PATCH 12/30] refactor(llm): extract OpenAiChatTransport to Transports/ (step 1 of service-layer dissolution) --- .../Service/AI/LLM/OpenAIService.cs | 254 +---------- .../Service/AI/LLM/PromptCachingHelper.cs | 425 ++++++++++-------- .../AI/LLM/Transports/OpenAiChatTransport.cs | 222 +++++++++ 3 files changed, 467 insertions(+), 434 deletions(-) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs index 98ae7ad4..d893b11c 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs @@ -191,7 +191,7 @@ private static List GetStablePrefixMessages(List provi } /// Compact, deterministic history serialization for prompt-cache key hashing. - private static List SerializeProviderHistory(List history) { + internal static List SerializeProviderHistory(List history) { var result = new List(); foreach (var msg in history) { string role; @@ -214,7 +214,7 @@ private static List SerializeProviderHistory(List> GetAllModels(LLMChannel channel) { if (channel.Provider.Equals(LLMProvider.Ollama)) { return new List(); @@ -1275,7 +1229,7 @@ private async IAsyncEnumerable ExecWithNativeToolCallingAsync( var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); var clientParts = BuildClientParts(channel, binding, ChatId); - var transport = new OpenAiChatTransport(this, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, + var transport = new Transports.OpenAiChatTransport(_logger, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, nativeTools: true, promptCachingEnabled, includeEmptyReasoningContent); var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); @@ -1318,7 +1272,7 @@ private async IAsyncEnumerable ExecWithXmlToolCallingAsync( var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); var clientParts = BuildClientParts(channel, binding, ChatId); - var transport = new OpenAiChatTransport(this, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, + var transport = new Transports.OpenAiChatTransport(_logger, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, nativeTools: false, promptCachingEnabled, includeEmptyReasoningContent); var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); @@ -1422,7 +1376,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSna var promptCachingEnabled = channel.Provider == LLMProvider.OpenAI && await IsPromptCachingEnabledAsync(); var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); var clientParts = BuildClientParts(channel, binding, snapshot.ChatId); - var transport = new OpenAiChatTransport(this, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, + var transport = new Transports.OpenAiChatTransport(_logger, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, nativeTools: false, promptCachingEnabled, includeEmptyReasoningContent); var meta = new LlmToolLoopMeta { @@ -1455,199 +1409,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSna /// Stateless transport adapter for the OpenAI Chat Completions API: converts the /// normalized history into SDK messages per turn and streams one assistant turn. /// - private sealed class OpenAiChatTransport : ILlmTransport { - private readonly OpenAIService _svc; - private readonly HttpClientPipelineTransport _pipelineTransport; - private readonly ApiKeyCredential _credential; - private readonly OpenAIClientOptions _clientOptions; - private readonly string _modelName; - private readonly LLMChannel _channel; - private readonly bool _nativeTools; - private readonly bool _promptCachingEnabled; - private readonly bool _includeEmptyReasoningContent; - - public OpenAiChatTransport(OpenAIService svc, HttpClientPipelineTransport pipelineTransport, - ApiKeyCredential credential, OpenAIClientOptions clientOptions, string modelName, LLMChannel channel, - bool nativeTools, bool promptCachingEnabled, bool includeEmptyReasoningContent) { - _svc = svc; - _pipelineTransport = pipelineTransport; - _credential = credential; - _clientOptions = clientOptions; - _modelName = modelName; - _channel = channel; - _nativeTools = nativeTools; - _promptCachingEnabled = promptCachingEnabled; - _includeEmptyReasoningContent = includeEmptyReasoningContent; - } - - public bool SupportsNativeTools => _nativeTools; - - public async IAsyncEnumerable StreamTurnAsync( - LlmTurnRequest request, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - var chatClient = new ChatClient(model: request.Config.ModelName, credential: _credential, _clientOptions); - var providerHistory = ToProviderHistory(request); - var completionOptions = new ChatCompletionOptions(); - if (_nativeTools && request.Tools is { Count: > 0 }) { - foreach (var spec in request.Tools) { - completionOptions.Tools.Add(ChatTool.CreateFunctionTool( - spec.Name, - spec.Description, - BinaryData.FromString(spec.ParametersJson))); - } - } - - var shouldObservePromptCaching = _channel.Provider == LLMProvider.OpenAI; - var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIService.BuildPromptCachingContext( - "OpenAI", - _modelName, - _nativeTools ? "chat-native" : "chat-xml", - providerHistory, - excludeDynamicTail: true); - var cacheKeyAttached = false; - if (promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(completionOptions, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); - cacheKeyAttached = true; - } - - var contentBuilder = new StringBuilder(); - var reasoningContentBuilder = new StringBuilder(); - var toolCallAccumulators = new Dictionary(); - ChatFinishReason? finishReason = null; - ChatTokenUsage latestUsage = null; - var streamedAny = false; - - await foreach (var update in chatClient.CompleteChatStreamingAsync(providerHistory, completionOptions, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { - if (updatePart?.Text != null) { - contentBuilder.Append(updatePart.Text); - streamedAny = true; - yield return new LlmStreamEvent.TextDelta(updatePart.Text); - } - } - - var reasoningUpdate = GetStreamingReasoningContent(update); - if (!string.IsNullOrEmpty(reasoningUpdate)) { - reasoningContentBuilder.Append(reasoningUpdate); - streamedAny = true; - yield return new LlmStreamEvent.ThinkingDelta(reasoningUpdate); - } - - foreach (var toolCallUpdate in update.ToolCallUpdates ?? Enumerable.Empty()) { - int index = toolCallUpdate.Index; - if (!toolCallAccumulators.ContainsKey(index)) { - toolCallAccumulators[index] = new ToolCallAccumulator(); - } - - var acc = toolCallAccumulators[index]; - if (toolCallUpdate.ToolCallId != null) acc.Id ??= toolCallUpdate.ToolCallId; - if (toolCallUpdate.FunctionName != null) acc.Name ??= toolCallUpdate.FunctionName; - if (toolCallUpdate.FunctionArgumentsUpdate != null) { - acc.Arguments.Append(toolCallUpdate.FunctionArgumentsUpdate); - } - } - - if (update.FinishReason.HasValue) { - finishReason = update.FinishReason.Value; - } - - if (update.Usage != null) { - latestUsage = update.Usage; - } - } - - if (shouldObservePromptCaching) { - _svc.LogOpenAiPromptCachingObservation( - "OpenAI", _channel, _modelName, promptCachingEnabled, - toolDefinitionHash, stablePrefixHash, promptCacheKey, - latestUsage, cacheKeyAttached); - } - - var responseText = contentBuilder.ToString().Trim(); - var reasoningContent = reasoningContentBuilder.ToString().Trim(); - var toolCalls = new List(); - var malformed = false; - - if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCallAccumulators.Any()) { - foreach (var (index, acc) in toolCallAccumulators) { - if (string.IsNullOrWhiteSpace(acc.Id)) { - _svc._logger.LogWarning("{ServiceName}: Tool call at index {Index} has no ID, generating fallback.", _svc.ServiceName, index); - } - toolCalls.Add(new LlmToolCall { - Id = NormalizeToolCallId(acc.Id), - Name = NormalizeToolCallName(acc.Name), - ArgumentsJson = NormalizeToolCallArguments(acc.Arguments.ToString()) - }); - } - } else if (toolCallAccumulators.Any()) { - _svc._logger.LogWarning( - "{ServiceName}: Native response contained tool call updates but finish reason was not ToolCalls. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}", - _svc.ServiceName, finishReason, toolCallAccumulators.Count); - } - - // Reasoning metadata sanity check (legacy behavior preserved): tool-call turns - // whose metadata could not be parsed at all are flagged for self-correction. - if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCalls.Count == 0) { - malformed = true; - } - - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = responseText, - Reasoning = reasoningContent, - ToolCalls = toolCalls, - MalformedToolCall = malformed, - StreamedAny = streamedAny - }); - } - - private List ToProviderHistory(LlmTurnRequest request) { - var messages = new List { new SystemChatMessage(request.SystemPrompt) }; - foreach (var m in request.History) { - switch (m.Role) { - case LlmRole.User: { - var parts = new List(); - if (!string.IsNullOrEmpty(m.Text)) { - parts.Add(ChatMessageContentPart.CreateTextPart(m.Text)); - } - if (m.ImagePng != null && request.Config.SupportsVision) { - parts.Add(ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(m.ImagePng), m.ImageMediaType ?? "image/png")); - } - messages.Add(parts.Count > 0 ? new UserChatMessage(parts) : new UserChatMessage(m.Text ?? string.Empty)); - break; - } - case LlmRole.Assistant: { - if (m.ToolCalls is { Count: > 0 }) { - var calls = m.ToolCalls.Select(tc => ChatToolCall.CreateFunctionToolCall( - tc.Id, tc.Name, BinaryData.FromString(tc.ArgumentsJson))).ToList(); - var assistant = new AssistantChatMessage(calls); - if (!string.IsNullOrWhiteSpace(m.Text)) { - assistant = new AssistantChatMessage(calls) { Content = { ChatMessageContentPart.CreateTextPart(m.Text) } }; - } - SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); - messages.Add(assistant); - } else { - var assistant = new AssistantChatMessage(m.Text ?? string.Empty); - SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); - messages.Add(assistant); - } - break; - } - case LlmRole.Tool: - messages.Add(new ToolChatMessage(m.ToolCallId ?? string.Empty, m.Text ?? string.Empty)); - break; - } - } - return messages; - } - } - /// - /// Extract reasoning_content from AssistantChatMessage if available. - /// For thinking mode models, the reasoning process is returned separately. - /// - private static string? GetAssistantReasoningContent(AssistantChatMessage assistantMsg) { + internal static string? GetAssistantReasoningContent(AssistantChatMessage assistantMsg) { // Try to access reasoning content - OpenAI Chat SDK may store it in various ways // The reasoning_content is typically available via reflection or specific properties try { @@ -1677,7 +1439,7 @@ private List ToProviderHistory(LlmTurnRequest request) { /// Extract reasoning_content from streaming update for thinking mode models. /// Uses Patch API first (OpenAI SDK), falls back to reflection for internal properties. /// - private static string? GetStreamingReasoningContent(StreamingChatCompletionUpdate update) { + internal static string? GetStreamingReasoningContent(StreamingChatCompletionUpdate update) { // Primary: use Patch API to read reasoning_content from raw JSON response #pragma warning disable SCME0001 // Patch is for evaluation, may be changed in future if (update.Patch.TryGetValue("$.choices[0].delta.reasoning_content"u8, out string? reasoningFromPatch)) { @@ -1748,7 +1510,7 @@ internal static bool ShouldIncludeEmptyReasoningContent(LLMChannel channel, stri model.Contains("deepseek", StringComparison.OrdinalIgnoreCase); } - private static void SetAssistantReasoningContent(AssistantChatMessage msg, string reasoningContent, bool includeEmptyReasoningContent = false) { + internal static void SetAssistantReasoningContent(AssistantChatMessage msg, string reasoningContent, bool includeEmptyReasoningContent = false) { if (reasoningContent is null || (reasoningContent.Length == 0 && !includeEmptyReasoningContent)) { return; } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs index 2c18a32d..cce300ee 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs @@ -1,188 +1,237 @@ -#pragma warning disable OPENAI001 -#pragma warning disable SCME0001 - -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography; -using System.Text; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using OpenAI.Chat; -using OpenAI.Responses; -using TelegramSearchBot.Model.AI; - -namespace TelegramSearchBot.Service.AI.LLM { - public static class PromptCachingOutcomes { - public const string Disabled = "disabled"; - public const string Unsupported = "unsupported"; - public const string AttemptedWrite = "attempted_write"; - public const string Hit = "hit"; - public const string Miss = "miss"; - public const string NoSignal = "no_signal"; - public const string Error = "error"; - } - - public sealed class PromptCachingObservation { - public string Provider { get; set; } = string.Empty; - public int ChannelId { get; set; } - public string Model { get; set; } = string.Empty; - public bool PromptCachingEnabled { get; set; } - public string StablePrefixHash { get; set; } = string.Empty; - public string ToolDefinitionHash { get; set; } = string.Empty; - public string CacheOutcome { get; set; } = PromptCachingOutcomes.NoSignal; - public string MissReason { get; set; } - public string PromptCacheKey { get; set; } - public string PromptCacheRetention { get; set; } - public bool CacheKeyAttached { get; set; } - public bool CacheBreakpointInserted { get; set; } - public int? CachedTokenCount { get; set; } - public long? CacheCreationInputTokens { get; set; } - public long? CacheReadInputTokens { get; set; } - public string ProviderUsageJson { get; set; } - } - - public static class PromptCachingHelper { - public const string OpenAiDefaultPromptCacheRetention = "in_memory"; - - private static readonly JsonSerializerSettings CanonicalJsonSettings = new() { - Formatting = Formatting.None, - NullValueHandling = NullValueHandling.Ignore, - }; - - private static readonly ConcurrentDictionary SeenCacheRoutingKeys = new(StringComparer.Ordinal); - - public static string ComputeToolDefinitionHash() { - return ComputeCanonicalHash(McpToolHelper.ExportToolDefinitions()); - } - - public static string ComputeStablePrefixHash(object payload) { - return ComputeCanonicalHash(payload); - } - - public static string BuildOpenAiPromptCacheKey(string provider, string model, string toolDefinitionHash, string stablePrefixHash) { - return string.Join(":", new[] { - SanitizeKeySegment(provider), - SanitizeKeySegment(model), - toolDefinitionHash, - stablePrefixHash, - }); - } - - public static void ApplyOpenAiPromptCaching(ChatCompletionOptions options, string promptCacheKey, string promptCacheRetention) { - ref var patch = ref options.Patch; - patch.Set("$.prompt_cache_key"u8, promptCacheKey); - if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { - patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); - } - } - - public static void ApplyOpenAiPromptCaching(CreateResponseOptions options, string promptCacheKey, string promptCacheRetention) { - ref var patch = ref options.Patch; - patch.Set("$.prompt_cache_key"u8, promptCacheKey); - if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { - patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); - } - } - - public static string DetermineOpenAiOutcome(bool promptCachingEnabled, bool cacheKeyAttached, string promptCacheKey, int? cachedTokenCount, out string missReason) { - missReason = null; - if (!promptCachingEnabled) { - return PromptCachingOutcomes.Disabled; - } - - if (!cacheKeyAttached || string.IsNullOrWhiteSpace(promptCacheKey)) { - missReason = "prompt_cache_key_not_attached"; - return PromptCachingOutcomes.Unsupported; - } - - if (!cachedTokenCount.HasValue) { - missReason = "cached_tokens_unavailable"; - return PromptCachingOutcomes.NoSignal; - } - - if (cachedTokenCount.Value > 0) { - SeenCacheRoutingKeys[promptCacheKey] = 0; - return PromptCachingOutcomes.Hit; - } - - var firstObservation = SeenCacheRoutingKeys.TryAdd(promptCacheKey, 0); - missReason = firstObservation - ? "first_observation_without_cached_tokens" - : "repeated_observation_without_cached_tokens"; - return firstObservation - ? PromptCachingOutcomes.AttemptedWrite - : PromptCachingOutcomes.Miss; - } - - public static string DetermineAnthropicOutcome(bool promptCachingEnabled, bool cacheBreakpointInserted, string observationKey, long? cacheCreationInputTokens, long? cacheReadInputTokens, out string missReason) { - missReason = null; - if (!promptCachingEnabled) { - return PromptCachingOutcomes.Disabled; - } - - if (!cacheBreakpointInserted) { - missReason = "cache_control_not_inserted"; - return PromptCachingOutcomes.Unsupported; - } - - if (cacheReadInputTokens.GetValueOrDefault() > 0) { - SeenCacheRoutingKeys[observationKey] = 0; - return PromptCachingOutcomes.Hit; - } - - if (cacheCreationInputTokens.GetValueOrDefault() > 0) { - SeenCacheRoutingKeys[observationKey] = 0; - return PromptCachingOutcomes.AttemptedWrite; - } - - var firstObservation = SeenCacheRoutingKeys.TryAdd(observationKey, 0); - missReason = firstObservation - ? "usage_reported_zero_cache_tokens" - : "repeated_prefix_without_cache_read"; - return firstObservation - ? PromptCachingOutcomes.NoSignal - : PromptCachingOutcomes.Miss; - } - - public static void LogObservation(ILogger logger, PromptCachingObservation observation) { - logger.LogInformation( - "Prompt caching observation. Provider={Provider}, ChannelId={ChannelId}, Model={Model}, PromptCachingEnabled={PromptCachingEnabled}, StablePrefixHash={StablePrefixHash}, ToolDefinitionHash={ToolDefinitionHash}, CacheOutcome={CacheOutcome}, MissReason={MissReason}, PromptCacheKey={PromptCacheKey}, PromptCacheRetention={PromptCacheRetention}, CacheKeyAttached={CacheKeyAttached}, CacheBreakpointInserted={CacheBreakpointInserted}, CachedTokenCount={CachedTokenCount}, CacheCreationInputTokens={CacheCreationInputTokens}, CacheReadInputTokens={CacheReadInputTokens}, ProviderUsage={ProviderUsage}", - observation.Provider, - observation.ChannelId, - observation.Model, - observation.PromptCachingEnabled, - observation.StablePrefixHash, - observation.ToolDefinitionHash, - observation.CacheOutcome, - observation.MissReason, - observation.PromptCacheKey, - observation.PromptCacheRetention, - observation.CacheKeyAttached, - observation.CacheBreakpointInserted, - observation.CachedTokenCount, - observation.CacheCreationInputTokens, - observation.CacheReadInputTokens, - observation.ProviderUsageJson); - } - - private static string ComputeCanonicalHash(object payload) { - var json = JsonConvert.SerializeObject(payload, CanonicalJsonSettings); - var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(json)); - return Convert.ToHexString(bytes).ToLowerInvariant(); - } - - private static string SanitizeKeySegment(string value) { - if (string.IsNullOrWhiteSpace(value)) { - return "unknown"; - } - - var builder = new StringBuilder(value.Length); - foreach (var c in value.Trim()) { - builder.Append(char.IsLetterOrDigit(c) || c is '-' or '_' or '.' ? c : '_'); - } - return builder.ToString(); - } - } -} +#pragma warning disable OPENAI001 +#pragma warning disable SCME0001 + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using OpenAI.Chat; +using OpenAI.Responses; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM { + public static class PromptCachingOutcomes { + public const string Disabled = "disabled"; + public const string Unsupported = "unsupported"; + public const string AttemptedWrite = "attempted_write"; + public const string Hit = "hit"; + public const string Miss = "miss"; + public const string NoSignal = "no_signal"; + public const string Error = "error"; + } + + public sealed class PromptCachingObservation { + public string Provider { get; set; } = string.Empty; + public int ChannelId { get; set; } + public string Model { get; set; } = string.Empty; + public bool PromptCachingEnabled { get; set; } + public string StablePrefixHash { get; set; } = string.Empty; + public string ToolDefinitionHash { get; set; } = string.Empty; + public string CacheOutcome { get; set; } = PromptCachingOutcomes.NoSignal; + public string MissReason { get; set; } + public string PromptCacheKey { get; set; } + public string PromptCacheRetention { get; set; } + public bool CacheKeyAttached { get; set; } + public bool CacheBreakpointInserted { get; set; } + public int? CachedTokenCount { get; set; } + public long? CacheCreationInputTokens { get; set; } + public long? CacheReadInputTokens { get; set; } + public string ProviderUsageJson { get; set; } + } + + public static class PromptCachingHelper { + public const string OpenAiDefaultPromptCacheRetention = "in_memory"; + + private static readonly JsonSerializerSettings CanonicalJsonSettings = new() { + Formatting = Formatting.None, + NullValueHandling = NullValueHandling.Ignore, + }; + + private static readonly ConcurrentDictionary SeenCacheRoutingKeys = new(StringComparer.Ordinal); + + public static string ComputeToolDefinitionHash() { + return ComputeCanonicalHash(McpToolHelper.ExportToolDefinitions()); + } + + public static string ComputeStablePrefixHash(object payload) { + return ComputeCanonicalHash(payload); + } + + public static string BuildOpenAiPromptCacheKey(string provider, string model, string toolDefinitionHash, string stablePrefixHash) { + return string.Join(":", new[] { + SanitizeKeySegment(provider), + SanitizeKeySegment(model), + toolDefinitionHash, + stablePrefixHash, + }); + } + + public static void ApplyOpenAiPromptCaching(ChatCompletionOptions options, string promptCacheKey, string promptCacheRetention) { + ref var patch = ref options.Patch; + patch.Set("$.prompt_cache_key"u8, promptCacheKey); + if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { + patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); + } + } + + public static void ApplyOpenAiPromptCaching(CreateResponseOptions options, string promptCacheKey, string promptCacheRetention) { + ref var patch = ref options.Patch; + patch.Set("$.prompt_cache_key"u8, promptCacheKey); + if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { + patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); + } + } + + /// Logs one OpenAI prompt-caching observation (moved from OpenAIService instance method). + public static void LogOpenAiPromptCachingObservation( + ILogger logger, + string providerName, + LLMChannel channel, + string modelName, + bool promptCachingEnabled, + string toolDefinitionHash, + string stablePrefixHash, + string promptCacheKey, + ChatTokenUsage usage, + bool cacheKeyAttached) { + var cachedTokenCount = usage?.InputTokenDetails?.CachedTokenCount; + var usageJson = usage == null + ? null + : JsonConvert.SerializeObject(new { + usage.InputTokenCount, + usage.OutputTokenCount, + usage.TotalTokenCount, + CachedTokenCount = usage.InputTokenDetails?.CachedTokenCount, + ReasoningTokenCount = usage.OutputTokenDetails?.ReasoningTokenCount, + AudioInputTokenCount = usage.InputTokenDetails?.AudioTokenCount, + AudioOutputTokenCount = usage.OutputTokenDetails?.AudioTokenCount, + }); + var outcome = DetermineOpenAiOutcome( + promptCachingEnabled, + cacheKeyAttached, + promptCacheKey, + cachedTokenCount, + out var missReason); + + LogObservation(logger, new PromptCachingObservation { + Provider = providerName, + ChannelId = channel.Id, + Model = modelName, + PromptCachingEnabled = promptCachingEnabled, + StablePrefixHash = stablePrefixHash, + ToolDefinitionHash = toolDefinitionHash, + CacheOutcome = outcome, + MissReason = missReason, + PromptCacheKey = promptCacheKey, + PromptCacheRetention = OpenAiDefaultPromptCacheRetention, + CacheKeyAttached = cacheKeyAttached, + CachedTokenCount = cachedTokenCount, + ProviderUsageJson = usageJson, + }); + } + + public static string DetermineOpenAiOutcome(bool promptCachingEnabled, bool cacheKeyAttached, string promptCacheKey, int? cachedTokenCount, out string missReason) { + missReason = null; + if (!promptCachingEnabled) { + return PromptCachingOutcomes.Disabled; + } + + if (!cacheKeyAttached || string.IsNullOrWhiteSpace(promptCacheKey)) { + missReason = "prompt_cache_key_not_attached"; + return PromptCachingOutcomes.Unsupported; + } + + if (!cachedTokenCount.HasValue) { + missReason = "cached_tokens_unavailable"; + return PromptCachingOutcomes.NoSignal; + } + + if (cachedTokenCount.Value > 0) { + SeenCacheRoutingKeys[promptCacheKey] = 0; + return PromptCachingOutcomes.Hit; + } + + var firstObservation = SeenCacheRoutingKeys.TryAdd(promptCacheKey, 0); + missReason = firstObservation + ? "first_observation_without_cached_tokens" + : "repeated_observation_without_cached_tokens"; + return firstObservation + ? PromptCachingOutcomes.AttemptedWrite + : PromptCachingOutcomes.Miss; + } + + public static string DetermineAnthropicOutcome(bool promptCachingEnabled, bool cacheBreakpointInserted, string observationKey, long? cacheCreationInputTokens, long? cacheReadInputTokens, out string missReason) { + missReason = null; + if (!promptCachingEnabled) { + return PromptCachingOutcomes.Disabled; + } + + if (!cacheBreakpointInserted) { + missReason = "cache_control_not_inserted"; + return PromptCachingOutcomes.Unsupported; + } + + if (cacheReadInputTokens.GetValueOrDefault() > 0) { + SeenCacheRoutingKeys[observationKey] = 0; + return PromptCachingOutcomes.Hit; + } + + if (cacheCreationInputTokens.GetValueOrDefault() > 0) { + SeenCacheRoutingKeys[observationKey] = 0; + return PromptCachingOutcomes.AttemptedWrite; + } + + var firstObservation = SeenCacheRoutingKeys.TryAdd(observationKey, 0); + missReason = firstObservation + ? "usage_reported_zero_cache_tokens" + : "repeated_prefix_without_cache_read"; + return firstObservation + ? PromptCachingOutcomes.NoSignal + : PromptCachingOutcomes.Miss; + } + + public static void LogObservation(ILogger logger, PromptCachingObservation observation) { + logger.LogInformation( + "Prompt caching observation. Provider={Provider}, ChannelId={ChannelId}, Model={Model}, PromptCachingEnabled={PromptCachingEnabled}, StablePrefixHash={StablePrefixHash}, ToolDefinitionHash={ToolDefinitionHash}, CacheOutcome={CacheOutcome}, MissReason={MissReason}, PromptCacheKey={PromptCacheKey}, PromptCacheRetention={PromptCacheRetention}, CacheKeyAttached={CacheKeyAttached}, CacheBreakpointInserted={CacheBreakpointInserted}, CachedTokenCount={CachedTokenCount}, CacheCreationInputTokens={CacheCreationInputTokens}, CacheReadInputTokens={CacheReadInputTokens}, ProviderUsage={ProviderUsage}", + observation.Provider, + observation.ChannelId, + observation.Model, + observation.PromptCachingEnabled, + observation.StablePrefixHash, + observation.ToolDefinitionHash, + observation.CacheOutcome, + observation.MissReason, + observation.PromptCacheKey, + observation.PromptCacheRetention, + observation.CacheKeyAttached, + observation.CacheBreakpointInserted, + observation.CachedTokenCount, + observation.CacheCreationInputTokens, + observation.CacheReadInputTokens, + observation.ProviderUsageJson); + } + + private static string ComputeCanonicalHash(object payload) { + var json = JsonConvert.SerializeObject(payload, CanonicalJsonSettings); + var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(json)); + return Convert.ToHexString(bytes).ToLowerInvariant(); + } + + private static string SanitizeKeySegment(string value) { + if (string.IsNullOrWhiteSpace(value)) { + return "unknown"; + } + + var builder = new StringBuilder(value.Length); + foreach (var c in value.Trim()) { + builder.Append(char.IsLetterOrDigit(c) || c is '-' or '_' or '.' ? c : '_'); + } + return builder.ToString(); + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs new file mode 100644 index 00000000..12b78cf5 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs @@ -0,0 +1,222 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using OpenAI; +using OpenAI.Chat; +using System.ClientModel; +using System.ClientModel.Primitives; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + /// Mutable accumulator for streaming tool call updates. + public class ToolCallAccumulator { + public string Id { get; set; } + public string Name { get; set; } + public StringBuilder Arguments { get; } = new StringBuilder(); + } + + /// + /// Stateless transport adapter for the OpenAI Chat Completions API: converts the + /// normalized history into SDK messages per turn and streams one assistant turn. + /// + public sealed class OpenAiChatTransport : ILlmTransport { + private readonly ILogger _logger; + private readonly HttpClientPipelineTransport _pipelineTransport; + private readonly ApiKeyCredential _credential; + private readonly OpenAIClientOptions _clientOptions; + private readonly string _modelName; + private readonly LLMChannel _channel; + private readonly bool _nativeTools; + private readonly bool _promptCachingEnabled; + private readonly bool _includeEmptyReasoningContent; + + public OpenAiChatTransport(ILogger logger, HttpClientPipelineTransport pipelineTransport, + ApiKeyCredential credential, OpenAIClientOptions clientOptions, string modelName, LLMChannel channel, + bool nativeTools, bool promptCachingEnabled, bool includeEmptyReasoningContent) { + _logger = logger; + _pipelineTransport = pipelineTransport; + _credential = credential; + _clientOptions = clientOptions; + _modelName = modelName; + _channel = channel; + _nativeTools = nativeTools; + _promptCachingEnabled = promptCachingEnabled; + _includeEmptyReasoningContent = includeEmptyReasoningContent; + } + + public bool SupportsNativeTools => _nativeTools; + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + var chatClient = new ChatClient(model: request.Config.ModelName, credential: _credential, _clientOptions); + var providerHistory = ToProviderHistory(request); + var completionOptions = new ChatCompletionOptions(); + if (_nativeTools && request.Tools is { Count: > 0 }) { + foreach (var spec in request.Tools) { + completionOptions.Tools.Add(ChatTool.CreateFunctionTool( + spec.Name, + spec.Description, + BinaryData.FromString(spec.ParametersJson))); + } + } + + var shouldObservePromptCaching = _channel.Provider == LLMProvider.OpenAI; + var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; + var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIService.BuildPromptCachingContext( + "OpenAI", + _modelName, + _nativeTools ? "chat-native" : "chat-xml", + providerHistory, + excludeDynamicTail: true); + var cacheKeyAttached = false; + if (promptCachingEnabled) { + PromptCachingHelper.ApplyOpenAiPromptCaching(completionOptions, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); + cacheKeyAttached = true; + } + + var contentBuilder = new StringBuilder(); + var reasoningContentBuilder = new StringBuilder(); + var toolCallAccumulators = new Dictionary(); + ChatFinishReason? finishReason = null; + ChatTokenUsage latestUsage = null; + var streamedAny = false; + + await foreach (var update in chatClient.CompleteChatStreamingAsync(providerHistory, completionOptions, cancellationToken).WithCancellation(cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + + foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { + if (updatePart?.Text != null) { + contentBuilder.Append(updatePart.Text); + streamedAny = true; + yield return new LlmStreamEvent.TextDelta(updatePart.Text); + } + } + + var reasoningUpdate = OpenAIService.GetStreamingReasoningContent(update); + if (!string.IsNullOrEmpty(reasoningUpdate)) { + reasoningContentBuilder.Append(reasoningUpdate); + streamedAny = true; + yield return new LlmStreamEvent.ThinkingDelta(reasoningUpdate); + } + + foreach (var toolCallUpdate in update.ToolCallUpdates ?? Enumerable.Empty()) { + int index = toolCallUpdate.Index; + if (!toolCallAccumulators.ContainsKey(index)) { + toolCallAccumulators[index] = new ToolCallAccumulator(); + } + + var acc = toolCallAccumulators[index]; + if (toolCallUpdate.ToolCallId != null) acc.Id ??= toolCallUpdate.ToolCallId; + if (toolCallUpdate.FunctionName != null) acc.Name ??= toolCallUpdate.FunctionName; + if (toolCallUpdate.FunctionArgumentsUpdate != null) { + acc.Arguments.Append(toolCallUpdate.FunctionArgumentsUpdate); + } + } + + if (update.FinishReason.HasValue) { + finishReason = update.FinishReason.Value; + } + + if (update.Usage != null) { + latestUsage = update.Usage; + } + } + + if (shouldObservePromptCaching) { + PromptCachingHelper.LogOpenAiPromptCachingObservation( + _logger, "OpenAI", _channel, _modelName, promptCachingEnabled, + toolDefinitionHash, stablePrefixHash, promptCacheKey, + latestUsage, cacheKeyAttached); + } + + var responseText = contentBuilder.ToString().Trim(); + var reasoningContent = reasoningContentBuilder.ToString().Trim(); + var toolCalls = new List(); + var malformed = false; + + if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCallAccumulators.Any()) { + foreach (var (index, acc) in toolCallAccumulators) { + if (string.IsNullOrWhiteSpace(acc.Id)) { + _logger.LogWarning("OpenAIService: Tool call at index {Index} has no ID, generating fallback.", index); + } + toolCalls.Add(new LlmToolCall { + Id = OpenAIService.NormalizeToolCallId(acc.Id), + Name = OpenAIService.NormalizeToolCallName(acc.Name), + ArgumentsJson = OpenAIService.NormalizeToolCallArguments(acc.Arguments.ToString()) + }); + } + } else if (toolCallAccumulators.Any()) { + _logger.LogWarning( + "OpenAIService: Native response contained tool call updates but finish reason was not ToolCalls. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}", + finishReason, toolCallAccumulators.Count); + } + + // Reasoning metadata sanity check (legacy behavior preserved): tool-call turns + // whose metadata could not be parsed at all are flagged for self-correction. + if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCalls.Count == 0) { + malformed = true; + } + + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = responseText, + Reasoning = reasoningContent, + ToolCalls = toolCalls, + MalformedToolCall = malformed, + StreamedAny = streamedAny + }); + } + + private List ToProviderHistory(LlmTurnRequest request) { + var messages = new List { new SystemChatMessage(request.SystemPrompt) }; + foreach (var m in request.History) { + switch (m.Role) { + case LlmRole.User: { + var parts = new List(); + if (!string.IsNullOrEmpty(m.Text)) { + parts.Add(ChatMessageContentPart.CreateTextPart(m.Text)); + } + if (m.ImagePng != null && request.Config.SupportsVision) { + parts.Add(ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(m.ImagePng), m.ImageMediaType ?? "image/png")); + } + messages.Add(parts.Count > 0 ? new UserChatMessage(parts) : new UserChatMessage(m.Text ?? string.Empty)); + break; + } + case LlmRole.Assistant: { + if (m.ToolCalls is { Count: > 0 }) { + var calls = m.ToolCalls.Select(tc => ChatToolCall.CreateFunctionToolCall( + tc.Id, tc.Name, BinaryData.FromString(tc.ArgumentsJson))).ToList(); + var assistant = new AssistantChatMessage(calls); + if (!string.IsNullOrWhiteSpace(m.Text)) { + assistant = new AssistantChatMessage(calls) { Content = { ChatMessageContentPart.CreateTextPart(m.Text) } }; + } + OpenAIService.SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); + messages.Add(assistant); + } else { + var assistant = new AssistantChatMessage(m.Text ?? string.Empty); + OpenAIService.SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); + messages.Add(assistant); + } + break; + } + case LlmRole.Tool: + messages.Add(new ToolChatMessage(m.ToolCallId ?? string.Empty, m.Text ?? string.Empty)); + break; + } + } + return messages; + } + } + /// + /// Extract reasoning_content from AssistantChatMessage if available. + /// For thinking mode models, the reasoning process is returned separately. + /// +} From 2e23d7b6f6dc4cefc7fbcd676bf53318b299414b Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 09:22:31 +0800 Subject: [PATCH 13/30] refactor(llm): extract AnthropicMessagesTransport to Transports/ --- .../Service/AI/LLM/AnthropicService.cs | 274 +----------------- .../Service/AI/LLM/PromptCachingHelper.cs | 39 +++ .../Transports/AnthropicMessagesTransport.cs | 269 +++++++++++++++++ 3 files changed, 316 insertions(+), 266 deletions(-) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs index 9d585d61..a9e48fd6 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs @@ -114,11 +114,11 @@ private async Task IsPromptCachingEnabledAsync() { return _promptCachingSettingsService == null || await _promptCachingSettingsService.IsEnabledAsync(); } - private static CacheControlEphemeral CreateCacheControl() { + internal static CacheControlEphemeral CreateCacheControl() { return new CacheControlEphemeral(); } - private static MessageCreateParamsSystem BuildSystemPrompt(string systemPrompt, bool enablePromptCaching) { + internal static MessageCreateParamsSystem BuildSystemPrompt(string systemPrompt, bool enablePromptCaching) { if (!enablePromptCaching) { return systemPrompt; } @@ -178,7 +178,7 @@ private static MessageParam CloneMessageForCachePrefix(MessageParam message, boo }; } - private static List PrepareMessagesForPromptCaching(List providerHistory, bool enablePromptCaching, bool excludeDynamicTail, out bool cacheBreakpointInserted) { + internal static List PrepareMessagesForPromptCaching(List providerHistory, bool enablePromptCaching, bool excludeDynamicTail, out bool cacheBreakpointInserted) { cacheBreakpointInserted = enablePromptCaching; if (!enablePromptCaching || providerHistory.Count == 0) { return providerHistory; @@ -198,7 +198,7 @@ private static List PrepareMessagesForPromptCaching(List stableHistory) { + internal static (string toolDefinitionHash, string stablePrefixHash) BuildPromptCachingContext(string mode, string systemPrompt, List stableHistory) { var toolDefinitionHash = PromptCachingHelper.ComputeToolDefinitionHash(); var stablePrefixHash = PromptCachingHelper.ComputeStablePrefixHash(new { Mode = mode, @@ -449,7 +449,7 @@ private void AddMessageToHistory(List history, long fromUserId, st /// Ensures message list starts with user and alternates between user/assistant. /// Merges consecutive same-role messages. /// - private static List EnsureAlternatingRoles(List messages) { + internal static List EnsureAlternatingRoles(List messages) { if (!messages.Any()) return messages; var result = new List(); @@ -742,7 +742,7 @@ public async IAsyncEnumerable ExecWithNativeToolCallingAsync( var promptCachingEnabled = await IsPromptCachingEnabledAsync(); using var client = CreateClient(channel, binding); - var transport = new AnthropicMessagesTransport(this, client, systemPrompt, modelName, channel, + var transport = new Transports.AnthropicMessagesTransport(_logger, client, systemPrompt, modelName, channel, nativeTools: true, promptCachingEnabled); var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); @@ -793,7 +793,7 @@ private async IAsyncEnumerable ExecWithXmlToolCallingAsync( var promptCachingEnabled = await IsPromptCachingEnabledAsync(); using var client = CreateClient(channel, binding); - var transport = new AnthropicMessagesTransport(this, client, systemPrompt, modelName, channel, + var transport = new Transports.AnthropicMessagesTransport(_logger, client, systemPrompt, modelName, channel, nativeTools: false, promptCachingEnabled); var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); @@ -882,7 +882,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( var promptCachingEnabled = await IsPromptCachingEnabledAsync(); using var client = CreateClient(channel, binding); - var transport = new AnthropicMessagesTransport(this, client, systemPrompt, modelName, channel, + var transport = new Transports.AnthropicMessagesTransport(_logger, client, systemPrompt, modelName, channel, nativeTools: false, promptCachingEnabled); var meta = new LlmToolLoopMeta { @@ -922,262 +922,6 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( #endregion - /// - /// Stateless transport adapter for the Anthropic Messages API. Converts the normalized - /// history to MessageParam per turn (including tool_use/tool_result blocks and prompt - /// cache breakpoints) and streams one assistant turn. - /// - private sealed class AnthropicMessagesTransport : ILlmTransport { - private readonly AnthropicService _svc; - private readonly AnthropicClient _client; - private readonly string _systemPrompt; - private readonly string _modelName; - private readonly LLMChannel _channel; - private readonly bool _nativeTools; - private readonly bool _promptCachingEnabled; - private List? _preparedHistory; - private int _convertedCount; - private bool _cacheBreakpointInserted; - - public AnthropicMessagesTransport(AnthropicService svc, AnthropicClient client, string systemPrompt, - string modelName, LLMChannel channel, bool nativeTools, bool promptCachingEnabled) { - _svc = svc; - _client = client; - _systemPrompt = systemPrompt; - _modelName = modelName; - _channel = channel; - _nativeTools = nativeTools; - _promptCachingEnabled = promptCachingEnabled; - } - - private LlmTurnRequest? _lastRequest; - - public bool SupportsNativeTools => _nativeTools; - - public async IAsyncEnumerable StreamTurnAsync( - LlmTurnRequest request, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - _lastRequest = request; - // Legacy wire semantics: prompt-cache preparation runs once per run (first turn); - // later turns append raw deltas so user messages stay string-typed on the wire. - if (_preparedHistory == null) { - _preparedHistory = PrepareMessagesForPromptCaching(ToProviderHistory(request.History), _promptCachingEnabled, excludeDynamicTail: true, out var cacheBreakpointInserted); - _cacheBreakpointInserted = cacheBreakpointInserted; - } else if (request.History.Count > _convertedCount) { - _preparedHistory.AddRange(ToProviderHistory(request.History.Skip(_convertedCount))); - } - _convertedCount = request.History.Count; - var providerHistory = _preparedHistory; - try { System.IO.File.AppendAllText("C:/temp/anthropic_dbg.log", "caching=" + _promptCachingEnabled + " " + System.Text.Json.JsonSerializer.Serialize(new { P = providerHistory }) + Environment.NewLine + "###" + Environment.NewLine); } catch { } - - var nativeToolSpecs = request.Tools; - var rawHistory = providerHistory.ToList(); - var (toolDefinitionHash, stablePrefixHash) = BuildPromptCachingContext( - _nativeTools ? "anthropic-native" : "anthropic-xml", - _systemPrompt, - SerializeProviderHistory(_systemPrompt, rawHistory)); - var parameters = new MessageCreateParams { - Model = _modelName, - MaxTokens = 8192, - System = BuildSystemPrompt(_systemPrompt, _promptCachingEnabled), - Messages = providerHistory, - Tools = _nativeTools && nativeToolSpecs is { Count: > 0 } - ? ConvertToAnthropicToolSpecs(nativeToolSpecs, _promptCachingEnabled) - : null, - }; - - - long? cacheCreationInputTokens = null; - long? cacheReadInputTokens = null; - object usageObservation = null; - - var turnText = new StringBuilder(); - var toolUseBlocks = new List<(string id, string name, string inputJson)>(); - var currentToolInputBuilder = new StringBuilder(); - string currentToolId = null; - string currentToolName = null; - - await foreach (var rawEvent in _client.Messages.CreateStreaming(parameters, cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - if (rawEvent.TryPickStart(out var messageStartEvent) && messageStartEvent.Message?.Usage != null) { - usageObservation = new { - messageStartEvent.Message.Usage.InputTokens, - messageStartEvent.Message.Usage.OutputTokens, - messageStartEvent.Message.Usage.CacheCreationInputTokens, - messageStartEvent.Message.Usage.CacheReadInputTokens, - RawData = messageStartEvent.Message.Usage.RawData, - }; - } - - if (rawEvent.TryPickDelta(out var messageDeltaEvent) && messageDeltaEvent.Usage != null) { - cacheCreationInputTokens = messageDeltaEvent.Usage.CacheCreationInputTokens ?? cacheCreationInputTokens; - cacheReadInputTokens = messageDeltaEvent.Usage.CacheReadInputTokens ?? cacheReadInputTokens; - usageObservation = new { - messageDeltaEvent.Usage.InputTokens, - messageDeltaEvent.Usage.OutputTokens, - messageDeltaEvent.Usage.CacheCreationInputTokens, - messageDeltaEvent.Usage.CacheReadInputTokens, - RawData = messageDeltaEvent.Usage.RawData, - }; - } - - if (rawEvent.TryPickContentBlockStart(out var startEvent)) { - if (startEvent.ContentBlock.TryPickToolUse(out var toolUseStart)) { - currentToolId = toolUseStart.ID; - currentToolName = toolUseStart.Name; - currentToolInputBuilder.Clear(); - } - } else if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { - if (deltaEvent.Delta.TryPickText(out var textDelta)) { - turnText.Append(textDelta.Text); - yield return new LlmStreamEvent.TextDelta(textDelta.Text); - } else if (deltaEvent.Delta.TryPickInputJson(out var inputJsonDelta)) { - currentToolInputBuilder.Append(inputJsonDelta.PartialJson); - } - } else if (rawEvent.TryPickContentBlockStop(out _)) { - if (currentToolId != null) { - toolUseBlocks.Add((currentToolId, currentToolName, currentToolInputBuilder.ToString())); - currentToolId = null; - currentToolName = null; - } - } - } - - _svc.LogPromptCachingObservation( - _channel, "Anthropic", _modelName, _promptCachingEnabled, - toolDefinitionHash, stablePrefixHash, _cacheBreakpointInserted, - cacheCreationInputTokens, cacheReadInputTokens, usageObservation); - - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = turnText.ToString().Trim(), - ToolCalls = toolUseBlocks.Select(b => new LlmToolCall { - Id = b.id, - Name = b.name, - ArgumentsJson = string.IsNullOrWhiteSpace(b.inputJson) ? "{}" : b.inputJson - }).ToList(), - StreamedAny = turnText.Length > 0, - UsageObservation = usageObservation - }); - } - - private List ToProviderHistory(IEnumerable messages) { - var request = _lastRequest!; - - var result = new List(); - var pendingToolResults = new List(); - - void FlushToolResults() { - if (pendingToolResults.Count > 0) { - result.Add(new MessageParam { Role = Role.User, Content = pendingToolResults.ToList() }); - pendingToolResults.Clear(); - } - } - - foreach (var m in request.History) { - switch (m.Role) { - case LlmRole.User: { - FlushToolResults(); - if (m.ImagePng != null && request.Config.SupportsVision) { - var blocks = new List(); - if (!string.IsNullOrEmpty(m.Text)) { - blocks.Add(new TextBlockParam(m.Text)); - } - blocks.Add(new ImageBlockParam(new Base64ImageSource { - Data = Convert.ToBase64String(m.ImagePng), - MediaType = MediaType.ImagePng - })); - result.Add(new MessageParam { Role = Role.User, Content = blocks }); - } else { - // Plain-text user messages use string content (wire-compatible with legacy). - result.Add(new MessageParam { Role = Role.User, Content = m.Text ?? string.Empty }); - } - break; - } - case LlmRole.Assistant: { - FlushToolResults(); - var blocks = new List(); - if (!string.IsNullOrEmpty(m.Text)) { - blocks.Add(new TextBlockParam(m.Text)); - } - if (m.ToolCalls is { Count: > 0 }) { - foreach (var tc in m.ToolCalls) { - Dictionary parsedInput; - try { - parsedInput = string.IsNullOrWhiteSpace(tc.ArgumentsJson) - ? new Dictionary() - : System.Text.Json.JsonSerializer.Deserialize>(tc.ArgumentsJson) - ?? new Dictionary(); - } catch (Exception) { - parsedInput = new Dictionary(); - } - blocks.Add(new ToolUseBlockParam { ID = tc.Id, Name = tc.Name, Input = parsedInput }); - } - } - if (blocks.Count > 0) { - result.Add(new MessageParam { Role = Role.Assistant, Content = blocks }); - } - break; - } - case LlmRole.Tool: - pendingToolResults.Add(new ToolResultBlockParam(m.ToolCallId ?? string.Empty) { - Content = m.Text ?? string.Empty, - IsError = m.IsToolError - }); - break; - } - } - FlushToolResults(); - - // Anthropic requires strictly alternating user/assistant starting with user. - return EnsureAlternatingRoles(result); - } - - private static List ConvertToAnthropicToolSpecs(IReadOnlyList specs, bool enablePromptCaching) { - var tools = new List(); - for (int index = 0; index < specs.Count; index++) { - var spec = specs[index]; - try { - var schemaDoc = System.Text.Json.JsonDocument.Parse(spec.ParametersJson); - var root = schemaDoc.RootElement; - - var properties = new Dictionary(); - var required = new List(); - - if (root.TryGetProperty("properties", out var propsEl) && propsEl.ValueKind == JsonValueKind.Object) { - foreach (var prop in propsEl.EnumerateObject()) { - properties[prop.Name] = prop.Value.Clone(); - } - } - - if (root.TryGetProperty("required", out var reqEl) && reqEl.ValueKind == JsonValueKind.Array) { - foreach (var item in reqEl.EnumerateArray()) { - required.Add(item.GetString()); - } - } - - var inputSchema = new InputSchema { - Type = System.Text.Json.JsonDocument.Parse("\"object\"").RootElement, - Properties = properties, - Required = required - }; - - tools.Add(new ToolUnion(new Tool { - Name = spec.Name, - Description = spec.Description, - InputSchema = inputSchema, - CacheControl = enablePromptCaching && index == specs.Count - 1 ? CreateCacheControl() : null, - }, null)); - } catch (Exception) { - } - } - return tools; - } - } - - - #region Serialization - /// /// Serialize Anthropic message list to portable format for snapshot persistence. /// @@ -1231,8 +975,6 @@ public static (string systemPrompt, List messages) DeserializeProv return (systemPrompt, messages); } - #endregion - #region Embeddings public Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs index cce300ee..0757f08f 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs @@ -165,6 +165,45 @@ public static string DetermineOpenAiOutcome(bool promptCachingEnabled, bool cach : PromptCachingOutcomes.Miss; } + /// Logs one Anthropic prompt-caching observation (moved from AnthropicService instance method). + public static void LogAnthropicPromptCachingObservation( + ILogger logger, + LLMChannel channel, + string providerName, + string modelName, + bool promptCachingEnabled, + string toolDefinitionHash, + string stablePrefixHash, + bool cacheBreakpointInserted, + long? cacheCreationInputTokens, + long? cacheReadInputTokens, + object usage) { + var usageJson = usage == null ? null : JsonConvert.SerializeObject(usage); + var observationKey = $"{providerName}:{modelName}:{stablePrefixHash}:{toolDefinitionHash}"; + var outcome = DetermineAnthropicOutcome( + promptCachingEnabled, + cacheBreakpointInserted, + observationKey, + cacheCreationInputTokens, + cacheReadInputTokens, + out var missReason); + + LogObservation(logger, new PromptCachingObservation { + Provider = providerName, + ChannelId = channel.Id, + Model = modelName, + PromptCachingEnabled = promptCachingEnabled, + StablePrefixHash = stablePrefixHash, + ToolDefinitionHash = toolDefinitionHash, + CacheOutcome = outcome, + MissReason = missReason, + CacheBreakpointInserted = cacheBreakpointInserted, + CacheCreationInputTokens = cacheCreationInputTokens, + CacheReadInputTokens = cacheReadInputTokens, + ProviderUsageJson = usageJson, + }); + } + public static string DetermineAnthropicOutcome(bool promptCachingEnabled, bool cacheBreakpointInserted, string observationKey, long? cacheCreationInputTokens, long? cacheReadInputTokens, out string missReason) { missReason = null; if (!promptCachingEnabled) { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs new file mode 100644 index 00000000..b558290d --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs @@ -0,0 +1,269 @@ +#pragma warning disable OPENAI001 + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Anthropic; +using Anthropic.Models.Messages; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using OpenAI.Chat; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + public sealed class AnthropicMessagesTransport : ILlmTransport { + private readonly ILogger _logger; + private readonly AnthropicClient _client; + private readonly string _systemPrompt; + private readonly string _modelName; + private readonly LLMChannel _channel; + private readonly bool _nativeTools; + private readonly bool _promptCachingEnabled; + private List? _preparedHistory; + private int _convertedCount; + private bool _cacheBreakpointInserted; + + public AnthropicMessagesTransport(ILogger logger, AnthropicClient client, string systemPrompt, + string modelName, LLMChannel channel, bool nativeTools, bool promptCachingEnabled) { + _logger = logger; + _client = client; + _systemPrompt = systemPrompt; + _modelName = modelName; + _channel = channel; + _nativeTools = nativeTools; + _promptCachingEnabled = promptCachingEnabled; + } + + private LlmTurnRequest? _lastRequest; + + public bool SupportsNativeTools => _nativeTools; + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + _lastRequest = request; + // Legacy wire semantics: prompt-cache preparation runs once per run (first turn); + // later turns append raw deltas so user messages stay string-typed on the wire. + if (_preparedHistory == null) { + _preparedHistory = AnthropicService.PrepareMessagesForPromptCaching(ToProviderHistory(request.History), _promptCachingEnabled, excludeDynamicTail: true, out var cacheBreakpointInserted); + _cacheBreakpointInserted = cacheBreakpointInserted; + } else if (request.History.Count > _convertedCount) { + _preparedHistory.AddRange(ToProviderHistory(request.History.Skip(_convertedCount))); + } + _convertedCount = request.History.Count; + var providerHistory = _preparedHistory; + try { System.IO.File.AppendAllText("C:/temp/anthropic_dbg.log", "caching=" + _promptCachingEnabled + " " + System.Text.Json.JsonSerializer.Serialize(new { P = providerHistory }) + Environment.NewLine + "###" + Environment.NewLine); } catch { } + + var nativeToolSpecs = request.Tools; + var rawHistory = providerHistory.ToList(); + var (toolDefinitionHash, stablePrefixHash) = AnthropicService.BuildPromptCachingContext( + _nativeTools ? "anthropic-native" : "anthropic-xml", + _systemPrompt, + AnthropicService.SerializeProviderHistory(_systemPrompt, rawHistory)); + var parameters = new MessageCreateParams { + Model = _modelName, + MaxTokens = 8192, + System = AnthropicService.BuildSystemPrompt(_systemPrompt, _promptCachingEnabled), + Messages = providerHistory, + Tools = _nativeTools && nativeToolSpecs is { Count: > 0 } + ? ConvertToAnthropicToolSpecs(nativeToolSpecs, _promptCachingEnabled) + : null, + }; + + + long? cacheCreationInputTokens = null; + long? cacheReadInputTokens = null; + object usageObservation = null; + + var turnText = new StringBuilder(); + var toolUseBlocks = new List<(string id, string name, string inputJson)>(); + var currentToolInputBuilder = new StringBuilder(); + string currentToolId = null; + string currentToolName = null; + + await foreach (var rawEvent in _client.Messages.CreateStreaming(parameters, cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + + if (rawEvent.TryPickStart(out var messageStartEvent) && messageStartEvent.Message?.Usage != null) { + usageObservation = new { + messageStartEvent.Message.Usage.InputTokens, + messageStartEvent.Message.Usage.OutputTokens, + messageStartEvent.Message.Usage.CacheCreationInputTokens, + messageStartEvent.Message.Usage.CacheReadInputTokens, + RawData = messageStartEvent.Message.Usage.RawData, + }; + } + + if (rawEvent.TryPickDelta(out var messageDeltaEvent) && messageDeltaEvent.Usage != null) { + cacheCreationInputTokens = messageDeltaEvent.Usage.CacheCreationInputTokens ?? cacheCreationInputTokens; + cacheReadInputTokens = messageDeltaEvent.Usage.CacheReadInputTokens ?? cacheReadInputTokens; + usageObservation = new { + messageDeltaEvent.Usage.InputTokens, + messageDeltaEvent.Usage.OutputTokens, + messageDeltaEvent.Usage.CacheCreationInputTokens, + messageDeltaEvent.Usage.CacheReadInputTokens, + RawData = messageDeltaEvent.Usage.RawData, + }; + } + + if (rawEvent.TryPickContentBlockStart(out var startEvent)) { + if (startEvent.ContentBlock.TryPickToolUse(out var toolUseStart)) { + currentToolId = toolUseStart.ID; + currentToolName = toolUseStart.Name; + currentToolInputBuilder.Clear(); + } + } else if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { + if (deltaEvent.Delta.TryPickText(out var textDelta)) { + turnText.Append(textDelta.Text); + yield return new LlmStreamEvent.TextDelta(textDelta.Text); + } else if (deltaEvent.Delta.TryPickInputJson(out var inputJsonDelta)) { + currentToolInputBuilder.Append(inputJsonDelta.PartialJson); + } + } else if (rawEvent.TryPickContentBlockStop(out _)) { + if (currentToolId != null) { + toolUseBlocks.Add((currentToolId, currentToolName, currentToolInputBuilder.ToString())); + currentToolId = null; + currentToolName = null; + } + } + } + + PromptCachingHelper.LogAnthropicPromptCachingObservation( + _logger, _channel, "Anthropic", _modelName, _promptCachingEnabled, + toolDefinitionHash, stablePrefixHash, _cacheBreakpointInserted, + cacheCreationInputTokens, cacheReadInputTokens, usageObservation); + + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = turnText.ToString().Trim(), + ToolCalls = toolUseBlocks.Select(b => new LlmToolCall { + Id = b.id, + Name = b.name, + ArgumentsJson = string.IsNullOrWhiteSpace(b.inputJson) ? "{}" : b.inputJson + }).ToList(), + StreamedAny = turnText.Length > 0, + UsageObservation = usageObservation + }); + } + + private List ToProviderHistory(IEnumerable messages) { + var request = _lastRequest!; + + var result = new List(); + var pendingToolResults = new List(); + + void FlushToolResults() { + if (pendingToolResults.Count > 0) { + result.Add(new MessageParam { Role = Role.User, Content = pendingToolResults.ToList() }); + pendingToolResults.Clear(); + } + } + + foreach (var m in request.History) { + switch (m.Role) { + case LlmRole.User: { + FlushToolResults(); + if (m.ImagePng != null && request.Config.SupportsVision) { + var blocks = new List(); + if (!string.IsNullOrEmpty(m.Text)) { + blocks.Add(new TextBlockParam(m.Text)); + } + blocks.Add(new ImageBlockParam(new Base64ImageSource { + Data = Convert.ToBase64String(m.ImagePng), + MediaType = MediaType.ImagePng + })); + result.Add(new MessageParam { Role = Role.User, Content = blocks }); + } else { + // Plain-text user messages use string content (wire-compatible with legacy). + result.Add(new MessageParam { Role = Role.User, Content = m.Text ?? string.Empty }); + } + break; + } + case LlmRole.Assistant: { + FlushToolResults(); + var blocks = new List(); + if (!string.IsNullOrEmpty(m.Text)) { + blocks.Add(new TextBlockParam(m.Text)); + } + if (m.ToolCalls is { Count: > 0 }) { + foreach (var tc in m.ToolCalls) { + Dictionary parsedInput; + try { + parsedInput = string.IsNullOrWhiteSpace(tc.ArgumentsJson) + ? new Dictionary() + : System.Text.Json.JsonSerializer.Deserialize>(tc.ArgumentsJson) + ?? new Dictionary(); + } catch (Exception) { + parsedInput = new Dictionary(); + } + blocks.Add(new ToolUseBlockParam { ID = tc.Id, Name = tc.Name, Input = parsedInput }); + } + } + if (blocks.Count > 0) { + result.Add(new MessageParam { Role = Role.Assistant, Content = blocks }); + } + break; + } + case LlmRole.Tool: + pendingToolResults.Add(new ToolResultBlockParam(m.ToolCallId ?? string.Empty) { + Content = m.Text ?? string.Empty, + IsError = m.IsToolError + }); + break; + } + } + FlushToolResults(); + + // Anthropic requires strictly alternating user/assistant starting with user. + return AnthropicService.EnsureAlternatingRoles(result); + } + + private static List ConvertToAnthropicToolSpecs(IReadOnlyList specs, bool enablePromptCaching) { + var tools = new List(); + for (int index = 0; index < specs.Count; index++) { + var spec = specs[index]; + try { + var schemaDoc = System.Text.Json.JsonDocument.Parse(spec.ParametersJson); + var root = schemaDoc.RootElement; + + var properties = new Dictionary(); + var required = new List(); + + if (root.TryGetProperty("properties", out var propsEl) && propsEl.ValueKind == JsonValueKind.Object) { + foreach (var prop in propsEl.EnumerateObject()) { + properties[prop.Name] = prop.Value.Clone(); + } + } + + if (root.TryGetProperty("required", out var reqEl) && reqEl.ValueKind == JsonValueKind.Array) { + foreach (var item in reqEl.EnumerateArray()) { + required.Add(item.GetString()); + } + } + + var inputSchema = new InputSchema { + Type = System.Text.Json.JsonDocument.Parse("\"object\"").RootElement, + Properties = properties, + Required = required + }; + + tools.Add(new ToolUnion(new Tool { + Name = spec.Name, + Description = spec.Description, + InputSchema = inputSchema, + CacheControl = enablePromptCaching && index == specs.Count - 1 ? AnthropicService.CreateCacheControl() : null, + }, null)); + } catch (Exception) { + } + } + return tools; + } + } +} From dc5c0ebe4996c237ee379b7ab46dcbcf22197e3b Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 09:28:19 +0800 Subject: [PATCH 14/30] refactor(llm): extract OpenAIResponsesTransport to Transports/ --- .../Service/AI/LLM/OpenAIResponsesService.cs | 255 +----------------- .../Service/AI/LLM/PromptCachingHelper.cs | 46 ++++ .../Transports/OpenAIResponsesTransport.cs | 216 +++++++++++++++ 3 files changed, 271 insertions(+), 246 deletions(-) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs index 313064a6..3ae6374c 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs @@ -42,7 +42,7 @@ public class OpenAIResponsesService : IService, ILLMService { /// /// Mutable accumulator for streaming tool call argument deltas. /// - private class ResponsesToolCallAccumulator { + internal class ResponsesToolCallAccumulator { public string CallId { get; set; } public string Name { get; set; } public StringBuilder Arguments { get; } = new StringBuilder(); @@ -114,7 +114,7 @@ private static List GetStablePrefixInputItems(List i return inputItems.Take(inputItems.Count - 1).ToList(); } - private static (string toolDefinitionHash, string stablePrefixHash, string promptCacheKey) BuildPromptCachingContext( + internal static (string toolDefinitionHash, string stablePrefixHash, string promptCacheKey) BuildPromptCachingContext( string providerName, string modelName, string mode, @@ -131,49 +131,6 @@ private static (string toolDefinitionHash, string stablePrefixHash, string promp return (toolDefinitionHash, stablePrefixHash, promptCacheKey); } - private void LogPromptCachingObservation( - string providerName, - LLMChannel channel, - string modelName, - bool promptCachingEnabled, - string toolDefinitionHash, - string stablePrefixHash, - string promptCacheKey, - ResponseTokenUsage usage, - bool cacheKeyAttached) { - var cachedTokenCount = usage?.InputTokenDetails?.CachedTokenCount; - var usageJson = usage == null - ? null - : JsonConvert.SerializeObject(new { - usage.InputTokenCount, - usage.OutputTokenCount, - usage.TotalTokenCount, - CachedTokenCount = usage.InputTokenDetails?.CachedTokenCount, - ReasoningTokenCount = usage.OutputTokenDetails?.ReasoningTokenCount, - }); - var outcome = PromptCachingHelper.DetermineOpenAiOutcome( - promptCachingEnabled, - cacheKeyAttached, - promptCacheKey, - cachedTokenCount, - out var missReason); - - PromptCachingHelper.LogObservation(_logger, new PromptCachingObservation { - Provider = providerName, - ChannelId = channel.Id, - Model = modelName, - PromptCachingEnabled = promptCachingEnabled, - StablePrefixHash = stablePrefixHash, - ToolDefinitionHash = toolDefinitionHash, - CacheOutcome = outcome, - MissReason = missReason, - PromptCacheKey = promptCacheKey, - PromptCacheRetention = PromptCachingHelper.OpenAiDefaultPromptCacheRetention, - CacheKeyAttached = cacheKeyAttached, - CachedTokenCount = cachedTokenCount, - ProviderUsageJson = usageJson, - }); - } // ======================================================================== // ILLMService Implementation @@ -216,7 +173,7 @@ public async IAsyncEnumerable ExecAsync( var supportsVision = await CheckVisionSupport(modelName, channel.Id); var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); - var transport = new ResponsesTransport(this, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); + var transport = new Transports.ResponsesTransport(_httpClientFactory, _logger, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); var botName = await GetBotNameAsync(); var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); @@ -272,7 +229,7 @@ public async IAsyncEnumerable ExecWithHistoryAsync( var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); - var transport = new ResponsesTransport(this, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); + var transport = new Transports.ResponsesTransport(_httpClientFactory, _logger, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); var botName = await GetBotNameAsync(); var projected = LlmHistoryProjector.Project(history, supportsVision, _logger); @@ -359,7 +316,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( } var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); - var transport = new ResponsesTransport(this, endpoint, resolvedApiKey, binding, channel, false, promptCachingEnabled); + var transport = new Transports.ResponsesTransport(_httpClientFactory, _logger, endpoint, resolvedApiKey, binding, channel, false, promptCachingEnabled); var meta = new LlmToolLoopMeta { ChatId = snapshot.ChatId, @@ -400,200 +357,6 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( /// Native transport for the OpenAI Responses API: converts the normalized history to /// input items per turn and streams one assistant turn (text + function calls). /// - private sealed class ResponsesTransport : ILlmTransport { - private readonly OpenAIResponsesService _svc; - private readonly string _endpoint; - private readonly string _apiKey; - private readonly LLMApiBinding? _binding; - private readonly LLMChannel _channel; - private readonly bool _supportsVision; - private readonly bool _promptCachingEnabled; - private ResponsesClient? _client; - - public ResponsesTransport(OpenAIResponsesService svc, string endpoint, string apiKey, - LLMApiBinding? binding, LLMChannel channel, bool supportsVision, bool promptCachingEnabled) { - _svc = svc; - _endpoint = endpoint; - _apiKey = apiKey; - _binding = binding; - _channel = channel; - _supportsVision = supportsVision; - _promptCachingEnabled = promptCachingEnabled; - } - - public bool SupportsNativeTools => true; - - public async IAsyncEnumerable StreamTurnAsync( - LlmTurnRequest request, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (_client == null) { - // ponytail: HttpClient outlives this scope (transport persists per run); factory-managed. - var httpClient = _svc._httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, _channel, _binding, $"tsb-{_channel?.Id}"); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(_endpoint), - Transport = new HttpClientPipelineTransport(httpClient), - }; - _client = new ResponsesClient(new ApiKeyCredential(_apiKey), clientOptions); - } - - var inputItems = ToInputItems(request); - var instructions = request.SystemPrompt; - - var shouldObservePromptCaching = _channel.Provider == LLMProvider.ResponsesAPI; - var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIResponsesService.BuildPromptCachingContext( - "OpenAIResponses", - request.Config.ModelName, - "responses", - instructions, - inputItems, - excludeDynamicTail: true); - - var options = new CreateResponseOptions { - Model = request.Config.ModelName, - Instructions = instructions, - StreamingEnabled = true, - }; - var cacheKeyAttached = false; - if (promptCachingEnabled) { - PromptCachingHelper.ApplyOpenAiPromptCaching(options, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); - cacheKeyAttached = true; - } - if (request.Tools is { Count: > 0 }) { - foreach (var spec in request.Tools) { - options.Tools.Add(new FunctionTool( - spec.Name, - BinaryData.FromString(spec.ParametersJson), - spec.StrictSchema) { - FunctionDescription = spec.Description - }); - } - } - foreach (var item in inputItems) { - options.InputItems.Add(item); - } - - var textBuilder = new StringBuilder(); - var reasoningBuilder = new StringBuilder(); - var uiBuilder = new StringBuilder(); - var toolCallAccums = new Dictionary(); - ResponseResult completedResult = null; - var lastUiLength = 0; - var streamedAny = false; - - await foreach (var update in _client.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - OpenAIResponsesService.ProcessStreamingUpdate(update, textBuilder, uiBuilder, reasoningBuilder, toolCallAccums, ref completedResult); - - if (uiBuilder.Length > lastUiLength) { - var delta = uiBuilder.ToString(lastUiLength, uiBuilder.Length - lastUiLength); - lastUiLength = uiBuilder.Length; - streamedAny = true; - yield return new LlmStreamEvent.TextDelta(delta); - } - } - - if (shouldObservePromptCaching) { - _svc.LogPromptCachingObservation( - "OpenAIResponses", - _channel, - request.Config.ModelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - promptCacheKey, - completedResult?.Usage, - cacheKeyAttached); - } - - var responseText = textBuilder.ToString().Trim(); - var reasoningContent = reasoningBuilder.ToString().Trim(); - var toolCalls = new List(); - - if (completedResult?.OutputItems != null) { - foreach (var outputItem in completedResult.OutputItems) { - if (outputItem is FunctionCallResponseItem fcItem - && !string.IsNullOrWhiteSpace(fcItem.CallId) - && !string.IsNullOrWhiteSpace(fcItem.FunctionName)) { - toolCalls.Add(new LlmToolCall { - Id = OpenAIService.NormalizeToolCallId(fcItem.CallId), - Name = OpenAIService.NormalizeToolCallName(fcItem.FunctionName), - ArgumentsJson = OpenAIService.NormalizeToolCallArguments(fcItem.FunctionArguments?.ToString() ?? "{}") - }); - } - } - } - - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = responseText, - Reasoning = reasoningContent, - ToolCalls = toolCalls, - StreamedAny = streamedAny - }); - } - - private List ToInputItems(LlmTurnRequest request) { - var inputItems = new List(); - foreach (var m in request.History) { - switch (m.Role) { - case LlmRole.User: { - var images = m.ImagePng != null && request.Config.SupportsVision - ? new List { m.ImagePng } - : null; - _svc.AddResponseItemFromAccumulated(inputItems, 1, m.Text ?? string.Empty, images); - break; - } - case LlmRole.Assistant: - if (!string.IsNullOrWhiteSpace(m.Text)) { - inputItems.Add(ResponseItem.CreateAssistantMessageItem(m.Text)); - } - if (m.ToolCalls is { Count: > 0 }) { - foreach (var tc in m.ToolCalls) { - inputItems.Add(ResponseItem.CreateFunctionCallItem( - tc.Id, tc.Name, BinaryData.FromString(tc.ArgumentsJson))); - } - } - break; - case LlmRole.Tool: - inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(m.ToolCallId ?? string.Empty, m.Text ?? string.Empty)); - break; - } - } - return inputItems; - } - } - - private void AddResponseItemFromAccumulated( - List inputItems, long fromUserId, string content, List images) { - if (string.IsNullOrWhiteSpace(content) && (images == null || images.Count == 0)) return; - if (!string.IsNullOrWhiteSpace(content)) { - content = System.Text.RegularExpressions.Regex.Replace(content.Trim(), @"\n{3,}", "\n\n"); - } - - if (fromUserId == Env.BotId) { - // Assistant message - if (!string.IsNullOrWhiteSpace(content)) { - inputItems.Add(ResponseItem.CreateAssistantMessageItem(content)); - } - } else { - // User message (possibly with images) - if (images != null && images.Count > 0) { - var parts = new List(); - if (!string.IsNullOrWhiteSpace(content)) { - parts.Add(ResponseContentPart.CreateInputTextPart(content.Trim())); - } - foreach (var imageBytes in images) { - parts.Add(ResponseContentPart.CreateInputImagePart( - BinaryData.FromBytes(imageBytes), null)); - } - inputItems.Add(ResponseItem.CreateUserMessageItem((IEnumerable)parts)); - } else { - inputItems.Add(ResponseItem.CreateUserMessageItem(content.Trim())); - } - } - } // ======================================================================== // Helper: Vision support check @@ -658,7 +421,7 @@ private byte[] TryLoadMessagePhoto(long chatId, long messageId) { // ======================================================================== - private static void ProcessStreamingUpdate( + internal static void ProcessStreamingUpdate( StreamingResponseUpdate update, StringBuilder textBuilder, StringBuilder contentBuilder, @@ -710,7 +473,7 @@ private static void ProcessStreamingUpdate( } - private static void ProcessResumeStreamingUpdate( + internal static void ProcessResumeStreamingUpdate( StreamingResponseUpdate update, StringBuilder textBuilder, StringBuilder fullContentBuilder, @@ -1137,7 +900,7 @@ private async Task> BuildResponseInputItemsAsync( } if (previous != null && !IsSameSender(previous, message)) { - AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); + Transports.ResponsesTransport.AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); str.Clear(); pendingImages.Clear(); } @@ -1182,7 +945,7 @@ private async Task> BuildResponseInputItemsAsync( } if (previous != null && str.Length > 0) { - AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); + Transports.ResponsesTransport.AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); } return inputItems; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs index 0757f08f..f338e2fe 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs @@ -204,6 +204,52 @@ public static void LogAnthropicPromptCachingObservation( }); } + /// Logs one OpenAI Responses prompt-caching observation (moved from OpenAIResponsesService instance method). + public static void LogResponsesPromptCachingObservation( + ILogger logger, + string providerName, + LLMChannel channel, + string modelName, + bool promptCachingEnabled, + string toolDefinitionHash, + string stablePrefixHash, + string promptCacheKey, + ResponseTokenUsage usage, + bool cacheKeyAttached) { + var cachedTokenCount = usage?.InputTokenDetails?.CachedTokenCount; + var usageJson = usage == null + ? null + : JsonConvert.SerializeObject(new { + usage.InputTokenCount, + usage.OutputTokenCount, + usage.TotalTokenCount, + CachedTokenCount = usage.InputTokenDetails?.CachedTokenCount, + ReasoningTokenCount = usage.OutputTokenDetails?.ReasoningTokenCount, + }); + var outcome = DetermineOpenAiOutcome( + promptCachingEnabled, + cacheKeyAttached, + promptCacheKey, + cachedTokenCount, + out var missReason); + + LogObservation(logger, new PromptCachingObservation { + Provider = providerName, + ChannelId = channel.Id, + Model = modelName, + PromptCachingEnabled = promptCachingEnabled, + StablePrefixHash = stablePrefixHash, + ToolDefinitionHash = toolDefinitionHash, + CacheOutcome = outcome, + MissReason = missReason, + PromptCacheKey = promptCacheKey, + PromptCacheRetention = OpenAiDefaultPromptCacheRetention, + CacheKeyAttached = cacheKeyAttached, + CachedTokenCount = cachedTokenCount, + ProviderUsageJson = usageJson, + }); + } + public static string DetermineAnthropicOutcome(bool promptCachingEnabled, bool cacheBreakpointInserted, string observationKey, long? cacheCreationInputTokens, long? cacheReadInputTokens, out string missReason) { missReason = null; if (!promptCachingEnabled) { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs new file mode 100644 index 00000000..2a4326a3 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs @@ -0,0 +1,216 @@ +#pragma warning disable OPENAI001 + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.ClientModel; +using System.ClientModel.Primitives; +using Microsoft.Extensions.Http; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using OpenAI; +using OpenAI.Responses; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + public sealed class ResponsesTransport : ILlmTransport { + private readonly IHttpClientFactory _httpClientFactory; + private readonly ILogger _logger; + private readonly string _endpoint; + private readonly string _apiKey; + private readonly LLMApiBinding? _binding; + private readonly LLMChannel _channel; + private readonly bool _supportsVision; + private readonly bool _promptCachingEnabled; + private ResponsesClient? _client; + + public ResponsesTransport(IHttpClientFactory httpClientFactory, ILogger logger, string endpoint, string apiKey, + LLMApiBinding? binding, LLMChannel channel, bool supportsVision, bool promptCachingEnabled) { + _httpClientFactory = httpClientFactory; + _logger = logger; + _endpoint = endpoint; + _apiKey = apiKey; + _binding = binding; + _channel = channel; + _supportsVision = supportsVision; + _promptCachingEnabled = promptCachingEnabled; + } + + public bool SupportsNativeTools => true; + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (_client == null) { + // ponytail: HttpClient outlives this scope (transport persists per run); factory-managed. + var httpClient = _httpClientFactory.CreateClient(); + OpencodeSessionHeaders.Apply(httpClient, _channel, _binding, $"tsb-{_channel?.Id}"); + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(_endpoint), + Transport = new HttpClientPipelineTransport(httpClient), + }; + _client = new ResponsesClient(new ApiKeyCredential(_apiKey), clientOptions); + } + + var inputItems = ToInputItems(request); + var instructions = request.SystemPrompt; + + var shouldObservePromptCaching = _channel.Provider == LLMProvider.ResponsesAPI; + var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; + var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIResponsesService.BuildPromptCachingContext( + "OpenAIResponses", + request.Config.ModelName, + "responses", + instructions, + inputItems, + excludeDynamicTail: true); + + var options = new CreateResponseOptions { + Model = request.Config.ModelName, + Instructions = instructions, + StreamingEnabled = true, + }; + var cacheKeyAttached = false; + if (promptCachingEnabled) { + PromptCachingHelper.ApplyOpenAiPromptCaching(options, promptCacheKey, PromptCachingHelper.OpenAiDefaultPromptCacheRetention); + cacheKeyAttached = true; + } + if (request.Tools is { Count: > 0 }) { + foreach (var spec in request.Tools) { + options.Tools.Add(new FunctionTool( + spec.Name, + BinaryData.FromString(spec.ParametersJson), + spec.StrictSchema) { + FunctionDescription = spec.Description + }); + } + } + foreach (var item in inputItems) { + options.InputItems.Add(item); + } + + var textBuilder = new StringBuilder(); + var reasoningBuilder = new StringBuilder(); + var uiBuilder = new StringBuilder(); + var toolCallAccums = new Dictionary(); + ResponseResult completedResult = null; + var lastUiLength = 0; + var streamedAny = false; + + await foreach (var update in _client.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + + OpenAIResponsesService.ProcessStreamingUpdate(update, textBuilder, uiBuilder, reasoningBuilder, toolCallAccums, ref completedResult); + + if (uiBuilder.Length > lastUiLength) { + var delta = uiBuilder.ToString(lastUiLength, uiBuilder.Length - lastUiLength); + lastUiLength = uiBuilder.Length; + streamedAny = true; + yield return new LlmStreamEvent.TextDelta(delta); + } + } + + if (shouldObservePromptCaching) { + PromptCachingHelper.LogResponsesPromptCachingObservation( + _logger, "OpenAI Responses", _channel, request.Config.ModelName, _promptCachingEnabled, + toolDefinitionHash, + stablePrefixHash, + promptCacheKey, + completedResult?.Usage, + cacheKeyAttached); + } + + var responseText = textBuilder.ToString().Trim(); + var reasoningContent = reasoningBuilder.ToString().Trim(); + var toolCalls = new List(); + + if (completedResult?.OutputItems != null) { + foreach (var outputItem in completedResult.OutputItems) { + if (outputItem is FunctionCallResponseItem fcItem + && !string.IsNullOrWhiteSpace(fcItem.CallId) + && !string.IsNullOrWhiteSpace(fcItem.FunctionName)) { + toolCalls.Add(new LlmToolCall { + Id = OpenAIService.NormalizeToolCallId(fcItem.CallId), + Name = OpenAIService.NormalizeToolCallName(fcItem.FunctionName), + ArgumentsJson = OpenAIService.NormalizeToolCallArguments(fcItem.FunctionArguments?.ToString() ?? "{}") + }); + } + } + } + + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = responseText, + Reasoning = reasoningContent, + ToolCalls = toolCalls, + StreamedAny = streamedAny + }); + } + + private List ToInputItems(LlmTurnRequest request) { + var inputItems = new List(); + foreach (var m in request.History) { + switch (m.Role) { + case LlmRole.User: { + var images = m.ImagePng != null && request.Config.SupportsVision + ? new List { m.ImagePng } + : null; + AddResponseItemFromAccumulated(inputItems, 1, m.Text ?? string.Empty, images); + break; + } + case LlmRole.Assistant: + if (!string.IsNullOrWhiteSpace(m.Text)) { + inputItems.Add(ResponseItem.CreateAssistantMessageItem(m.Text)); + } + if (m.ToolCalls is { Count: > 0 }) { + foreach (var tc in m.ToolCalls) { + inputItems.Add(ResponseItem.CreateFunctionCallItem( + tc.Id, tc.Name, BinaryData.FromString(tc.ArgumentsJson))); + } + } + break; + case LlmRole.Tool: + inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(m.ToolCallId ?? string.Empty, m.Text ?? string.Empty)); + break; + } + } + return inputItems; + } + + internal static void AddResponseItemFromAccumulated( + List inputItems, long fromUserId, string content, List images) { + if (string.IsNullOrWhiteSpace(content) && (images == null || images.Count == 0)) return; + if (!string.IsNullOrWhiteSpace(content)) { + content = System.Text.RegularExpressions.Regex.Replace(content.Trim(), @"\n{3,}", "\n\n"); + } + + if (fromUserId == Env.BotId) { + // Assistant message + if (!string.IsNullOrWhiteSpace(content)) { + inputItems.Add(ResponseItem.CreateAssistantMessageItem(content)); + } + } else { + // User message (possibly with images) + if (images != null && images.Count > 0) { + var parts = new List(); + if (!string.IsNullOrWhiteSpace(content)) { + parts.Add(ResponseContentPart.CreateInputTextPart(content.Trim())); + } + foreach (var imageBytes in images) { + parts.Add(ResponseContentPart.CreateInputImagePart( + BinaryData.FromBytes(imageBytes), null)); + } + inputItems.Add(ResponseItem.CreateUserMessageItem((IEnumerable)parts)); + } else { + inputItems.Add(ResponseItem.CreateUserMessageItem(content.Trim())); + } + } + } +} +} From 6cbe2fb3d991f722dd62c60cd8cb6b7f2c526206 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 09:32:31 +0800 Subject: [PATCH 15/30] refactor(llm): extract GeminiTransport and OllamaTransport to Transports/ --- .../Service/AI/LLM/GeminiService.cs | 74 +---------------- .../Service/AI/LLM/OllamaService.cs | 61 +------------- .../AI/LLM/Transports/GeminiTransport.cs | 81 +++++++++++++++++++ .../AI/LLM/Transports/OllamaTransport.cs | 69 ++++++++++++++++ 4 files changed, 154 insertions(+), 131 deletions(-) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs index be88838f..c96ac187 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs @@ -422,7 +422,7 @@ await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, can var botName = await GetBotNameAsync(); var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - var transport = new GeminiTransport(this, model, supportsVision); + var transport = new Transports.GeminiTransport(model, supportsVision); var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); var meta = new LlmToolLoopMeta { @@ -480,7 +480,7 @@ await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, can var botName = await GetBotNameAsync(); var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - var transport = new GeminiTransport(this, model, supportsVision); + var transport = new Transports.GeminiTransport(model, supportsVision); var projected = LlmHistoryProjector.Project(history, supportsVision, _logger); var meta = new LlmToolLoopMeta { @@ -513,76 +513,6 @@ await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, can } } - /// - /// Text-protocol transport for Gemini (GenerativeAI session). Seeds the session from the - /// normalized history and streams the newest user message each turn. - /// - private sealed class GeminiTransport : ILlmTransport { - private readonly GeminiService _svc; - private readonly GenerativeModel _model; - private readonly bool _supportsVision; - private ChatSession? _chatSession; - - public GeminiTransport(GeminiService svc, GenerativeModel model, bool supportsVision) { - _svc = svc; - _model = model; - _supportsVision = supportsVision; - } - - public bool SupportsNativeTools => false; - - public async IAsyncEnumerable StreamTurnAsync( - LlmTurnRequest request, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (_chatSession == null) { - var seedHistory = ToContents(request.History.Take(request.History.Count - 1).ToList()); - _chatSession = _model.StartChat(history: seedHistory); - } - - var lastUser = request.History.LastOrDefault(m => m.Role == LlmRole.User); - var prompt = lastUser?.Text ?? string.Empty; - - var turnText = new StringBuilder(); - await foreach (var chunk in _chatSession.StreamContentAsync(prompt).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - turnText.Append(chunk.Text); - yield return new LlmStreamEvent.TextDelta(chunk.Text); - } - - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = turnText.ToString().Trim(), - StreamedAny = turnText.Length > 0 - }); - } - - private List ToContents(List messages) { - var contents = new List(); - foreach (var m in messages) { - if (m.Role == LlmRole.User) { - var role = Roles.User; - if (m.ImagePng != null && _supportsVision) { - var parts = new List(); - if (!string.IsNullOrWhiteSpace(m.Text)) { - parts.Add(new Part { Text = m.Text.Trim() }); - } - parts.Add(new Part { - InlineData = new GenerativeAI.Types.Blob { - MimeType = m.ImageMediaType ?? "image/png", - Data = Convert.ToBase64String(m.ImagePng) - } - }); - contents.Add(new Content { Parts = parts, Role = role }); - } else { - contents.Add(new Content(m.Text?.Trim() ?? string.Empty, role)); - } - } else if (m.Role == LlmRole.Assistant) { - contents.Add(new Content(m.Text?.Trim() ?? string.Empty, Roles.Model)); - } - } - return contents; - } - } - public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { if (channel == null || string.IsNullOrWhiteSpace(channel.ApiKey)) { _logger.LogError("{ServiceName}: Channel or ApiKey is not configured", ServiceName); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs index cdb8049c..e485b267 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs @@ -164,7 +164,7 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long var botName = await GetBotNameAsync(); var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - var transport = new OllamaTransport(this, ollama, systemPrompt); + var transport = new Transports.OllamaTransport(_logger, ollama, systemPrompt); var history = LlmHistoryProjector.Project(rows, supportsVision: false, _logger); var meta = new LlmToolLoopMeta { @@ -232,7 +232,7 @@ public async IAsyncEnumerable ExecWithHistoryAsync( var botName = await GetBotNameAsync(); var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - var transport = new OllamaTransport(this, ollama, systemPrompt); + var transport = new Transports.OllamaTransport(_logger, ollama, systemPrompt); var projected = LlmHistoryProjector.Project(history, supportsVision: false, _logger); var meta = new LlmToolLoopMeta { @@ -266,63 +266,6 @@ public async IAsyncEnumerable ExecWithHistoryAsync( } } - /// - /// Text-protocol transport for Ollama (OllamaSharp session). Seeds the session from the - /// normalized history and streams the newest user message each turn. - /// - private sealed class OllamaTransport : ILlmTransport { - private readonly OllamaService _svc; - private readonly OllamaApiClient _ollama; - private readonly string _systemPrompt; - private OllamaSharp.Chat? _chat; - - public OllamaTransport(OllamaService svc, OllamaApiClient ollama, string systemPrompt) { - _svc = svc; - _ollama = ollama; - _systemPrompt = systemPrompt; - } - - public bool SupportsNativeTools => false; - - public async IAsyncEnumerable StreamTurnAsync( - LlmTurnRequest request, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (_chat == null) { - _chat = new OllamaSharp.Chat(_ollama, _systemPrompt); - // Seed the session with all history except the newest user message. - for (int i = 0; i < request.History.Count - 1; i++) { - var m = request.History[i]; - if (m.Role == LlmRole.User) { - _chat.Messages.Add(new OllamaSharp.Models.Chat.Message { Role = ChatRole.User, Content = m.Text ?? string.Empty }); - } else if (m.Role == LlmRole.Assistant) { - _chat.Messages.Add(new OllamaSharp.Models.Chat.Message { Role = ChatRole.Assistant, Content = m.Text ?? string.Empty }); - } - } - } - - var lastUser = request.History.LastOrDefault(m => m.Role == LlmRole.User); - var prompt = lastUser?.Text ?? string.Empty; - - var turnText = new StringBuilder(); - var streamedAny = false; - await foreach (var token in _chat.SendAsync(prompt, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - turnText.Append(token); - streamedAny = true; - yield return new LlmStreamEvent.TextDelta(token); - } - - if (!streamedAny) { - _svc._logger.LogWarning("{ServiceName}: Ollama returned an empty stream during a tool cycle.", _svc.ServiceName); - } - - yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { - Text = turnText.ToString().Trim(), - StreamedAny = streamedAny - }); - } - } - public virtual async Task> GetAllModels(LLMChannel channel) { if (channel == null || string.IsNullOrWhiteSpace(channel.Gateway)) { return Enumerable.Empty(); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs new file mode 100644 index 00000000..39d350ea --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using GenerativeAI; +using GenerativeAI.Types; +using Microsoft.Extensions.Logging; +using OpenAI.Chat; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + public sealed class GeminiTransport : ILlmTransport { + private readonly GenerativeModel _model; + private readonly bool _supportsVision; + private ChatSession? _chatSession; + + public GeminiTransport(GenerativeModel model, bool supportsVision) { + + _model = model; + _supportsVision = supportsVision; + } + + public bool SupportsNativeTools => false; + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (_chatSession == null) { + var seedHistory = ToContents(request.History.Take(request.History.Count - 1).ToList()); + _chatSession = _model.StartChat(history: seedHistory); + } + + var lastUser = request.History.LastOrDefault(m => m.Role == LlmRole.User); + var prompt = lastUser?.Text ?? string.Empty; + + var turnText = new StringBuilder(); + await foreach (var chunk in _chatSession.StreamContentAsync(prompt).WithCancellation(cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + turnText.Append(chunk.Text); + yield return new LlmStreamEvent.TextDelta(chunk.Text); + } + + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = turnText.ToString().Trim(), + StreamedAny = turnText.Length > 0 + }); + } + + private List ToContents(List messages) { + var contents = new List(); + foreach (var m in messages) { + if (m.Role == LlmRole.User) { + var role = Roles.User; + if (m.ImagePng != null && _supportsVision) { + var parts = new List(); + if (!string.IsNullOrWhiteSpace(m.Text)) { + parts.Add(new Part { Text = m.Text.Trim() }); + } + parts.Add(new Part { + InlineData = new GenerativeAI.Types.Blob { + MimeType = m.ImageMediaType ?? "image/png", + Data = Convert.ToBase64String(m.ImagePng) + } + }); + contents.Add(new Content { Parts = parts, Role = role }); + } else { + contents.Add(new Content(m.Text?.Trim() ?? string.Empty, role)); + } + } else if (m.Role == LlmRole.Assistant) { + contents.Add(new Content(m.Text?.Trim() ?? string.Empty, Roles.Model)); + } + } + return contents; + } + }} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs new file mode 100644 index 00000000..e1c441c9 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using OllamaSharp; +using OllamaSharp.Models; +using OllamaSharp.Models.Chat; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + public sealed class OllamaTransport : ILlmTransport { + private readonly ILogger _logger; + private readonly OllamaApiClient _ollama; + private readonly string _systemPrompt; + private OllamaSharp.Chat? _chat; + + public OllamaTransport(ILogger logger, OllamaApiClient ollama, string systemPrompt) { + _logger = logger; + _ollama = ollama; + _systemPrompt = systemPrompt; + } + + public bool SupportsNativeTools => false; + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (_chat == null) { + _chat = new OllamaSharp.Chat(_ollama, _systemPrompt); + // Seed the session with all history except the newest user message. + for (int i = 0; i < request.History.Count - 1; i++) { + var m = request.History[i]; + if (m.Role == LlmRole.User) { + _chat.Messages.Add(new OllamaSharp.Models.Chat.Message { Role = ChatRole.User, Content = m.Text ?? string.Empty }); + } else if (m.Role == LlmRole.Assistant) { + _chat.Messages.Add(new OllamaSharp.Models.Chat.Message { Role = ChatRole.Assistant, Content = m.Text ?? string.Empty }); + } + } + } + + var lastUser = request.History.LastOrDefault(m => m.Role == LlmRole.User); + var prompt = lastUser?.Text ?? string.Empty; + + var turnText = new StringBuilder(); + var streamedAny = false; + await foreach (var token in _chat.SendAsync(prompt, cancellationToken).WithCancellation(cancellationToken)) { + if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); + turnText.Append(token); + streamedAny = true; + yield return new LlmStreamEvent.TextDelta(token); + } + + if (!streamedAny) { + _logger.LogWarning("OllamaService: Ollama returned an empty stream during a tool cycle."); + } + + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = turnText.ToString().Trim(), + StreamedAny = streamedAny + }); + } + }} From fd38e2319b5571ace6c0adabd35a3a17b5657a93 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 09:37:35 +0800 Subject: [PATCH 16/30] refactor(llm): replace ILLMService/ILLMFactory/LLMFactory with ILlmProvider + LlmProviderRegistry --- .../Service/AI/LLM/GeneralLLMServiceTests.cs | 26 +- .../Service/AI/LLM/LLMFactoryTests.cs | 266 ------------------ .../Interface/AI/LLM/IGeneralLLMService.cs | 10 +- .../Interface/AI/LLM/ILLMFactory.cs | 16 -- .../LLM/{ILLMService.cs => ILlmProvider.cs} | 2 +- .../Service/AI/LLM/AnthropicService.cs | 2 +- .../Service/AI/LLM/GeminiService.cs | 2 +- .../Service/AI/LLM/GeneralLLMService.cs | 24 +- .../{LLMFactory.cs => LlmProviderRegistry.cs} | 33 +-- .../Service/AI/LLM/ModelCapabilityService.cs | 20 +- .../Service/AI/LLM/OllamaService.cs | 4 +- .../Service/AI/LLM/OpenAIResponsesService.cs | 14 +- .../Service/AI/LLM/OpenAIService.cs | 2 +- .../Service/LlmServiceProxy.cs | 6 +- .../Manage/EditLLMConfHelperTest.cs | 10 +- .../Manage/EditLLMConfTest.cs | 8 +- .../Extension/ServiceCollectionExtension.cs | 2 +- .../Service/Manage/EditLLMConfHelper.cs | 8 +- 18 files changed, 84 insertions(+), 371 deletions(-) delete mode 100644 TelegramSearchBot.LLM.Test/Service/AI/LLM/LLMFactoryTests.cs delete mode 100644 TelegramSearchBot.LLM/Interface/AI/LLM/ILLMFactory.cs rename TelegramSearchBot.LLM/Interface/AI/LLM/{ILLMService.cs => ILlmProvider.cs} (99%) rename TelegramSearchBot.LLM/Service/AI/LLM/{LLMFactory.cs => LlmProviderRegistry.cs} (65%) diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs index 608d9d21..59e15133 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs @@ -25,7 +25,7 @@ public class GeneralLLMServiceTests { private readonly Mock _openAIServiceMock; private readonly Mock _ollamaServiceMock; private readonly Mock _geminiServiceMock; - private readonly Mock _factoryMock; + private readonly Mock _factoryMock; private readonly GeneralLLMService _service; public GeneralLLMServiceTests() { @@ -68,7 +68,7 @@ public GeneralLLMServiceTests() { var anthropicServiceMock = new Mock( _dbContext, anthropicLogger.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); - _factoryMock = new Mock(); + _factoryMock = new Mock((IServiceProvider)null); _service = new GeneralLLMService( _redisMock.Object, @@ -213,7 +213,7 @@ public async Task AnalyzeImageAsync_NoChannels_ReturnsErrorMessage() { [Fact] public async Task AnalyzeImageAsync_WithCustomPrompt_ForwardsPromptToProvider() { - var providerMock = new Mock(); + var providerMock = new Mock(); var channel = new LLMChannel { Name = "vision-channel", Gateway = "https://example.com", @@ -445,14 +445,14 @@ public async Task ExecOperationAsync_UsesResolvedRouteBinding_AndPassesBindingTo }); await _dbContext.SaveChangesAsync(); - var serviceMock = new Mock(); + var serviceMock = new Mock(); serviceMock.Setup(s => s.IsHealthyAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(true); serviceMock.Setup(s => s.ExecAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(EmptyStringStream()); - _factoryMock.Setup(f => f.GetLLMService(It.IsAny())).Returns(serviceMock.Object); + _factoryMock.Setup(f => f.GetProvider(It.IsAny())).Returns(serviceMock.Object); var results = new List(); var message = new TelegramSearchBot.Model.Data.Message { Content = "hi", GroupId = 123, MessageId = 1, FromUserId = 1 }; @@ -462,7 +462,7 @@ public async Task ExecOperationAsync_UsesResolvedRouteBinding_AndPassesBindingTo results.Add(r); } - _factoryMock.Verify(f => f.GetLLMService(It.Is(r => r.Channel.Id == 11 && r.Binding != null && r.Binding.Id == bDefault.Id)), Times.Once); + _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == 11 && r.Binding != null && r.Binding.Id == bDefault.Id)), Times.Once); serviceMock.Verify(s => s.ExecAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.Is(b => b.Id == bDefault.Id), It.IsAny(), It.IsAny()), @@ -484,14 +484,14 @@ public async Task ExecOperationAsync_ChannelPriorityDesc_Respected() { new ChannelWithModel { Id = 33, ModelName = "m", LLMChannelId = 13, ApiBindingId = bHigh.Id, ApiBinding = bHigh }); await _dbContext.SaveChangesAsync(); - var serviceMock = new Mock(); + var serviceMock = new Mock(); serviceMock.Setup(s => s.IsHealthyAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(true); serviceMock.Setup(s => s.ExecAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(EmptyStringStream()); - _factoryMock.Setup(f => f.GetLLMService(It.IsAny())).Returns(serviceMock.Object); + _factoryMock.Setup(f => f.GetProvider(It.IsAny())).Returns(serviceMock.Object); var message = new TelegramSearchBot.Model.Data.Message { Content = "hi", GroupId = 123, MessageId = 1, FromUserId = 1 }; await foreach (var _ in _service.ExecOperationAsync( @@ -500,8 +500,8 @@ public async Task ExecOperationAsync_ChannelPriorityDesc_Respected() { } // 高优先级渠道先被选中;成功后低优先级渠道不再被访问 - _factoryMock.Verify(f => f.GetLLMService(It.Is(r => r.Channel.Id == highChannel.Id && r.Binding!.Id == bHigh.Id)), Times.Once); - _factoryMock.Verify(f => f.GetLLMService(It.Is(r => r.Channel.Id == lowChannel.Id)), Times.Never); + _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == highChannel.Id && r.Binding!.Id == bHigh.Id)), Times.Once); + _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == lowChannel.Id)), Times.Never); } [Fact] @@ -521,12 +521,12 @@ public async Task ResumeFromSnapshotAsync_ResolvesRouteAndPassesBinding() { }); await _dbContext.SaveChangesAsync(); - var serviceMock = new Mock(); + var serviceMock = new Mock(); serviceMock.Setup(s => s.ResumeFromSnapshotAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(EmptyStringStream()); - _factoryMock.Setup(f => f.GetLLMService(It.IsAny())).Returns(serviceMock.Object); + _factoryMock.Setup(f => f.GetProvider(It.IsAny())).Returns(serviceMock.Object); var snapshot = new LlmContinuationSnapshot { SnapshotId = "s1", @@ -542,7 +542,7 @@ public async Task ResumeFromSnapshotAsync_ResolvesRouteAndPassesBinding() { results.Add(r); } - _factoryMock.Verify(f => f.GetLLMService(It.Is(r => r.Channel.Id == 14 && r.Binding != null && r.Binding.Id == bDefault.Id)), Times.Once); + _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == 14 && r.Binding != null && r.Binding.Id == bDefault.Id)), Times.Once); serviceMock.Verify(s => s.ResumeFromSnapshotAsync( It.IsAny(), It.IsAny(), It.Is(b => b.Id == bDefault.Id), It.IsAny(), It.IsAny()), diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/LLMFactoryTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LLMFactoryTests.cs deleted file mode 100644 index 2f258ea0..00000000 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/LLMFactoryTests.cs +++ /dev/null @@ -1,266 +0,0 @@ -#pragma warning disable CS8602 // Dereference of a possibly null reference -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Moq; -using StackExchange.Redis; -using TelegramSearchBot.Interface; -using TelegramSearchBot.Interface.AI.LLM; -using TelegramSearchBot.Model; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; -using TelegramSearchBot.Service.AI.LLM; -using Xunit; - -namespace TelegramSearchBot.Test.Service.AI.LLM { - public class LLMFactoryTests { - private readonly Mock _redisMock; - private readonly Mock> _loggerMock; - private readonly DataDbContext _dbContext; - private readonly Mock _openAIServiceMock; - private readonly Mock _ollamaServiceMock; - private readonly Mock _geminiServiceMock; - private readonly LLMFactory _factory; - - public LLMFactoryTests() { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) - .Options; - _dbContext = new DataDbContext(options); - - _redisMock = new Mock(); - _loggerMock = new Mock>(); - - var openAILogger = new Mock>(); - var responsesLogger = new Mock>(); - var ollamaLogger = new Mock>(); - var geminiLogger = new Mock>(); - var anthropicLogger = new Mock>(); - var messageExtensionServiceMock = new Mock(); - var httpClientFactoryMock = new Mock(); - var serviceProviderMock = new Mock(); - - _openAIServiceMock = new Mock( - _dbContext, openAILogger.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); - _ollamaServiceMock = new Mock( - _dbContext, ollamaLogger.Object, serviceProviderMock.Object, httpClientFactoryMock.Object); - _geminiServiceMock = new Mock( - _dbContext, geminiLogger.Object, httpClientFactoryMock.Object); - var anthropicServiceMock = new Mock( - _dbContext, anthropicLogger.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); - var responsesService = new OpenAIResponsesService( - _dbContext, responsesLogger.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); - - var services = new ServiceCollection() - .AddSingleton(_openAIServiceMock.Object) - .AddSingleton(_ollamaServiceMock.Object) - .AddSingleton(_geminiServiceMock.Object) - .AddSingleton(anthropicServiceMock.Object) - .AddSingleton(responsesService) - .BuildServiceProvider(); - - _factory = new LLMFactory( - services, - _loggerMock.Object); - } - - [Fact] - public void GetLLMService_OpenAI_ReturnsOpenAIService() { - var service = _factory.GetLLMService(LLMProvider.OpenAI); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Ollama_ReturnsOllamaService() { - var service = _factory.GetLLMService(LLMProvider.Ollama); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Gemini_ReturnsGeminiService() { - var service = _factory.GetLLMService(LLMProvider.Gemini); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_MiniMax_ReturnsOpenAIService() { - var service = _factory.GetLLMService(LLMProvider.MiniMax); - - Assert.Same(_openAIServiceMock.Object, service); - } - - [Fact] - public void GetLLMService_None_ThrowsKeyNotFound() { - Assert.Throws(() => _factory.GetLLMService(LLMProvider.None)); - } - - // ==================================================================== - // Phase 2:按 binding 协议选择 client(LlmProtocol) - // ==================================================================== - - [Fact] - public void GetLLMService_Protocol_OpenAIChat_ReturnsOpenAIService() { - var service = _factory.GetLLMService(LlmProtocol.OpenAIChat); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Protocol_OpenAIResponses_ReturnsOpenAIResponsesService() { - var service = _factory.GetLLMService(LlmProtocol.OpenAIResponses); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Protocol_AnthropicMessages_ReturnsAnthropicService() { - var service = _factory.GetLLMService(LlmProtocol.AnthropicMessages); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Protocol_Ollama_ReturnsOllamaService() { - var service = _factory.GetLLMService(LlmProtocol.Ollama); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Protocol_Gemini_ReturnsGeminiService() { - var service = _factory.GetLLMService(LlmProtocol.Gemini); - Assert.NotNull(service); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Route_BindingNull_FallsBackToProvider() { - var channel = new LLMChannel { - Name = "legacy", - Gateway = "https://legacy.example", - ApiKey = "k", - Provider = LLMProvider.Anthropic, - Parallel = 1, - Priority = 1 - }; - var route = new ResolvedLlmRoute(channel, null, new ChannelWithModel { ModelName = "m" }); - var service = _factory.GetLLMService(route); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GetLLMService_Route_BindingNonNull_UsesBindingProtocol() { - var channel = new LLMChannel { - Name = "oc", - Gateway = "https://legacy.example", - ApiKey = "k", - Provider = LLMProvider.Anthropic, - Parallel = 1, - Priority = 1 - }; - var binding = new LLMApiBinding { - Id = 7, - LLMChannelId = channel.Id, - Endpoint = "https://opencode.ai/zen/v1", - Protocol = LlmProtocol.OpenAIChat, - AuthProfile = LlmAuthProfile.Bearer, - IsDefault = true - }; - var route = new ResolvedLlmRoute(channel, binding, new ChannelWithModel { ModelName = "m" }); - // 渠道 provider 是 Anthropic,但 binding 协议是 OpenAIChat → 必须选 OpenAIService(绝不按品牌猜) - var service = _factory.GetLLMService(route); - Assert.IsAssignableFrom(service); - } - - [Fact] - public void GeneralAndAgentPaths_ResolveSameRoute_AndSelectSameClient() { - // 同一模型、同一 channel、双 binding:General 路径(Resolve)与 Agent 路径(ResolveFirst) - // 必须解析出相同的 binding(IsPreferred 优先于 channel 默认),且 factory 选同一 client。 - var channel = new LLMChannel { - Name = "oc", - Gateway = "https://legacy.example", - ApiKey = "shared-secret", - Provider = LLMProvider.OpenAI, - Parallel = 2, - Priority = 10 - }; - var chatBinding = new LLMApiBinding { - Id = 1, - LLMChannelId = channel.Id, - Endpoint = "https://opencode.ai/zen/v1", - Protocol = LlmProtocol.OpenAIChat, - AuthProfile = LlmAuthProfile.Bearer, - IsDefault = true - }; - var responsesBinding = new LLMApiBinding { - Id = 2, - LLMChannelId = channel.Id, - Endpoint = "https://opencode.ai/zen/go/v1", - Protocol = LlmProtocol.OpenAIResponses, - AuthProfile = LlmAuthProfile.Bearer, - IsDefault = false - }; - channel.Bindings.Add(chatBinding); - channel.Bindings.Add(responsesBinding); - var chatRow = new ChannelWithModel { - Id = 1, - ModelName = "gpt-x", - LLMChannelId = channel.Id, - LLMChannel = channel, - ApiBindingId = chatBinding.Id, - ApiBinding = chatBinding - }; - var preferredRow = new ChannelWithModel { - Id = 2, - ModelName = "gpt-x", - LLMChannelId = channel.Id, - LLMChannel = channel, - ApiBindingId = responsesBinding.Id, - ApiBinding = responsesBinding, - IsPreferred = true - }; - var rows = new List { chatRow, preferredRow }; - - var generalRoute = LlmRouteResolver.Resolve(channel, "gpt-x", rows, _loggerMock.Object); - var agentRoute = LlmRouteResolver.ResolveFirst( - new[] { (channel, rows) }, "gpt-x", _loggerMock.Object); - - Assert.NotNull(generalRoute); - Assert.NotNull(agentRoute); - Assert.Equal(generalRoute!.Binding!.Id, agentRoute!.Binding!.Id); - Assert.Equal(responsesBinding.Id, generalRoute.Binding.Id); - Assert.Equal(responsesBinding.Endpoint, generalRoute.Binding.Endpoint); - Assert.Equal(responsesBinding.Protocol, generalRoute.Binding.Protocol); - Assert.Equal(responsesBinding.AuthProfile, generalRoute.Binding.AuthProfile); - Assert.Equal("gpt-x", generalRoute.Model.ModelName); - - var generalService = _factory.GetLLMService(generalRoute); - var agentService = _factory.GetLLMService(agentRoute); - Assert.Same(generalService, agentService); - Assert.IsAssignableFrom(generalService); - } - - [Fact] - public void ServiceName_ReturnsLLMFactory() { - Assert.Equal("LLMFactory", _factory.ServiceName); - } - - [Fact] - public void Factory_ImplementsILLMFactory() { - Assert.IsAssignableFrom(_factory); - } - - [Fact] - public void Factory_ImplementsIService() { - Assert.IsAssignableFrom(_factory); - } - } -} diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs index e26cf990..d89d1948 100644 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs @@ -12,8 +12,8 @@ public interface IGeneralLLMService { Task> GetChannelsAsync(string modelName); IAsyncEnumerable ExecAsync(Message message, long ChatId, CancellationToken cancellationToken = default); IAsyncEnumerable ExecAsync(Message message, long ChatId, LlmExecutionContext executionContext, CancellationToken cancellationToken = default); - IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, ILLMService service, LLMChannel channel, CancellationToken cancellation); - IAsyncEnumerable ExecOperationAsync(Func> operation, string modelName, CancellationToken cancellationToken = default); + IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellation); + IAsyncEnumerable ExecOperationAsync(Func> operation, string modelName, CancellationToken cancellationToken = default); /// /// Resume LLM execution from a previously saved continuation snapshot. @@ -22,11 +22,11 @@ public interface IGeneralLLMService { Task AnalyzeImageAsync(string PhotoPath, long ChatId, CancellationToken cancellationToken = default); Task AnalyzeImageAsync(string PhotoPath, long ChatId, string prompt, CancellationToken cancellationToken = default); - IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILLMService service, LLMChannel channel, CancellationToken cancellationToken = default); - IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILLMService service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default); + IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default); + IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default); Task GenerateEmbeddingsAsync(Message message, long ChatId); Task GenerateEmbeddingsAsync(string message, CancellationToken cancellationToken = default); - IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILLMService service, LLMChannel channel, CancellationToken cancellationToken = default); + IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default); Task GetAltPhotoAvailableCapacityAsync(); Task GetAvailableCapacityAsync(string modelName = "gemma3:27b"); } diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMFactory.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMFactory.cs deleted file mode 100644 index 7336ca70..00000000 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMFactory.cs +++ /dev/null @@ -1,16 +0,0 @@ -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Service.AI.LLM; - -namespace TelegramSearchBot.Interface.AI.LLM { - public interface ILLMFactory : IService { - /// 按 legacy 品牌/订阅枚举选择服务(Phase-3 调用方仍使用,必须保持可用)。 - ILLMService GetLLMService(LLMProvider provider); - - /// 按 binding 线协议选择服务:OpenAIChat→OpenAIService,OpenAIResponses→OpenAIResponsesService, - /// AnthropicMessages→AnthropicService,Ollama→OllamaService,Gemini→GeminiService。 - ILLMService GetLLMService(LlmProtocol protocol); - - /// 按解析路由选择服务:有 binding 时按 binding.Protocol,否则(legacy 临时路由)按 channel.Provider。 - ILLMService GetLLMService(ResolvedLlmRoute route); - } -} diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProvider.cs similarity index 99% rename from TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs rename to TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProvider.cs index e676036f..3248a35d 100644 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProvider.cs @@ -8,7 +8,7 @@ using TelegramSearchBot.Model.Data; namespace TelegramSearchBot.Interface.AI.LLM { - public interface ILLMService { + public interface ILlmProvider { public IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, LLMChannel channel, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs index a9e48fd6..0ac1edd3 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs @@ -25,7 +25,7 @@ namespace TelegramSearchBot.Service.AI.LLM { [Injectable(ServiceLifetime.Transient)] - public class AnthropicService : IService, ILLMService { + public class AnthropicService : IService, ILlmProvider { public string ServiceName => "AnthropicService"; private readonly ILogger _logger; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs index c96ac187..e5b74c9b 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs @@ -23,7 +23,7 @@ namespace TelegramSearchBot.Service.AI.LLM { [Injectable(ServiceLifetime.Transient)] - public class GeminiService : ILLMService, IService { + public class GeminiService : ILlmProvider, IService { public string ServiceName => "GeminiService"; private readonly ILogger _logger; private readonly DataDbContext _dbContext; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs index 4a39f57d..a6811e39 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs @@ -19,7 +19,7 @@ public class GeneralLLMService : IService, IGeneralLLMService { protected IConnectionMultiplexer connectionMultiplexer { get; set; } private readonly DataDbContext _dbContext; private readonly ILogger _logger; - private readonly ILLMFactory _LLMFactory; + private readonly LlmProviderRegistry _LLMFactory; public string ServiceName => "GeneralLLMService"; @@ -36,7 +36,7 @@ public GeneralLLMService( IConnectionMultiplexer connectionMultiplexer, DataDbContext dbContext, ILogger logger, - ILLMFactory _LLMFactory + LlmProviderRegistry _LLMFactory ) { this.connectionMultiplexer = connectionMultiplexer; _dbContext = dbContext; @@ -96,7 +96,7 @@ public async IAsyncEnumerable ExecAsync( Model.Data.Message message, long ChatId, string modelName, - ILLMService service, + ILlmProvider service, LLMChannel channel, CancellationToken cancellation) { await foreach (var e in service.ExecAsync(message, ChatId, modelName, channel, cancellation).WithCancellation(cancellation)) { @@ -149,7 +149,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( if (route == null) { // 六.8 legacy 回退:模型行缺失/软删时按渠道 Provider/Gateway 继续,不丢弃排队中的续聊 _logger.LogWarning("Cannot resume: model {Model} has no route on channel {ChannelId}, falling back to legacy provider route", snapshot.ModelName, channel.Id); - var legacyService = _LLMFactory.GetLLMService(channel.Provider); + var legacyService = _LLMFactory.GetProvider(channel.Provider); await foreach (var item in legacyService.ResumeFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken) .WithCancellation(cancellationToken)) { yield return item; @@ -157,7 +157,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( yield break; } - var service = _LLMFactory.GetLLMService(route); + var service = _LLMFactory.GetProvider(route); await foreach (var item in service.ResumeFromSnapshotAsync(snapshot, channel, route.Binding, executionContext, cancellationToken) .WithCancellation(cancellationToken)) { @@ -165,7 +165,7 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( } } public async IAsyncEnumerable ExecOperationAsync( - Func> operation, + Func> operation, string modelName, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default ) { @@ -208,7 +208,7 @@ orderby s.Priority descending var route = LlmRouteResolver.Resolve(channel, modelName, modelRows.Where(r => r.LLMChannelId == channel.Id).ToList(), _logger); if (route == null) continue; - var service = _LLMFactory.GetLLMService(route); + var service = _LLMFactory.GetProvider(route); if (count < channel.Parallel) { // 获取锁并增加计数 @@ -275,19 +275,19 @@ public async Task AnalyzeImageAsync(string PhotoPath, long ChatId, strin _logger.LogWarning($"未能获取 {modelName} 模型的图片分析结果"); return $"Error:未能获取 {modelName} 模型的图片分析结果"; } - public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILLMService service, LLMChannel channel, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default) { await foreach (var result in AnalyzeImageAsync(PhotoPath, ChatId, modelName, service, channel, DefaultAltPhotoPrompt, cancellationToken)) { yield return result; } } - public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILLMService service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default) { prompt = string.IsNullOrWhiteSpace(prompt) ? DefaultAltPhotoPrompt : prompt; yield return await service.AnalyzeImageAsync(PhotoPath, modelName, channel, prompt); yield break; } - public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILLMService service, LLMChannel channel, LLMApiBinding binding, string prompt, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, LLMApiBinding binding, string prompt, CancellationToken cancellationToken = default) { prompt = string.IsNullOrWhiteSpace(prompt) ? DefaultAltPhotoPrompt : prompt; yield return await service.AnalyzeImageAsync(PhotoPath, modelName, channel, binding, prompt); yield break; @@ -323,11 +323,11 @@ public async Task GenerateEmbeddingsAsync(string message, CancellationT _logger.LogWarning($"未能获取 {modelName} 模型的嵌入向量"); return Array.Empty(); } - public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILLMService service, LLMChannel channel, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default) { yield return await service.GenerateEmbeddingsAsync(message, modelName, channel); yield break; } - public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILLMService service, LLMChannel channel, LLMApiBinding binding, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmProvider service, LLMChannel channel, LLMApiBinding binding, CancellationToken cancellationToken = default) { yield return await service.GenerateEmbeddingsAsync(message, modelName, channel, binding); yield break; } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LLMFactory.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs similarity index 65% rename from TelegramSearchBot.LLM/Service/AI/LLM/LLMFactory.cs rename to TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs index 397155d4..3ffd7690 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LLMFactory.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs @@ -3,27 +3,23 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using TelegramSearchBot.Attributes; -using TelegramSearchBot.Interface; using TelegramSearchBot.Interface.AI.LLM; using TelegramSearchBot.Model.AI; namespace TelegramSearchBot.Service.AI.LLM { - [Injectable(ServiceLifetime.Transient)] - public class LLMFactory : IService, ILLMFactory { - public string ServiceName => "LLMFactory"; - + /// + /// Resolves the provider implementation for a channel/protocol/route. + /// Replaces the old ILLMFactory/LLMFactory pair (no IService baggage). + /// + [Injectable(ServiceLifetime.Singleton)] + public class LlmProviderRegistry { private readonly IServiceProvider _serviceProvider; - private readonly ILogger _logger; - public LLMFactory( - IServiceProvider serviceProvider, - ILogger logger - ) { - _logger = logger; + public LlmProviderRegistry(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } - public ILLMService GetLLMService(LLMProvider provider) { + public virtual ILlmProvider GetProvider(LLMProvider provider) { return provider switch { LLMProvider.OpenAI => _serviceProvider.GetRequiredService(), LLMProvider.Ollama => _serviceProvider.GetRequiredService(), @@ -32,26 +28,25 @@ public ILLMService GetLLMService(LLMProvider provider) { LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), - _ => throw new KeyNotFoundException($"No LLM service registered for provider {provider}.") + _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") }; } - public ILLMService GetLLMService(LlmProtocol protocol) { + public virtual ILlmProvider GetProvider(LlmProtocol protocol) { return protocol switch { LlmProtocol.OpenAIChat => _serviceProvider.GetRequiredService(), LlmProtocol.OpenAIResponses => _serviceProvider.GetRequiredService(), LlmProtocol.AnthropicMessages => _serviceProvider.GetRequiredService(), LlmProtocol.Ollama => _serviceProvider.GetRequiredService(), LlmProtocol.Gemini => _serviceProvider.GetRequiredService(), - _ => throw new KeyNotFoundException($"No LLM service registered for protocol {protocol}.") + _ => throw new KeyNotFoundException($"No LLM provider registered for protocol {protocol}.") }; } - public ILLMService GetLLMService(ResolvedLlmRoute route) { + public virtual ILlmProvider GetProvider(ResolvedLlmRoute route) { return route.Binding != null - ? GetLLMService(route.Binding.Protocol) - : GetLLMService(route.Channel.Provider); + ? GetProvider(route.Binding.Protocol) + : GetProvider(route.Channel.Provider); } - } } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs index 42d08acf..a78858e5 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs @@ -48,7 +48,7 @@ public async Task UpdateChannelModelCapabilities(int channelId) { return false; } - var service = GetLLMService(channel.Provider); + var service = GetProvider(channel.Provider); if (service == null) { _logger.LogWarning("No LLM service found for provider {Provider}", channel.Provider); return false; @@ -281,15 +281,15 @@ private string GetCapabilityDescription(string capabilityName) { /// /// 根据提供商获取对应的LLM服务 /// - private ILLMService GetLLMService(LLMProvider provider) { + private ILlmProvider GetProvider(LLMProvider provider) { return provider switch { - LLMProvider.OpenAI => _serviceProvider.GetService(typeof(OpenAIService)) as ILLMService, - LLMProvider.Ollama => _serviceProvider.GetService(typeof(OllamaService)) as ILLMService, - LLMProvider.Gemini => _serviceProvider.GetService(typeof(GeminiService)) as ILLMService, - LLMProvider.MiniMax => _serviceProvider.GetService(typeof(OpenAIService)) as ILLMService, - LLMProvider.LMStudio => _serviceProvider.GetService(typeof(OpenAIService)) as ILLMService, - LLMProvider.Anthropic => _serviceProvider.GetService(typeof(AnthropicService)) as ILLMService, - LLMProvider.ResponsesAPI => _serviceProvider.GetService(typeof(OpenAIResponsesService)) as ILLMService, + LLMProvider.OpenAI => _serviceProvider.GetService(typeof(OpenAIService)) as ILlmProvider, + LLMProvider.Ollama => _serviceProvider.GetService(typeof(OllamaService)) as ILlmProvider, + LLMProvider.Gemini => _serviceProvider.GetService(typeof(GeminiService)) as ILlmProvider, + LLMProvider.MiniMax => _serviceProvider.GetService(typeof(OpenAIService)) as ILlmProvider, + LLMProvider.LMStudio => _serviceProvider.GetService(typeof(OpenAIService)) as ILlmProvider, + LLMProvider.Anthropic => _serviceProvider.GetService(typeof(AnthropicService)) as ILlmProvider, + LLMProvider.ResponsesAPI => _serviceProvider.GetService(typeof(OpenAIResponsesService)) as ILlmProvider, _ => null }; } @@ -305,7 +305,7 @@ public async Task TestGetAllModelCapabilitiesAsync() { foreach (var channel in channels) { results.Add($"\n=== {channel.Provider} 通道 (ID: {channel.Id}) ==="); - var service = GetLLMService(channel.Provider); + var service = GetProvider(channel.Provider); if (service == null) { results.Add($"未找到 {channel.Provider} 服务"); continue; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs index e485b267..47295b7e 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs @@ -19,7 +19,7 @@ using TelegramSearchBot.Attributes; using TelegramSearchBot.Common; using TelegramSearchBot.Interface; -using TelegramSearchBot.Interface.AI.LLM; // For ILLMService +using TelegramSearchBot.Interface.AI.LLM; // For ILlmProvider using TelegramSearchBot.Model; using TelegramSearchBot.Model.AI; using TelegramSearchBot.Model.Data; @@ -27,7 +27,7 @@ namespace TelegramSearchBot.Service.AI.LLM { // Standalone implementation, not using BaseLlmService [Injectable(ServiceLifetime.Transient)] - public class OllamaService : IService, ILLMService { + public class OllamaService : IService, ILlmProvider { public string ServiceName => "OllamaService"; private readonly ILogger _logger; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs index 3ae6374c..8089df19 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs @@ -36,7 +36,7 @@ namespace TelegramSearchBot.Service.AI.LLM { /// 与现有的 OpenAIService(Chat Completions API)并存。 /// [Injectable(ServiceLifetime.Transient)] - public class OpenAIResponsesService : IService, ILLMService { + public class OpenAIResponsesService : IService, ILlmProvider { public string ServiceName => "OpenAIResponsesService"; /// @@ -133,7 +133,7 @@ internal static (string toolDefinitionHash, string stablePrefixHash, string prom // ======================================================================== - // ILLMService Implementation + // ILlmProvider Implementation // ======================================================================== public async IAsyncEnumerable ExecAsync( @@ -1040,23 +1040,23 @@ private ModelWithCapabilities InferOpenAIModelCapabilities(string modelName) { return model; } - Task ILLMService.AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt) { + Task ILlmProvider.AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt) { return AnalyzeImageAsync(photoPath, modelName, channel, prompt); } - Task> ILLMService.GetAllModels(LLMChannel channel) { + Task> ILlmProvider.GetAllModels(LLMChannel channel) { return GetAllModels(channel); } - Task> ILLMService.GetAllModelsWithCapabilities(LLMChannel channel) { + Task> ILlmProvider.GetAllModelsWithCapabilities(LLMChannel channel) { return GetAllModelsWithCapabilities(channel); } - Task ILLMService.GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { + Task ILlmProvider.GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { return GenerateEmbeddingsAsync(text, modelName, channel); } - IAsyncEnumerable ILLMService.ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, + IAsyncEnumerable ILlmProvider.ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, LlmExecutionContext executionContext, CancellationToken cancellationToken) { return ResumeFromSnapshotAsync(snapshot, channel, executionContext, cancellationToken); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs index d893b11c..3c420fb7 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs @@ -33,7 +33,7 @@ namespace TelegramSearchBot.Service.AI.LLM { // Standalone implementation, not inheriting from BaseLlmService [Injectable(ServiceLifetime.Transient)] - public class OpenAIService : IService, ILLMService { + public class OpenAIService : IService, ILlmProvider { public string ServiceName => "OpenAIService"; /// diff --git a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs index 5acf3fd8..3f41afb4 100644 --- a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs +++ b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs @@ -76,7 +76,7 @@ public async IAsyncEnumerable CallAsync( }; } - private ILLMService ResolveService(LLMProvider provider) { + private ILlmProvider ResolveService(LLMProvider provider) { return provider switch { LLMProvider.Ollama => _serviceProvider.GetRequiredService(), LLMProvider.Gemini => _serviceProvider.GetRequiredService(), @@ -86,8 +86,8 @@ private ILLMService ResolveService(LLMProvider provider) { }; } - /// 按 binding 线协议解析 client(与 ILLMFactory.GetLLMService(LlmProtocol) 同构)。 - private ILLMService ResolveService(LlmProtocol protocol) { + /// 按 binding 线协议解析 client(与 LlmProviderRegistry.GetProvider(LlmProtocol) 同构)。 + private ILlmProvider ResolveService(LlmProtocol protocol) { return protocol switch { LlmProtocol.OpenAIChat => _serviceProvider.GetRequiredService(), LlmProtocol.OpenAIResponses => _serviceProvider.GetRequiredService(), diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs index d700b851..2ae718ea 100644 --- a/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs +++ b/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs @@ -78,11 +78,11 @@ public EditLLMConfHelperTest() { _geminiServiceMock = new Mock(_context, geminiLoggerMock.Object, httpClientFactoryMock.Object); // 新增 ILLMFactory mock - var llmFactoryMock = new Mock(); - llmFactoryMock.Setup(f => f.GetLLMService(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); - llmFactoryMock.Setup(f => f.GetLLMService(LLMProvider.MiniMax)).Returns(_openAIServiceMock.Object); - llmFactoryMock.Setup(f => f.GetLLMService(LLMProvider.Ollama)).Returns(_ollamaServiceMock.Object); - llmFactoryMock.Setup(f => f.GetLLMService(LLMProvider.Gemini)).Returns(_geminiServiceMock.Object); + var llmFactoryMock = new Mock((IServiceProvider)null); + llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); + llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.MiniMax)).Returns(_openAIServiceMock.Object); + llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Ollama)).Returns(_ollamaServiceMock.Object); + llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Gemini)).Returns(_geminiServiceMock.Object); // 创建Logger mock _loggerMock = new Mock>(); diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs index 7bfb2fc9..7fe0c8d1 100644 --- a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs +++ b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs @@ -78,10 +78,10 @@ public EditLLMConfTest() { geminiServiceMock.Setup(g => g.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "gemini-model1", "gemini-model2" }); - var llmFactoryMock = new Mock(); - llmFactoryMock.Setup(f => f.GetLLMService(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); - llmFactoryMock.Setup(f => f.GetLLMService(LLMProvider.Ollama)).Returns(ollamaServiceMock.Object); - llmFactoryMock.Setup(f => f.GetLLMService(LLMProvider.Gemini)).Returns(geminiServiceMock.Object); + var llmFactoryMock = new Mock((IServiceProvider)null); + llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); + llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Ollama)).Returns(ollamaServiceMock.Object); + llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Gemini)).Returns(geminiServiceMock.Object); helperMock = new Mock(); helperMock.Setup(h => h.AddChannel( diff --git a/TelegramSearchBot/Extension/ServiceCollectionExtension.cs b/TelegramSearchBot/Extension/ServiceCollectionExtension.cs index 33440f8e..a6710baf 100644 --- a/TelegramSearchBot/Extension/ServiceCollectionExtension.cs +++ b/TelegramSearchBot/Extension/ServiceCollectionExtension.cs @@ -124,7 +124,7 @@ public static IServiceCollection AddAutoRegisteredServices(this IServiceCollecti public static IServiceCollection ConfigureAllServices(this IServiceCollection services) { var assembly = typeof(GeneralBootstrap).Assembly; - var llmAssembly = typeof(ILLMFactory).Assembly; + var llmAssembly = typeof(LlmProviderRegistry).Assembly; return services .AddTelegramBotClient() .AddRedis() diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs index cb1c7e5a..15cfe379 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs @@ -20,13 +20,13 @@ namespace TelegramSearchBot.Service.Manage { public class EditLLMConfHelper : IService, IEditLLMConfHelper { public string ServiceName => "EditLLMConfHelper"; protected readonly DataDbContext DataContext; - private readonly ILLMFactory _LLMFactory; + private readonly LlmProviderRegistry _LLMFactory; private readonly IModelCapabilityService _modelCapabilityService; private readonly ILogger _logger; public EditLLMConfHelper( DataDbContext context, - ILLMFactory llmFactory, + LlmProviderRegistry llmFactory, IModelCapabilityService modelCapabilityService, ILogger logger ) { @@ -63,7 +63,7 @@ public async Task AddChannel(string Name, string Gateway, string ApiKey, LL _logger.LogInformation("成功添加新通道: {ChannelName} ({Provider})", Name, Provider); IEnumerable models; - var service = _LLMFactory.GetLLMService(Provider); + var service = _LLMFactory.GetProvider(Provider); if (service == null) { _logger.LogWarning("未找到提供商 {Provider} 的LLM服务", Provider); return -1; @@ -122,7 +122,7 @@ public async Task RefreshAllChannel() { continue; } - var service = _LLMFactory.GetLLMService(channel.Provider); + var service = _LLMFactory.GetProvider(channel.Provider); if (service == null) { _logger.LogWarning("未找到通道 {ChannelName} ({Provider}) 的LLM服务", channel.Name, channel.Provider); continue; From b7ee57f6a0f81a4ce581bcd0b402e1281fbae4b5 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 09:47:46 +0800 Subject: [PATCH 17/30] refactor(llm): remove dead GetChatHistory/IsSameSender legacy history paths from provider services --- .../Service/AI/LLM/AnthropicService.cs | 176 +----------------- .../Service/AI/LLM/GeminiService.cs | 85 --------- .../Service/AI/LLM/OpenAIResponsesService.cs | 23 +-- .../Service/AI/LLM/OpenAIService.cs | 153 --------------- 4 files changed, 22 insertions(+), 415 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs index 0ac1edd3..782713a1 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs @@ -294,161 +294,22 @@ private ModelWithCapabilities InferAnthropicModelCapabilities(string modelName) } #endregion - - #region Chat History - - public bool IsSameSender(DataMessage message1, DataMessage message2) { - if (message1 == null || message2 == null) return false; - bool msg1IsBot = message1.FromUserId == Env.BotId; - bool msg2IsBot = message2.FromUserId == Env.BotId; - return msg1IsBot == msg2IsBot; - } - - /// - /// Build Anthropic message list from DB history. Anthropic requires alternating user/assistant roles. - /// Returns (systemPrompt, messages). - /// - public async Task<(string systemPrompt, List messages)> GetChatHistory( - long chatId, string systemPrompt, DataMessage inputMessage = null) { - return await GetChatHistory(chatId, systemPrompt, inputMessage, false); - } - - public async Task<(string systemPrompt, List messages)> GetChatHistory( - long chatId, string systemPrompt, DataMessage inputMessage, bool supportsVision) { - var dbMessages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == chatId && m.DateTime > DateTime.UtcNow.AddHours(-1)) - .OrderBy(m => m.DateTime) - .ToListAsync(); - - if (dbMessages.Count < 10) { - dbMessages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == chatId) - .OrderByDescending(m => m.DateTime) - .Take(10) - .OrderBy(m => m.DateTime) - .ToListAsync(); - } - - if (_llmVisibilityService != null) { - dbMessages = await _llmVisibilityService.FilterVisibleMessagesAsync(chatId, dbMessages); - } - - if (inputMessage != null && - ( _llmVisibilityService == null || - !await _llmVisibilityService.IsUserInvisibleAsync(chatId, inputMessage.FromUserId) )) { - dbMessages.Add(inputMessage); + private static string ExtractTextFromContent(MessageParamContent content) { + if (content.TryPickString(out var text)) { + return text; } - - _logger.LogInformation("Anthropic GetChatHistory: Found {Count} messages for ChatId {ChatId}.", dbMessages.Count, chatId); - - var result = new List(); - var str = new StringBuilder(); - DataMessage previous = null; - var userCache = new Dictionary(); - var pendingImages = new List(); - - foreach (var message in dbMessages) { - // Skip leading bot messages (Anthropic messages must start with user) - if (previous == null && !result.Any() && message.FromUserId == Env.BotId) { - previous = message; - continue; - } - - if (previous != null && !IsSameSender(previous, message)) { - AddMessageToHistory(result, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - str.Clear(); - pendingImages.Clear(); - } - - str.Append($"[{message.DateTime:yyyy-MM-dd HH:mm:ss zzz}]"); - if (message.FromUserId != 0) { - if (!userCache.TryGetValue(message.FromUserId, out var fromUser)) { - fromUser = await _dbContext.UserData.AsNoTracking() - .FirstOrDefaultAsync(u => u.Id == message.FromUserId); - if (fromUser != null) userCache[message.FromUserId] = fromUser; - } - str.Append(fromUser != null ? $"{fromUser.FirstName} {fromUser.LastName}".Trim() : $"User({message.FromUserId})"); - } else { - str.Append("System/Unknown"); - } - - if (message.ReplyToMessageId != 0) { - str.Append($"(Reply to msg {message.ReplyToMessageId})"); - } - str.Append(':').Append(message.Content).Append("\n"); - - // Add message extensions if any - var extensions = await _messageExtensionService.GetByMessageDataIdAsync(message.Id); - if (extensions != null && extensions.Any()) { - str.Append("[扩展信息:"); - foreach (var ext in extensions) { - str.Append($"{ext.Name}={ext.Value}; "); - } - str.Append("]\n"); - } - - // 如果模型支持视觉,尝试加载消息关联的图片 - if (supportsVision && message.FromUserId != Env.BotId) { - var imageBytes = TryLoadMessagePhoto(message.GroupId, message.MessageId); - if (imageBytes != null) { - pendingImages.Add(imageBytes); + if (content.TryPickContentBlockParams(out var blocks)) { + var sb = new StringBuilder(); + foreach (var block in blocks) { + if (block.TryPickText(out var textBlock)) { + sb.Append(textBlock.Text); } } - - previous = message; - } - - if (previous != null && str.Length > 0) { - AddMessageToHistory(result, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - } - - // Ensure messages alternate user/assistant and start with user - result = EnsureAlternatingRoles(result); - - return (systemPrompt, result); - } - - private void AddMessageToHistory(List history, long fromUserId, string content) { - AddMessageToHistory(history, fromUserId, content, null); - } - - private void AddMessageToHistory(List history, long fromUserId, string content, List images) { - if (string.IsNullOrWhiteSpace(content) && ( images == null || images.Count == 0 )) return; - if (!string.IsNullOrWhiteSpace(content)) { - content = System.Text.RegularExpressions.Regex.Replace(content.Trim(), @"\n{3,}", "\n\n"); - } - - var role = fromUserId == Env.BotId ? Role.Assistant : Role.User; - - if (images != null && images.Count > 0 && role == Role.User) { - var contentBlocks = new List(); - if (!string.IsNullOrWhiteSpace(content)) { - contentBlocks.Add(new TextBlockParam(content.Trim())); - } - foreach (var imageBytes in images) { - contentBlocks.Add(new ImageBlockParam { - Source = new Base64ImageSource { - Data = Convert.ToBase64String(imageBytes), - MediaType = MediaType.ImagePng - } - }); - } - history.Add(new MessageParam { - Role = role, - Content = contentBlocks - }); - } else { - history.Add(new MessageParam { - Role = role, - Content = content?.Trim() ?? "" - }); + return sb.ToString(); } + return content.ToString(); } - /// - /// Ensures message list starts with user and alternates between user/assistant. - /// Merges consecutive same-role messages. - /// internal static List EnsureAlternatingRoles(List messages) { if (!messages.Any()) return messages; @@ -480,23 +341,6 @@ internal static List EnsureAlternatingRoles(List mes return result; } - private static string ExtractTextFromContent(MessageParamContent content) { - if (content.TryPickString(out var text)) { - return text; - } - if (content.TryPickContentBlockParams(out var blocks)) { - var sb = new StringBuilder(); - foreach (var block in blocks) { - if (block.TryPickText(out var textBlock)) { - sb.Append(textBlock.Text); - } - } - return sb.ToString(); - } - return content.ToString(); - } - - #endregion #region Vision Support diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs index e5b74c9b..3e53ade4 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs @@ -104,91 +104,6 @@ private void AddMessageToHistory(List chatHistory, l } } - public async Task> GetChatHistory(long chatId, Message inputMessage = null) { - return await GetChatHistory(chatId, inputMessage, false); - } - - public async Task> GetChatHistory(long chatId, Message inputMessage, bool supportsVision) { - var messages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == chatId && m.DateTime > DateTime.UtcNow.AddHours(-1)) - .OrderBy(m => m.DateTime) - .ToListAsync(); - - if (messages.Count < 10) { - messages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == chatId) - .OrderByDescending(m => m.DateTime) - .Take(10) - .OrderBy(m => m.DateTime) - .ToListAsync(); - } - - if (_llmVisibilityService != null) { - messages = await _llmVisibilityService.FilterVisibleMessagesAsync(chatId, messages); - } - - if (inputMessage != null && - ( _llmVisibilityService == null || - !await _llmVisibilityService.IsUserInvisibleAsync(chatId, inputMessage.FromUserId) )) { - messages.Add(inputMessage); - } - - var chatHistory = new List(); - var str = new StringBuilder(); - Message previous = null; - var userCache = new Dictionary(); - var pendingImages = new List(); - - foreach (var message in messages) { - if (previous == null && !chatHistory.Any() && message.FromUserId == Env.BotId) { - previous = message; - continue; - } - - if (previous != null && ( previous.FromUserId == Env.BotId ) != ( message.FromUserId == Env.BotId )) { - AddMessageToHistory(chatHistory, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - str.Clear(); - pendingImages.Clear(); - } - - str.Append($"[{message.DateTime:yyyy-MM-dd HH:mm:ss zzz}]"); - if (message.FromUserId != 0) { - if (!userCache.TryGetValue(message.FromUserId, out var fromUser)) { - fromUser = await _dbContext.UserData.AsNoTracking() - .FirstOrDefaultAsync(u => u.Id == message.FromUserId); - if (fromUser != null) userCache[message.FromUserId] = fromUser; - } - str.Append(fromUser != null ? $"{fromUser.FirstName} {fromUser.LastName}".Trim() : $"User({message.FromUserId})"); - } else { - str.Append("System/Unknown"); - } - - if (message.ReplyToMessageId != 0) { - str.Append($" (Reply to msg {message.ReplyToMessageId})"); - } - str.Append(": ").Append(message.Content).Append("\n"); - - // 如果模型支持视觉,尝试加载消息关联的图片 - if (supportsVision && message.FromUserId != Env.BotId) { - var imageBytes = TryLoadMessagePhoto(message.GroupId, message.MessageId); - if (imageBytes != null) { - pendingImages.Add(imageBytes); - } - } - - previous = message; - } - - if (previous != null && str.Length > 0) { - AddMessageToHistory(chatHistory, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - } - - return chatHistory; - } - - /// - /// 检查模型是否支持视觉能力 - /// private async Task CheckVisionSupport(string modelName, int channelId) { try { var channelWithModel = await _dbContext.ChannelsWithModel diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs index 8089df19..0db9be85 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs @@ -363,6 +363,18 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( // ======================================================================== + public bool IsSameSender(Message message1, Message message2) { + if (message1 == null || message2 == null) return false; + bool msg1IsUser = message1.FromUserId != Env.BotId; + bool msg2IsUser = message2.FromUserId != Env.BotId; + return msg1IsUser == msg2IsUser; + } + + // ======================================================================== + // Helper: Load message photo + // ======================================================================== + + private async Task CheckVisionSupport(string modelName, int channelId) { try { var channelWithModel = await _dbContext.ChannelsWithModel @@ -384,17 +396,6 @@ private async Task CheckVisionSupport(string modelName, int channelId) { // Helper: IsSameSender // ======================================================================== - public bool IsSameSender(Message message1, Message message2) { - if (message1 == null || message2 == null) return false; - bool msg1IsUser = message1.FromUserId != Env.BotId; - bool msg2IsUser = message2.FromUserId != Env.BotId; - return msg1IsUser == msg2IsUser; - } - - // ======================================================================== - // Helper: Load message photo - // ======================================================================== - private byte[] TryLoadMessagePhoto(long chatId, long messageId) { try { var dirPath = Path.Combine(Env.WorkDir, "Photos", $"{chatId}"); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs index 3c420fb7..b6780067 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs @@ -901,159 +901,6 @@ private ModelWithCapabilities ParseLMStudioModelCapabilities(dynamic modelData) } } - // --- Helper Methods (Defined locally again) --- - - public bool IsSameSender(Model.Data.Message message1, Model.Data.Message message2) { - if (message1 == null || message2 == null) return false; - bool msg1IsUser = message1.FromUserId != Env.BotId; - bool msg2IsUser = message2.FromUserId != Env.BotId; - return msg1IsUser == msg2IsUser; - } - - private void AddMessageToHistory(List ChatHistory, long fromUserId, string content) { - AddMessageToHistory(ChatHistory, fromUserId, content, null); - } - - private void AddMessageToHistory(List ChatHistory, long fromUserId, string content, List images) { - if (string.IsNullOrWhiteSpace(content) && ( images == null || images.Count == 0 )) return; - if (!string.IsNullOrWhiteSpace(content)) { - content = System.Text.RegularExpressions.Regex.Replace(content.Trim(), @"\n{3,}", "\n\n"); - } - - if (fromUserId == Env.BotId) { - ChatHistory.Add(new AssistantChatMessage(content?.Trim() ?? "")); - } else { - if (images != null && images.Count > 0) { - var parts = new List(); - if (!string.IsNullOrWhiteSpace(content)) { - parts.Add(ChatMessageContentPart.CreateTextPart(content.Trim())); - } - foreach (var imageBytes in images) { - parts.Add(ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(imageBytes), "image/png")); - } - ChatHistory.Add(new UserChatMessage(parts)); - } else { - ChatHistory.Add(new UserChatMessage(content.Trim())); - } - } - } - - public async Task> GetChatHistory(long ChatId, List ChatHistory, Model.Data.Message InputToken) { - return await GetChatHistory(ChatId, ChatHistory, InputToken, false); - } - - public async Task> GetChatHistory(long ChatId, List ChatHistory, Model.Data.Message InputToken, bool supportsVision) { - var Messages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == ChatId && m.DateTime > DateTime.UtcNow.AddHours(-1)) - .OrderBy(m => m.DateTime) - .ToListAsync(); - if (Messages.Count < 10) { - Messages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == ChatId) - .OrderByDescending(m => m.DateTime) - .Take(10) - .OrderBy(m => m.DateTime) - .ToListAsync(); - } - - if (_llmVisibilityService != null) { - Messages = await _llmVisibilityService.FilterVisibleMessagesAsync(ChatId, Messages); - } - - if (InputToken != null && - ( _llmVisibilityService == null || - !await _llmVisibilityService.IsUserInvisibleAsync(ChatId, InputToken.FromUserId) )) { - Messages.Add(InputToken); - } - _logger.LogInformation($"OpenAI GetChatHistory: Found {Messages.Count} messages for ChatId {ChatId}."); - - var str = new StringBuilder(); - Model.Data.Message previous = null; - var userCache = new Dictionary(); - var pendingImages = new List(); - - foreach (var message in Messages) { - if (previous == null && !ChatHistory.Any(ch => ch is UserChatMessage || ch is AssistantChatMessage) && message.FromUserId.Equals(Env.BotId)) { - previous = message; - continue; - } - - if (previous != null && !IsSameSender(previous, message)) { - AddMessageToHistory(ChatHistory, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - str.Clear(); - pendingImages.Clear(); - } - - str.Append($"[{message.DateTime.ToString("yyyy-MM-dd HH:mm:ss zzz")}]"); - if (message.FromUserId != 0) { - if (!userCache.TryGetValue(message.FromUserId, out var fromUser)) { - fromUser = await _dbContext.UserData.AsNoTracking().FirstOrDefaultAsync(u => u.Id == message.FromUserId); - if (fromUser != null) userCache[message.FromUserId] = fromUser; - } - str.Append(fromUser != null ? $"{fromUser.FirstName} {fromUser.LastName}".Trim() : $"User({message.FromUserId})"); - } else { str.Append("System/Unknown"); } - - if (message.ReplyToMessageId != 0) { - str.Append('('); - // Simplified reply info - str.Append($"Reply to msg {message.ReplyToMessageId}"); - str.Append(')'); - } - str.Append(':').Append(message.Content).Append("\n"); - - // Add message extensions if any - var extensions = await _messageExtensionService.GetByMessageDataIdAsync(message.Id); - if (extensions != null && extensions.Any()) { - str.Append("[扩展信息:"); - foreach (var ext in extensions) { - str.Append($"{ext.Name}={ext.Value}; "); - } - str.Append("]\n"); - } - - // 如果模型支持视觉,尝试加载消息关联的图片 - if (supportsVision && message.FromUserId != Env.BotId) { - var imageBytes = TryLoadMessagePhoto(message.GroupId, message.MessageId); - if (imageBytes != null) { - pendingImages.Add(imageBytes); - } - } - - previous = message; - } - if (previous != null && str.Length > 0) { - AddMessageToHistory(ChatHistory, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - } - return ChatHistory; - } - - /// - /// 尝试加载消息关联的图片文件,转换为PNG格式的字节数组 - /// - private byte[] TryLoadMessagePhoto(long chatId, long messageId) { - try { - var dirPath = Path.Combine(Env.WorkDir, "Photos", $"{chatId}"); - if (!Directory.Exists(dirPath)) return null; - - var files = Directory.GetFiles(dirPath, $"{messageId}.*"); - if (files.Length == 0) return null; - - var filePath = files[0]; - using var fileStream = File.OpenRead(filePath); - var bitmap = SKBitmap.Decode(fileStream); - if (bitmap == null) return null; - - var encoded = bitmap.Encode(SKEncodedImageFormat.Png, 90); - return encoded?.ToArray(); - } catch (Exception ex) { - _logger.LogDebug(ex, "无法加载消息图片: ChatId={ChatId}, MessageId={MessageId}", chatId, messageId); - return null; - } - } - - // ConvertToolResultToString is now in McpToolHelper - - // --- Main Execution Logic --- private async Task CheckVisionSupport(string modelName, int channelId) { try { var channelWithModel = await _dbContext.ChannelsWithModel From 95190e9ed642ee4397b29674ef4a187dd735f346 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 09:50:48 +0800 Subject: [PATCH 18/30] refactor(llm): unify ModelCapabilityService and LlmServiceProxy provider resolution onto LlmProviderRegistry --- .../AI/LLM/ModelCapabilityServiceTests.cs | 5 +++- .../Service/AI/LLM/ModelCapabilityService.cs | 23 ++++++-------- .../Service/LlmServiceProxy.cs | 30 +++++++++---------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs index 7397fca3..cb304575 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs @@ -21,6 +21,7 @@ public class ModelCapabilityServiceTests { private readonly Mock> _loggerMock; private readonly Mock _serviceProviderMock; private readonly Mock _openAIServiceMock; + private readonly Mock _registryMock = new((IServiceProvider)null); private readonly ModelCapabilityService _service; public ModelCapabilityServiceTests() { @@ -39,11 +40,13 @@ public ModelCapabilityServiceTests() { new Mock().Object); _serviceProviderMock.Setup(sp => sp.GetService(typeof(OpenAIService))) .Returns(_openAIServiceMock.Object); + _registryMock.Setup(r => r.GetProvider(It.IsAny())) + .Returns(_openAIServiceMock.Object); _service = new ModelCapabilityService( _loggerMock.Object, _dbContext, - _serviceProviderMock.Object); + _registryMock.Object); } [Fact] diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs index a78858e5..f308d4cc 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs @@ -22,15 +22,15 @@ public class ModelCapabilityService : IModelCapabilityService, IService { private readonly ILogger _logger; private readonly DataDbContext _dbContext; - private readonly IServiceProvider _serviceProvider; + private readonly LlmProviderRegistry _registry; public ModelCapabilityService( ILogger logger, DataDbContext dbContext, - IServiceProvider serviceProvider) { + LlmProviderRegistry registry) { _logger = logger; _dbContext = dbContext; - _serviceProvider = serviceProvider; + _registry = registry; } /// @@ -279,19 +279,14 @@ private string GetCapabilityDescription(string capabilityName) { } /// - /// 根据提供商获取对应的LLM服务 + /// 根据提供商获取对应的LLM服务;未知提供商返回 null(保持原有的降级语义)。 /// private ILlmProvider GetProvider(LLMProvider provider) { - return provider switch { - LLMProvider.OpenAI => _serviceProvider.GetService(typeof(OpenAIService)) as ILlmProvider, - LLMProvider.Ollama => _serviceProvider.GetService(typeof(OllamaService)) as ILlmProvider, - LLMProvider.Gemini => _serviceProvider.GetService(typeof(GeminiService)) as ILlmProvider, - LLMProvider.MiniMax => _serviceProvider.GetService(typeof(OpenAIService)) as ILlmProvider, - LLMProvider.LMStudio => _serviceProvider.GetService(typeof(OpenAIService)) as ILlmProvider, - LLMProvider.Anthropic => _serviceProvider.GetService(typeof(AnthropicService)) as ILlmProvider, - LLMProvider.ResponsesAPI => _serviceProvider.GetService(typeof(OpenAIResponsesService)) as ILlmProvider, - _ => null - }; + try { + return _registry.GetProvider(provider); + } catch (KeyNotFoundException) { + return null; + } } /// diff --git a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs index 3f41afb4..6f966c0e 100644 --- a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs +++ b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs @@ -11,10 +11,12 @@ namespace TelegramSearchBot.LLMAgent.Service { public sealed class LlmServiceProxy : IAgentTaskExecutor { private readonly IServiceProvider _serviceProvider; + private readonly LlmProviderRegistry _registry; private readonly ILogger _logger; public LlmServiceProxy(IServiceProvider serviceProvider, ILogger logger) { _serviceProvider = serviceProvider; + _registry = serviceProvider.GetRequiredService(); _logger = logger; } @@ -77,25 +79,21 @@ public async IAsyncEnumerable CallAsync( } private ILlmProvider ResolveService(LLMProvider provider) { - return provider switch { - LLMProvider.Ollama => _serviceProvider.GetRequiredService(), - LLMProvider.Gemini => _serviceProvider.GetRequiredService(), - LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), - LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), - _ => _serviceProvider.GetRequiredService() - }; + try { + return _registry.GetProvider(provider); + } catch (KeyNotFoundException) { + // 保持原有降级语义:未知 provider 回退到 OpenAI 兼容路径 + return _registry.GetProvider(LLMProvider.OpenAI); + } } - /// 按 binding 线协议解析 client(与 LlmProviderRegistry.GetProvider(LlmProtocol) 同构)。 + /// 按 binding 线协议解析 client,统一委托给 LlmProviderRegistry;未知协议回退 OpenAI。 private ILlmProvider ResolveService(LlmProtocol protocol) { - return protocol switch { - LlmProtocol.OpenAIChat => _serviceProvider.GetRequiredService(), - LlmProtocol.OpenAIResponses => _serviceProvider.GetRequiredService(), - LlmProtocol.AnthropicMessages => _serviceProvider.GetRequiredService(), - LlmProtocol.Ollama => _serviceProvider.GetRequiredService(), - LlmProtocol.Gemini => _serviceProvider.GetRequiredService(), - _ => _serviceProvider.GetRequiredService() - }; + try { + return _registry.GetProvider(protocol); + } catch (KeyNotFoundException) { + return _registry.GetProvider(LlmProtocol.OpenAIChat); + } } private void ApplyBotIdentity(string botName, long botUserId) { From 3710dc458c978e1d9fc5ee4c30edfd6e36ccab5f Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Fri, 11 Sep 2026 22:58:36 +0800 Subject: [PATCH 19/30] docs(plan): add Phase 4.5 provider-class elimination plan with target architecture and ER diagrams --- Docs/Agent_Architecture_Refactor_Plan.md | 158 ++++++++++++++++++++++- 1 file changed, 156 insertions(+), 2 deletions(-) diff --git a/Docs/Agent_Architecture_Refactor_Plan.md b/Docs/Agent_Architecture_Refactor_Plan.md index 556e7707..65b624a1 100644 --- a/Docs/Agent_Architecture_Refactor_Plan.md +++ b/Docs/Agent_Architecture_Refactor_Plan.md @@ -191,6 +191,101 @@ public sealed class LlmTurnRequest { - 快照 round-trip 测试(native tool_call id 不丢) - worker 路径:task.History 直达 transport,零 DB 触碰(以 mock DbContext 断言) +### Phase 4.5:Provider 类彻底消除(pi 终态,本 PR 或下一 PR) + +Phase 4 之后 5 个 `*Service` 类(~4500 行)仍是聊天胶水 + Provider API 的载体,与 pi 的「provider=数据、dialect=模块」仍有距离。本阶段将其**物理删除**: + +**目标结构** + +``` +TelegramSearchBot.LLM/Service/AI/LLM/ + Transports/ ILlmTransport + 5 实现 + 各自 static Create(channel, binding, …) + ← 吸收 service 胶水:client 构造/端点归一化/MiniMax 判断/ + Opencode headers/prompt-caching 开关/静态助手随迁 + ProviderApi/ ILlmModelCatalog / ILlmEmbeddings / ILlmVision(可选接口) + + 5 个方言实现(目录/向量/图像/健康/Ollama pull) + LlmChatRunner 共享聊天 runner:history 组装 + native/XML 降级 dispatch + 调 LlmToolLoop + (替代 5 份 ExecWithHistoryAsync) + LlmProviderRegistry GetTransport(LlmProtocol, …) + GetCatalog/GetEmbeddings/GetVision(provider) + 删除:OpenAIService/AnthropicService/OpenAIResponsesService/GeminiService/OllamaService、ILlmProvider +``` + +**目标架构图** + +```mermaid +flowchart TB + subgraph CALLERS[调用方] + CTRL[GeneralLLMController] + AGENT[AgentChat 队列/Worker] + OCR[LLMOCRService / AltPhotoController / RefreshService] + FAISS[FaissVectorService] + CONF[EditLLMConfHelper] + CAP[ModelCapabilityService] + end + + subgraph FACADE[GeneralLLMService facade(签名不变)] + EXEC[ExecAsync / ExecWithHistoryAsync / ResumeFromSnapshot] + OP[ExecOperationAsync] + end + + subgraph CORE[共享核心] + RUNNER[LlmChatRunner\nnative/XML 降级 dispatch] + LOOP[LlmToolLoop\n唯一 agent 循环] + HIST[LlmHistoryQueryService\nLlmHistoryProjector\nLlmPhotoLoader] + SNAP[LlmContinuationService\nRedis 快照] + end + + subgraph REG[LlmProviderRegistry] + GT[GetTransport(protocol, config)] + GC[GetCatalog / GetEmbeddings / GetVision(provider)] + end + + subgraph TRANSPORTS[Transports/ — 方言=接口] + T1[OpenAiChatTransport\n+MiniMax/LMStudio/OpenCode 分支] + T2[AnthropicMessagesTransport] + T3[OpenAIResponsesTransport] + T4[GeminiTransport] + T5[OllamaTransport] + end + + subgraph API[ProviderApi/ — provider 数据面] + A1[OpenAiModelApi\n含 MiniMax 内部目录] + A2[AnthropicModelApi] + A3[ResponsesModelApi] + A4[GeminiModelApi\n能力推断] + A5[OllamaModelApi\n含 CheckAndPullModel] + end + + CTRL --> FACADE + AGENT -->|纯 DTO 历史| RUNNER + FACADE --> RUNNER + FACADE --> OP --> GC + OCR --> FACADE + FAISS --> FACADE + CONF --> GC + CAP --> GC + + RUNNER --> GT --> T1 & T2 & T3 & T4 & T5 + RUNNER --> LOOP + LOOP -.per-turn.-> TRANSPORTS + RUNNER --> HIST + RUNNER --> SNAP + GC --> A1 & A2 & A3 & A4 & A5 +``` + +**执行序列(每步编译+测试绿+提交)** + +| 步 | 内容 | 规模 | +|---|---|---| +| S1 | 新建 `LlmChatRunner`(native/XML 降级 dispatch 集中一份,替代 5 份重复) | ~200 行新 | +| S2-S6 | 每 transport 加 `static Create`,胶水+静态助手迁入,service 降级为两行转发 | 5×~250 行搬迁 | +| S7-S9 | 目录/向量/视觉/健康 → `ProviderApi/` 方言类 + registry 三种 Get | ~1100 行搬迁 | +| S10 | facade 收编:message 路径直接走 `LlmHistoryQueryService` + runner;`Func` → 能力接口 | ~150 行改写 | +| S11 | **删除 5 个 service 文件 + ILlmProvider**;测试改 mock 对象;wire 测试直接 new transport | 净删 ~2500 行 | +| S12 | push、CI 确认、PR 描述与文档更新 | — | + +**风险与校验**:OpenCode wire 测试只换构造点(transport 代码不动 → 格式锁定不受影响);native/XML 双路径 dispatch 等价迁移靠 `LlmToolLoopTests` + wire 测试兜底;`BotName`/`CheckVisionSupport` 下沉共享助手;`Mock` 4 参构造的测试逐个改 mock 新接口。 + ### Phase 5:单入口 + 状态收敛(独立 PR) **范围** @@ -211,6 +306,64 @@ Phase 4 完成后 worker 里只剩「BRPOP + 调 transport」~200 行,届时 --- +## 七、LLM 领域 ER 图(当前表结构 = 目标表结构) + +Schema 不变,迁移即语义对齐。SQLite 实体表如下;`LlmContinuationSnapshot` 只存 Redis(24h TTL),`AgentHistoryMessage`/`LlmMessage` 为纯 DTO 不落表。 + +```mermaid +erDiagram + LLMChannel ||--o{ ChannelWithModel : "Models" + LLMChannel ||--o{ LLMApiBinding : "Bindings" + ChannelWithModel |o--o| LLMApiBinding : "ApiBindingId?" + ChannelWithModel ||--o{ ModelCapability : "Capabilities" + + LLMChannel { + int Id PK + string Name + string Gateway + string ApiKey + LLMProvider Provider "enum: OpenAI/Anthropic/Gemini/Ollama/MiniMax/LMStudio/ResponsesAPI" + int Parallel "并发限制" + int Priority "路由排序" + } + + ChannelWithModel { + int Id PK + int LLMChannelId FK + string ModelName "如 gpt-4o / claude-4-sonnet" + int ApiBindingId FK "nullable,可继承 channel 默认" + bool IsDeleted "软删" + AuthorizationSource AuthorizationSource "Manual/Binding" + bool IsPreferred "默认模型偏好" + } + + LLMApiBinding { + int Id PK + int LLMChannelId FK + string Endpoint "绑定端点(可覆盖 Gateway)" + LlmProtocol Protocol "enum: OpenAIChat/AnthropicMessages/OpenAIResponses/Gemini/Ollama" + LlmAuthProfile AuthProfile + bool IsDefault "同名模型多绑定时默认走此绑定" + } + + ModelCapability { + int Id PK + int ChannelWithModelId FK + string CapabilityName "如 vision/tools/prompt_cache" + string CapabilityValue + string Description + datetime LastUpdated + } +``` + +**关键语义** +- `LLMChannel.Provider` 决定预设渠道与 ProviderApi(目录/向量/视觉);`LLMApiBinding.Protocol` 决定聊天走的 wire 方言(Transport)——两者可不同源(如 OpenAI 渠道绑 Anthropic 协议端点) +- `ChannelWithModel.ApiBindingId` 为空 = 用渠道默认端点;非空 = 该模型行走指定协议绑定 +- 路由(`ResolvedLlmRoute`)= 依 `ModelName` 找 `ChannelWithModel` 行 → binding 优先、否则 channel,`IsDefault`+`Priority` 破平 +- 编辑渠道的预设流程(`presetId` / `presetId|gateway`)写入的就是这三张表,预设只是元数据,不自动建行 + +--- + ## 五、兼容性与风险 | 风险 | 缓解 | @@ -223,13 +376,14 @@ Phase 4 完成后 worker 里只剩「BRPOP + 调 transport」~200 行,届时 --- -## 六、执行顺序 +## 八、执行顺序 ``` [PR #390 - 本分支] P1 共享循环(已完成) P2 预设(已完成) - P3+4 整体重写:LlmMessage → 投影 → 循环 v2 → 5 transports → registry → 删旧 ← 下一步 + P3+4 整体重写:LlmMessage → 投影 → 循环 v2 → 5 transports → registry → 删旧(已完成,CI 绿) + P4.5 provider 类彻底删除:LlmChatRunner → Transport.Create → ProviderApi → 删 5 个 service ← 下一步 [PR #391] P5 单入口 + Redis 状态收敛 From 99b7052f23dab79b024e20ef05948f95ff266c09 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:03:14 +0800 Subject: [PATCH 20/30] feat(llm): add LlmChatRunner + per-transport Create factories (S1+S2 start of Phase 4.5); OpenAI ExecWithHistoryAsync delegates to runner --- .../Service/AI/LLM/LlmChatRunner.cs | 204 ++++++++++++++++++ .../Service/AI/LLM/LlmProviderRegistry.cs | 42 ++++ .../Service/AI/LLM/LlmTransportTypes.cs | 3 + .../Service/AI/LLM/OpenAIService.cs | 49 +---- .../Transports/AnthropicMessagesTransport.cs | 34 +++ .../AI/LLM/Transports/GeminiTransport.cs | 19 ++ .../AI/LLM/Transports/OllamaTransport.cs | 61 ++++++ .../Transports/OpenAIResponsesTransport.cs | 20 ++ .../AI/LLM/Transports/OpenAiChatTransport.cs | 53 ++++- 9 files changed, 433 insertions(+), 52 deletions(-) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs new file mode 100644 index 00000000..aa230416 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs @@ -0,0 +1,204 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using System.ClientModel; +using System.Net.Http; +using OpenAI.Chat; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Interface; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Attributes; +using Microsoft.Extensions.DependencyInjection; +using TelegramSearchBot.Model.Data; +using TelegramSearchBot.Service.AI.LLM.Transports; + +namespace TelegramSearchBot.Service.AI.LLM { + /// + /// The single chat-runner: validates config, resolves the dialect transport via + + /// , owns the native-tools → XML-protocol fallback + /// dispatch, and hands off to . Replaces the per-service + /// ExecWithHistoryAsync/ResumeFromSnapshot glue. + /// + [Injectable(ServiceLifetime.Singleton)] + public class LlmChatRunner { + private readonly IHttpClientFactory _httpClientFactory; + private readonly ILogger _logger; + private readonly LlmProviderRegistry _registry; + private readonly IBotIdentityProvider _botIdentityProvider; + private readonly PromptCachingSettingsService _promptCachingSettingsService; + private string _fallbackBotName = string.Empty; + + public LlmChatRunner( + IHttpClientFactory httpClientFactory, + ILogger logger, + LlmProviderRegistry registry, + IBotIdentityProvider botIdentityProvider = null, + PromptCachingSettingsService promptCachingSettingsService = null) { + _httpClientFactory = httpClientFactory; + _logger = logger; + _registry = registry; + _botIdentityProvider = botIdentityProvider; + _promptCachingSettingsService = promptCachingSettingsService; + } + + /// Same contract as GeneralLLMService.BotName setter: seeded from bot config at startup. + public string BotName { + set => _fallbackBotName = value ?? string.Empty; + } + + private async Task GetBotNameAsync() { + if (_botIdentityProvider == null) { + return _fallbackBotName; + } + var identity = await _botIdentityProvider.GetIdentityAsync(); + return identity.UserName ?? string.Empty; + } + + private async Task IsPromptCachingEnabledAsync() { + return _promptCachingSettingsService == null || await _promptCachingSettingsService.IsEnabledAsync(); + } + + /// + /// Executes one agent chat turn for caller-provided history rows (agent/worker path). + /// + public async IAsyncEnumerable RunAsync( + IReadOnlyList history, + Model.Data.Message message, long chatId, string modelName, LLMChannel channel, + LLMApiBinding binding, LlmExecutionContext executionContext, + bool supportsVision, + [EnumeratorCancellation] CancellationToken cancellationToken = default) { + if (string.IsNullOrWhiteSpace(modelName)) modelName = DefaultModelFor(channel); + + if (string.IsNullOrWhiteSpace(modelName)) { + _logger.LogError("LlmChatRunner: model name is not configured (provider {Provider}).", channel.Provider); + yield return "Error: model name is not configured."; + yield break; + } + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { + _logger.LogError("LlmChatRunner: channel/gateway/apikey is not configured (provider {Provider}).", channel.Provider); + yield return "Error: channel/gateway/apikey is not configured."; + yield break; + } + + var nativeTools = McpToolHelper.GetNativeToolDefinitions(); + var useNativeToolCalling = nativeTools is { Count: > 0 }; + var botName = await GetBotNameAsync(); + var promptCachingEnabled = await IsPromptCachingEnabledAsync(); + + if (useNativeToolCalling) { + var nativeSystemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, chatId); + var nativeBundle = await CreateTransport(channel, binding, modelName, chatId, nativeTools: true, promptCachingEnabled, supportsVision, nativeSystemPrompt); + var nativeEnumerator = RunTurnAsync(nativeBundle, history, message, chatId, modelName, channel, executionContext, + nativeSystemPrompt, + tools: McpToolHelper.GetLlmToolSpecs(), + supportsVision, cancellationToken); + await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); + bool hasFirst = false; + bool nativeFailed = false; + try { + hasFirst = await enumerator.MoveNextAsync(); + } catch (Exception ex) when (IsToolCallingNotSupportedError(ex)) { + _logger.LogInformation("LlmChatRunner: native tool calling not supported for model {Model}, falling back to XML tool protocol. Error: {Error}", modelName, ex.Message); + nativeFailed = true; + } + + if (!nativeFailed) { + if (hasFirst) { + yield return enumerator.Current; + while (await enumerator.MoveNextAsync()) { + yield return enumerator.Current; + } + } + yield break; + } + } + + var xmlBundle = await CreateTransport(channel, binding, modelName, chatId, nativeTools: false, promptCachingEnabled, supportsVision, + McpToolHelper.FormatSystemPrompt(botName, chatId)); + await foreach (var item in RunTurnAsync(xmlBundle, history, message, chatId, modelName, channel, executionContext, + McpToolHelper.FormatSystemPrompt(await GetBotNameAsync(), chatId), + tools: null, supportsVision, cancellationToken)) { + yield return item; + } + } + + private async IAsyncEnumerable RunTurnAsync( + LlmTransportBundle bundle, + IReadOnlyList rows, + Model.Data.Message message, long chatId, string modelName, LLMChannel channel, + LlmExecutionContext executionContext, + string systemPrompt, + IReadOnlyList tools, + bool supportsVision, + [EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); + var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); + + var run = new LlmAgentRunRequest { + Transport = bundle.Transport, + SystemPrompt = systemPrompt, + History = history, + Tools = tools, + Config = bundle.Config, + ToolContext = new ToolContext { ChatId = chatId, UserId = message.FromUserId, MessageId = message.MessageId }, + Meta = new LlmToolLoopMeta { + ChatId = chatId, + OriginalMessageId = message.MessageId, + UserId = message.FromUserId, + ModelName = modelName, + Provider = ProviderLabel(channel), + ChannelId = channel.Id + }, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + yield return item; + } + } + + private async Task CreateTransport(LLMChannel channel, LLMApiBinding binding, string modelName, long chatId, + bool nativeTools, bool promptCachingEnabled, bool supportsVision, string systemPrompt) { + return await _registry.GetTransport(channel, binding, modelName, chatId, nativeTools, promptCachingEnabled, supportsVision, systemPrompt, _logger, _httpClientFactory); + } + + private static string ProviderLabel(LLMChannel channel) { + if (channel.Provider == LLMProvider.OpenAI && channel.Gateway != null && + OpenAIService.IsMiniMaxCompatibleEndpoint(channel, string.Empty)) { + return "MiniMax"; + } + return channel.Provider.ToString(); + } + + internal static string DefaultModelFor(LLMChannel channel) { + return channel.Provider switch { + LLMProvider.Anthropic => "claude-sonnet-4-20250514", + LLMProvider.Gemini => "gemini-1.5-flash", + _ => Env.OpenAIModelName + }; + } + + internal static bool IsToolCallingNotSupportedError(Exception ex) { + var message = ex.Message ?? ""; + if (ex is ClientResultException clientEx) { + if (clientEx.Status == 400 && message.Contains("tool", StringComparison.OrdinalIgnoreCase)) { + return true; + } + } + return message.Contains("tools", StringComparison.OrdinalIgnoreCase) && + ( message.Contains("not supported", StringComparison.OrdinalIgnoreCase) || + message.Contains("unsupported", StringComparison.OrdinalIgnoreCase) || + message.Contains("invalid", StringComparison.OrdinalIgnoreCase) ) || + message.Contains("unrecognized request argument", StringComparison.OrdinalIgnoreCase); + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs index 3ffd7690..e763d582 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs @@ -2,6 +2,9 @@ using System.Collections.Generic; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.Net.Http; +using TelegramSearchBot.Model.Data; +using Microsoft.Extensions.Logging; using TelegramSearchBot.Attributes; using TelegramSearchBot.Interface.AI.LLM; using TelegramSearchBot.Model.AI; @@ -48,5 +51,44 @@ public virtual ILlmProvider GetProvider(ResolvedLlmRoute route) { ? GetProvider(route.Binding.Protocol) : GetProvider(route.Channel.Provider); } + + /// + /// Resolves the chat wire transport for a channel/binding pair. Binding protocol wins + /// over channel provider (e.g. OpenAI channel on an Anthropic-protocol binding). + /// Async because Ollama may pull the model on first use. + /// + public async Task GetTransport(LLMChannel channel, LLMApiBinding binding, string modelName, long chatId, + bool nativeTools, bool promptCachingEnabled, bool supportsVision, string systemPrompt, + Microsoft.Extensions.Logging.ILogger logger, System.Net.Http.IHttpClientFactory httpClientFactory) { + var protocol = binding?.Protocol ?? channel.Provider switch { + LLMProvider.OpenAI => LlmProtocol.OpenAIChat, + LLMProvider.MiniMax => LlmProtocol.OpenAIChat, + LLMProvider.LMStudio => LlmProtocol.OpenAIChat, + LLMProvider.ResponsesAPI => LlmProtocol.OpenAIResponses, + LLMProvider.Anthropic => LlmProtocol.AnthropicMessages, + LLMProvider.Ollama => LlmProtocol.Ollama, + LLMProvider.Gemini => LlmProtocol.Gemini, + _ => LlmProtocol.OpenAIChat + }; + + switch (protocol) { + case LlmProtocol.OpenAIResponses: + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + return Transports.ResponsesTransport.Create(channel, binding, modelName, endpoint, apiKey, + promptCachingEnabled, supportsVision, logger, httpClientFactory); + case LlmProtocol.AnthropicMessages: + return Transports.AnthropicMessagesTransport.Create(channel, binding, modelName, systemPrompt, + nativeTools, promptCachingEnabled, logger); + case LlmProtocol.Gemini: + return Transports.GeminiTransport.Create(channel, binding, modelName, supportsVision, logger, httpClientFactory); + case LlmProtocol.Ollama: + return await Transports.OllamaTransport.CreateAsync(channel, binding, modelName, systemPrompt, logger, httpClientFactory); + case LlmProtocol.OpenAIChat: + default: + return Transports.OpenAiChatTransport.Create(channel, binding, modelName, chatId, + nativeTools, promptCachingEnabled, logger, httpClientFactory); + } + } } } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs index 87de423e..06a7b234 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs @@ -2,6 +2,9 @@ using TelegramSearchBot.Model.Data; namespace TelegramSearchBot.Service.AI.LLM { + /// A transport instance plus the turn config it was built for. + public sealed record LlmTransportBundle(ILlmTransport Transport, LlmTransportConfig Config); + /// Transport identity + credentials + quirks, resolved from LLMChannel + LLMApiBinding. public sealed class LlmTransportConfig { public string ModelName { get; set; } = string.Empty; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs index b6780067..a888ddbe 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs @@ -128,6 +128,7 @@ private static string SanitizeAndTruncateArguments(string arguments, int maxChar private readonly IGroupLlmSettingsService _groupLlmSettingsService; private readonly LlmVisibilityService _llmVisibilityService; private readonly PromptCachingSettingsService _promptCachingSettingsService; + private readonly LlmChatRunner _chatRunner; private string _fallbackBotName = string.Empty; public OpenAIService( @@ -146,7 +147,8 @@ public OpenAIService( IBotIdentityProvider botIdentityProvider, IGroupLlmSettingsService groupLlmSettingsService, LlmVisibilityService llmVisibilityService = null, - PromptCachingSettingsService promptCachingSettingsService = null) { + PromptCachingSettingsService promptCachingSettingsService = null, + LlmChatRunner chatRunner = null) { _logger = logger; _dbContext = context; _messageExtensionService = messageExtensionService; @@ -155,6 +157,7 @@ public OpenAIService( _groupLlmSettingsService = groupLlmSettingsService; _llmVisibilityService = llmVisibilityService; _promptCachingSettingsService = promptCachingSettingsService; + _chatRunner = chatRunner; _logger.LogInformation("OpenAIService instance created. McpToolHelper should be initialized at application startup."); } @@ -997,48 +1000,8 @@ public async IAsyncEnumerable ExecWithHistoryAsync( LLMApiBinding binding, LlmExecutionContext executionContext, bool supportsVision, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; - - if (string.IsNullOrWhiteSpace(modelName)) { - _logger.LogError("{ServiceName}: Model name is not configured.", ServiceName); - yield return $"Error: {ServiceName} model name is not configured."; - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/gateway/apikey is not configured."; - yield break; - } - - var nativeTools = McpToolHelper.GetNativeToolDefinitions(); - var useNativeToolCalling = nativeTools is { Count: > 0 }; - - if (useNativeToolCalling) { - bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); - await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); - bool hasFirst = false; - try { - hasFirst = await enumerator.MoveNextAsync(); - } catch (Exception ex) when (IsToolCallingNotSupportedError(ex)) { - _logger.LogInformation("{ServiceName}: Native tool calling not supported for model {Model}, falling back to XML prompt-based tool calling. Error: {Error}", ServiceName, modelName, ex.Message); - nativeFailed = true; - } - - if (!nativeFailed) { - if (hasFirst) { - yield return enumerator.Current; - while (await enumerator.MoveNextAsync()) { - yield return enumerator.Current; - } - } - yield break; - } - } - - await foreach (var item in ExecWithXmlToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { + // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. + await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { yield return item; } } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs index b558290d..da24accb 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs @@ -20,6 +20,40 @@ namespace TelegramSearchBot.Service.AI.LLM.Transports { public sealed class AnthropicMessagesTransport : ILlmTransport { + /// + /// Builds the transport from a channel/binding pair. Absorbs the client + /// construction glue formerly in AnthropicService.CreateClient. + /// + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, string systemPrompt, + bool nativeTools, bool promptCachingEnabled, ILogger logger) { + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + var options = new Anthropic.Core.ClientOptions { + ApiKey = apiKey, + }; + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + if (!string.IsNullOrWhiteSpace(endpoint)) { + // Binding URL 已含 /v1(如 https://opencode.ai/zen/v1),SDK 会再追加 /v1/messages; + // 剥离尾部 /v1 使 SDK 追加后命中精确 binding 路径。legacy channel.Gateway 保持字节一致。 + var trimmed = endpoint.TrimEnd('/'); + if (binding != null && trimmed.EndsWith("/v1", StringComparison.OrdinalIgnoreCase)) { + trimmed = trimmed.Substring(0, trimmed.Length - 3); + } + options.BaseUrl = trimmed; + } + var transport = new AnthropicMessagesTransport(logger, new AnthropicClient(options), systemPrompt, modelName, channel, + nativeTools, promptCachingEnabled); + var config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + PromptCachingEnabled = promptCachingEnabled + }; + return new LlmTransportBundle(transport, config); + } + private readonly ILogger _logger; private readonly AnthropicClient _client; private readonly string _systemPrompt; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs index 39d350ea..f6a392ab 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs @@ -11,11 +11,30 @@ using TelegramSearchBot.Common; using TelegramSearchBot.Interface.AI.LLM; using TelegramSearchBot.Model; +using System.Net.Http; using TelegramSearchBot.Model.AI; using TelegramSearchBot.Model.Data; namespace TelegramSearchBot.Service.AI.LLM.Transports { public sealed class GeminiTransport : ILlmTransport { + /// Builds the transport from a channel/binding pair (Gemini uses the channel ApiKey directly). + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, + bool supportsVision, ILogger logger, IHttpClientFactory httpClientFactory) { + var googleAI = new GoogleAi(channel.ApiKey, client: httpClientFactory.CreateClient()); + var model = googleAI.CreateGenerativeModel("models/" + modelName); + var transport = new GeminiTransport(model, supportsVision); + var config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding), + ApiKey = channel.ApiKey, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision + }; + return new LlmTransportBundle(transport, config); + } + private readonly GenerativeModel _model; private readonly bool _supportsVision; private ChatSession? _chatSession; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs index e1c441c9..0ce50fa4 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs @@ -5,6 +5,8 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using System.Net.Http; +using Microsoft.Extensions.Logging; using OllamaSharp; using OllamaSharp.Models; using OllamaSharp.Models.Chat; @@ -16,6 +18,65 @@ namespace TelegramSearchBot.Service.AI.LLM.Transports { public sealed class OllamaTransport : ILlmTransport { + /// + /// Builds the transport from a channel/binding pair; pulls the model locally if missing. + /// + public static async Task CreateAsync(LLMChannel channel, LLMApiBinding binding, string modelName, + string systemPrompt, ILogger logger, IHttpClientFactory httpClientFactory) { + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + // ponytail: "OllamaClient" named client may be unregistered in some hosts; fall back to a fresh client. + HttpClient httpClient = httpClientFactory?.CreateClient("OllamaClient") ?? new HttpClient(); + httpClient.BaseAddress = new Uri(endpoint); + var ollama = new OllamaApiClient(httpClient, modelName); + + if (!await CheckAndPullModelAsync(ollama, modelName, logger)) { + throw new InvalidOperationException($"Ollama model {modelName} is not available locally and could not be pulled."); + } + ollama.SelectedModel = modelName; + + var transport = new OllamaTransport(logger, ollama, systemPrompt); + var config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + Provider = channel.Provider, + Binding = binding, + Channel = channel + }; + return new LlmTransportBundle(transport, config); + } + + /// Ensures the model exists locally, pulling it otherwise. Moved from OllamaService. + public static async Task CheckAndPullModelAsync(OllamaApiClient ollama, string modelName, ILogger logger) { + logger.LogInformation("Checking for Ollama model: {ModelName}", modelName); + try { + var models = await ollama.ListLocalModelsAsync(); + if (models.Any(m => m.Name.Equals(modelName, StringComparison.OrdinalIgnoreCase) || m.Name.StartsWith(modelName + ":", StringComparison.OrdinalIgnoreCase))) { + logger.LogInformation("Model {ModelName} found locally.", modelName); + return true; + } + + logger.LogInformation("Model {ModelName} not found locally. Pulling...", modelName); + + await foreach (var status in ollama.PullModelAsync(modelName, System.Threading.CancellationToken.None)) { + if (status != null) { + logger.LogInformation("[{ModelName}] Pulling model {Percent}% - {Status}", modelName, status.Percent, status.Status); + } + } + logger.LogInformation("Model {ModelName} pull stream completed.", modelName); + + var modelsAfterPull = await ollama.ListLocalModelsAsync(); + if (!modelsAfterPull.Any(m => m.Name.Equals(modelName, StringComparison.OrdinalIgnoreCase) || m.Name.StartsWith(modelName + ":", StringComparison.OrdinalIgnoreCase))) { + logger.LogError("Model {ModelName} still not found after pull attempt.", modelName); + return false; + } + logger.LogInformation("Model {ModelName} confirmed present after pull.", modelName); + return true; + } catch (Exception ex) { + logger.LogError(ex, "Error checking or pulling Ollama model {ModelName}", modelName); + return false; + } + } + private readonly ILogger _logger; private readonly OllamaApiClient _ollama; private readonly string _systemPrompt; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs index 2a4326a3..1b317f81 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs @@ -21,6 +21,26 @@ namespace TelegramSearchBot.Service.AI.LLM.Transports { public sealed class ResponsesTransport : ILlmTransport { + /// + /// Builds the transport from a channel/binding pair (endpoint/apikey already resolved). + /// + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, string endpoint, string apiKey, + bool promptCachingEnabled, bool supportsVision, ILogger logger, IHttpClientFactory httpClientFactory) { + var transport = new ResponsesTransport(httpClientFactory, logger, endpoint, apiKey, binding, channel, + supportsVision, promptCachingEnabled); + var config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + SupportsVision = supportsVision, + PromptCachingEnabled = promptCachingEnabled + }; + return new LlmTransportBundle(transport, config); + } + private readonly IHttpClientFactory _httpClientFactory; private readonly ILogger _logger; private readonly string _endpoint; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs index 12b78cf5..4555aa71 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs @@ -9,6 +9,7 @@ using OpenAI.Chat; using System.ClientModel; using System.ClientModel.Primitives; +using System.Net.Http; using TelegramSearchBot.Common; using TelegramSearchBot.Interface.AI.LLM; using TelegramSearchBot.Model; @@ -28,15 +29,49 @@ public class ToolCallAccumulator { /// normalized history into SDK messages per turn and streams one assistant turn. /// public sealed class OpenAiChatTransport : ILlmTransport { - private readonly ILogger _logger; - private readonly HttpClientPipelineTransport _pipelineTransport; - private readonly ApiKeyCredential _credential; - private readonly OpenAIClientOptions _clientOptions; - private readonly string _modelName; - private readonly LLMChannel _channel; - private readonly bool _nativeTools; - private readonly bool _promptCachingEnabled; - private readonly bool _includeEmptyReasoningContent; + /// + /// Builds the transport plus its turn config from a channel/binding pair. + /// Absorbs the client-construction glue formerly in OpenAIService.BuildClientParts. + /// + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, long chatId, + bool nativeTools, bool promptCachingEnabled, ILogger logger, IHttpClientFactory httpClientFactory) { + var endpoint = OpenAIService.NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + var includeEmptyReasoningContent = binding == null && OpenAIService.ShouldIncludeEmptyReasoningContent(channel, modelName); + + // ponytail: HttpClient must outlive this method (transport persists per run); factory-managed, not disposed here. + var httpClient = httpClientFactory.CreateClient(); + OpencodeSessionHeaders.Apply(httpClient, channel, binding, $"tsb-{chatId}"); + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(endpoint), + Transport = new HttpClientPipelineTransport(httpClient), + }; + var transport = new OpenAiChatTransport(logger, new HttpClientPipelineTransport(httpClient), + new ApiKeyCredential(apiKey), clientOptions, modelName, channel, + nativeTools, promptCachingEnabled, includeEmptyReasoningContent); + + var config = new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Binding = binding, + Channel = channel, + PromptCachingEnabled = promptCachingEnabled, + IncludeEmptyReasoningContent = includeEmptyReasoningContent + }; + return new LlmTransportBundle(transport, config); + } + + private readonly ILogger _logger; + private readonly HttpClientPipelineTransport _pipelineTransport; + private readonly ApiKeyCredential _credential; + private readonly OpenAIClientOptions _clientOptions; + private readonly string _modelName; + private readonly LLMChannel _channel; + private readonly bool _nativeTools; + private readonly bool _promptCachingEnabled; + private readonly bool _includeEmptyReasoningContent; public OpenAiChatTransport(ILogger logger, HttpClientPipelineTransport pipelineTransport, ApiKeyCredential credential, OpenAIClientOptions clientOptions, string modelName, LLMChannel channel, From 1fd6458de4291eaee8a81e73327cf2648c721af4 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:04:13 +0800 Subject: [PATCH 21/30] refactor(llm): Anthropic ExecWithHistoryAsync delegates to LlmChatRunner --- .../Service/AI/LLM/AnthropicService.cs | 45 +++---------------- 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs index 782713a1..36603f0f 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs @@ -35,6 +35,7 @@ public class AnthropicService : IService, ILlmProvider { private readonly IBotIdentityProvider _botIdentityProvider; private readonly LlmVisibilityService _llmVisibilityService; private readonly PromptCachingSettingsService _promptCachingSettingsService; + private readonly LlmChatRunner _chatRunner; private string _fallbackBotName = string.Empty; public string BotName { @@ -73,7 +74,8 @@ public AnthropicService( IHttpClientFactory httpClientFactory, IBotIdentityProvider botIdentityProvider, LlmVisibilityService llmVisibilityService = null, - PromptCachingSettingsService promptCachingSettingsService = null) { + PromptCachingSettingsService promptCachingSettingsService = null, + LlmChatRunner chatRunner = null) { _logger = logger; _dbContext = context; _messageExtensionService = messageExtensionService; @@ -81,6 +83,7 @@ public AnthropicService( _botIdentityProvider = botIdentityProvider; _llmVisibilityService = llmVisibilityService; _promptCachingSettingsService = promptCachingSettingsService; + _chatRunner = chatRunner; _logger.LogInformation("AnthropicService instance created. McpToolHelper should be initialized at application startup."); } @@ -520,47 +523,11 @@ public async IAsyncEnumerable ExecWithHistoryAsync( LLMApiBinding binding, LlmExecutionContext executionContext, bool supportsVision, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (string.IsNullOrWhiteSpace(modelName)) modelName = "claude-sonnet-4-20250514"; - - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel or ApiKey is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/apikey is not configured."; - yield break; - } - - var nativeTools = McpToolHelper.GetNativeToolDefinitions(); - var useNativeToolCalling = nativeTools is { Count: > 0 }; - - if (useNativeToolCalling) { - bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); - await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); - bool hasFirst = false; - try { - hasFirst = await enumerator.MoveNextAsync(); - } catch (Exception ex) when (IsToolCallingNotSupportedError(ex)) { - _logger.LogInformation("{ServiceName}: Native tool calling not supported for model {Model}, falling back to XML prompt-based tool calling. Error: {Error}", ServiceName, modelName, ex.Message); - nativeFailed = true; - } - - if (!nativeFailed) { - if (hasFirst) { - yield return enumerator.Current; - while (await enumerator.MoveNextAsync()) { - yield return enumerator.Current; - } - } - yield break; - } - } - - await foreach (var item in ExecWithXmlToolCallingAsync(history, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { + // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. + await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { yield return item; } } - private static bool IsToolCallingNotSupportedError(Exception ex) { var message = ex.Message ?? ""; return message.Contains("tools", StringComparison.OrdinalIgnoreCase) && From 6aa4a34d9c638f5d5bb1809f81d5367e1f5e7a70 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:10:16 +0800 Subject: [PATCH 22/30] refactor(llm): OpenAIResponses ExecWithHistoryAsync delegates to LlmChatRunner --- .../Service/AI/LLM/OpenAIResponsesService.cs | 57 ++----------------- 1 file changed, 6 insertions(+), 51 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs index 0db9be85..23685914 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs @@ -66,6 +66,7 @@ public string BotName { private readonly IMessageExtensionService _messageExtensionService; private readonly LlmVisibilityService _llmVisibilityService; private readonly PromptCachingSettingsService _promptCachingSettingsService; + private readonly LlmChatRunner _chatRunner; public OpenAIResponsesService( DataDbContext context, @@ -82,7 +83,8 @@ public OpenAIResponsesService( IHttpClientFactory httpClientFactory, IBotIdentityProvider botIdentityProvider, LlmVisibilityService llmVisibilityService = null, - PromptCachingSettingsService promptCachingSettingsService = null) { + PromptCachingSettingsService promptCachingSettingsService = null, + LlmChatRunner chatRunner = null) { _logger = logger; _dbContext = context; _messageExtensionService = messageExtensionService; @@ -90,6 +92,7 @@ public OpenAIResponsesService( _botIdentityProvider = botIdentityProvider; _llmVisibilityService = llmVisibilityService; _promptCachingSettingsService = promptCachingSettingsService; + _chatRunner = chatRunner; _logger.LogInformation("OpenAIResponsesService instance created."); } @@ -217,59 +220,11 @@ public async IAsyncEnumerable ExecWithHistoryAsync( LLMApiBinding binding, LlmExecutionContext executionContext, bool supportsVision, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/gateway/apikey is not configured."; - yield break; - } - - var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); - - var transport = new Transports.ResponsesTransport(_httpClientFactory, _logger, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); - var botName = await GetBotNameAsync(); - var projected = LlmHistoryProjector.Project(history, supportsVision, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "OpenAIResponses", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId), - History = projected, - Tools = McpToolHelper.GetLlmToolSpecs(), - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = endpoint, - ApiKey = apiKey, - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = supportsVision, - PromptCachingEnabled = promptCachingEnabled - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. + await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { yield return item; } } - - // ======================================================================== - // Resume From Snapshot - // ======================================================================== - public async IAsyncEnumerable ResumeFromSnapshotAsync( LlmContinuationSnapshot snapshot, LLMChannel channel, LlmExecutionContext executionContext, From 9193fe3d0e858c4768849e9549c52e64b0513d85 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:11:11 +0800 Subject: [PATCH 23/30] refactor(llm): Gemini ExecWithHistoryAsync delegates to LlmChatRunner --- .../Service/AI/LLM/GeminiService.cs | 56 +++---------------- 1 file changed, 7 insertions(+), 49 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs index 3e53ade4..5cc0bea8 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs @@ -31,6 +31,7 @@ public class GeminiService : ILlmProvider, IService { private readonly IHttpClientFactory _httpClientFactory; private readonly IBotIdentityProvider _botIdentityProvider; private readonly LlmVisibilityService _llmVisibilityService; + private readonly LlmChatRunner _chatRunner; private string _fallbackBotName = string.Empty; public string BotName { get => GetBotNameAsync().ConfigureAwait(false).GetAwaiter().GetResult(); @@ -55,12 +56,14 @@ public GeminiService( ILogger logger, IHttpClientFactory httpClientFactory, IBotIdentityProvider botIdentityProvider, - LlmVisibilityService llmVisibilityService = null) { + LlmVisibilityService llmVisibilityService = null, + LlmChatRunner chatRunner = null) { _logger = logger; _dbContext = context; _httpClientFactory = httpClientFactory; _botIdentityProvider = botIdentityProvider; _llmVisibilityService = llmVisibilityService; + _chatRunner = chatRunner; _logger.LogInformation("GeminiService instance created"); } @@ -374,60 +377,15 @@ await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, can public async IAsyncEnumerable ExecWithHistoryAsync( IReadOnlyList history, Message message, - long ChatId, - string modelName, - LLMChannel channel, + long ChatId, string modelName, LLMChannel channel, LLMApiBinding binding, LlmExecutionContext executionContext, bool supportsVision, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - if (string.IsNullOrWhiteSpace(modelName)) modelName = "gemini-1.5-flash"; - - var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); - var model = googleAI.CreateGenerativeModel("models/" + modelName); - - if (_llmVisibilityService != null && - message != null && - await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, cancellationToken)) { - yield break; - } - - var botName = await GetBotNameAsync(); - var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - - var transport = new Transports.GeminiTransport(model, supportsVision); - var projected = LlmHistoryProjector.Project(history, supportsVision, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Gemini", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = projected, - Tools = null, - Config = new LlmTransportConfig { - ModelName = modelName, - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = supportsVision - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. + await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { yield return item; } } - public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { if (channel == null || string.IsNullOrWhiteSpace(channel.ApiKey)) { _logger.LogError("{ServiceName}: Channel or ApiKey is not configured", ServiceName); From a9e6003fe7275334adbf4956959c94bcd878bc01 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:11:46 +0800 Subject: [PATCH 24/30] refactor(llm): Ollama ExecWithHistoryAsync delegates to LlmChatRunner; all 5 providers now runner-dispatched --- .../Service/AI/LLM/OllamaService.cs | 65 ++----------------- 1 file changed, 6 insertions(+), 59 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs index 47295b7e..585b9107 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs @@ -35,6 +35,7 @@ public class OllamaService : IService, ILlmProvider { private readonly IServiceProvider _serviceProvider; private readonly IHttpClientFactory _httpClientFactory; private readonly IBotIdentityProvider _botIdentityProvider; + private readonly LlmChatRunner _chatRunner; private string _fallbackBotName = string.Empty; public string BotName { get => GetBotNameAsync().ConfigureAwait(false).GetAwaiter().GetResult(); @@ -61,12 +62,14 @@ public OllamaService( ILogger logger, IServiceProvider serviceProvider, IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider) { + IBotIdentityProvider botIdentityProvider, + LlmChatRunner chatRunner = null) { _logger = logger; _dbContext = context; _serviceProvider = serviceProvider; _httpClientFactory = httpClientFactory; _botIdentityProvider = botIdentityProvider; + _chatRunner = chatRunner; _logger.LogInformation("OllamaService instance created. McpToolHelper should be initialized at application startup."); } @@ -205,67 +208,11 @@ public async IAsyncEnumerable ExecWithHistoryAsync( LLMApiBinding binding, LlmExecutionContext executionContext, bool supportsVision, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - modelName = modelName ?? Env.OllamaModelName; - if (string.IsNullOrWhiteSpace(modelName)) { - _logger.LogError("{ServiceName}: Model name is not configured.", ServiceName); - yield return $"Error: {ServiceName} model name is not configured."; - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint)) { - _logger.LogError("{ServiceName}: Channel or Gateway is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/gateway is not configured."; - yield break; - } - - HttpClient httpClient = _httpClientFactory?.CreateClient("OllamaClient") ?? new HttpClient(); - httpClient.BaseAddress = new Uri(endpoint); - var ollama = new OllamaApiClient(httpClient, modelName); - - if (!await CheckAndPullModelAsync(ollama, modelName)) { - yield return $"Error: Could not check or pull Ollama model '{modelName}'."; - yield break; - } - ollama.SelectedModel = modelName; - - var botName = await GetBotNameAsync(); - var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - - var transport = new Transports.OllamaTransport(_logger, ollama, systemPrompt); - var projected = LlmHistoryProjector.Project(history, supportsVision: false, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Ollama", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = projected, - Tools = null, - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = endpoint, - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = false - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + // Glue (client construction, model pull check, fallback dispatch) lives in LlmChatRunner. + await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { yield return item; } } - public virtual async Task> GetAllModels(LLMChannel channel) { if (channel == null || string.IsNullOrWhiteSpace(channel.Gateway)) { return Enumerable.Empty(); From 78aa84561047d4c479e3105028d26a1419199a1d Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:18:22 +0800 Subject: [PATCH 25/30] feat(llm): capability interfaces ILlmModelCatalog/ILlmEmbeddings/ILlmVision + registry GetCatalog/GetEmbeddings/GetVision (S7) --- .../AI/LLM/ILlmProviderCapabilities.cs | 23 +++++++++++++++++++ .../Service/AI/LLM/AnthropicService.cs | 2 +- .../Service/AI/LLM/GeminiService.cs | 2 +- .../Service/AI/LLM/LlmProviderRegistry.cs | 10 ++++++++ .../Service/AI/LLM/OllamaService.cs | 2 +- .../Service/AI/LLM/OpenAIResponsesService.cs | 2 +- .../Service/AI/LLM/OpenAIService.cs | 2 +- 7 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs new file mode 100644 index 00000000..df7247b0 --- /dev/null +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Interface.AI.LLM { + /// Per-dialect model discovery (list + capability merge). Implemented by OpenAI/MiniMax/LMStudio, Anthropic, Responses, Gemini, Ollama APIs. + public interface ILlmModelCatalog { + Task> GetAllModels(LLMChannel channel); + Task> GetAllModelsWithCapabilities(LLMChannel channel); + } + + /// Embedding generation (OpenAI/Responses/Gemini/Ollama; not Anthropic). + public interface ILlmEmbeddings { + Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel); + } + + /// Vision image analysis (OpenAI-style chat vision; not Anthropic/Ollama). + public interface ILlmVision { + Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null); + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs index 36603f0f..e60b24b8 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs @@ -25,7 +25,7 @@ namespace TelegramSearchBot.Service.AI.LLM { [Injectable(ServiceLifetime.Transient)] - public class AnthropicService : IService, ILlmProvider { + public class AnthropicService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { public string ServiceName => "AnthropicService"; private readonly ILogger _logger; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs index 5cc0bea8..5243a5b0 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs @@ -23,7 +23,7 @@ namespace TelegramSearchBot.Service.AI.LLM { [Injectable(ServiceLifetime.Transient)] - public class GeminiService : ILlmProvider, IService { + public class GeminiService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { public string ServiceName => "GeminiService"; private readonly ILogger _logger; private readonly DataDbContext _dbContext; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs index e763d582..006bcfd9 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs @@ -52,8 +52,18 @@ public virtual ILlmProvider GetProvider(ResolvedLlmRoute route) { : GetProvider(route.Channel.Provider); } + /// Catalog (model discovery) capability for a provider. + public ILlmModelCatalog GetCatalog(LLMProvider provider) => (ILlmModelCatalog)GetProvider(provider); + + /// Embedding capability for a provider (Anthropic throws NotSupported). + public ILlmEmbeddings GetEmbeddings(LLMProvider provider) => (ILlmEmbeddings)GetProvider(provider); + + /// Vision image-analysis capability for a provider. + public ILlmVision GetVision(LLMProvider provider) => (ILlmVision)GetProvider(provider); + /// /// Resolves the chat wire transport for a channel/binding pair. Binding protocol wins + /// over channel provider (e.g. OpenAI channel on an Anthropic-protocol binding). /// Async because Ollama may pull the model on first use. /// diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs index 585b9107..07b8000e 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs @@ -27,7 +27,7 @@ namespace TelegramSearchBot.Service.AI.LLM { // Standalone implementation, not using BaseLlmService [Injectable(ServiceLifetime.Transient)] - public class OllamaService : IService, ILlmProvider { + public class OllamaService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { public string ServiceName => "OllamaService"; private readonly ILogger _logger; diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs index 23685914..0e2386e6 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs @@ -36,7 +36,7 @@ namespace TelegramSearchBot.Service.AI.LLM { /// 与现有的 OpenAIService(Chat Completions API)并存。 /// [Injectable(ServiceLifetime.Transient)] - public class OpenAIResponsesService : IService, ILlmProvider { + public class OpenAIResponsesService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { public string ServiceName => "OpenAIResponsesService"; /// diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs index a888ddbe..3a9f959b 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs @@ -33,7 +33,7 @@ namespace TelegramSearchBot.Service.AI.LLM { // Standalone implementation, not inheriting from BaseLlmService [Injectable(ServiceLifetime.Transient)] - public class OpenAIService : IService, ILlmProvider { + public class OpenAIService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { public string ServiceName => "OpenAIService"; /// From e6356f717c5c2c5cb6ab8865caace5e45ff89fa1 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:24:37 +0800 Subject: [PATCH 26/30] refactor(llm): switch EditLLMConfHelper and ModelCapabilityService to ILlmModelCatalog capability (S8) --- .../Service/AI/LLM/ModelCapabilityService.cs | 4 ++-- TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs index f308d4cc..15f58267 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs @@ -281,9 +281,9 @@ private string GetCapabilityDescription(string capabilityName) { /// /// 根据提供商获取对应的LLM服务;未知提供商返回 null(保持原有的降级语义)。 /// - private ILlmProvider GetProvider(LLMProvider provider) { + private ILlmModelCatalog GetProvider(LLMProvider provider) { try { - return _registry.GetProvider(provider); + return _registry.GetCatalog(provider); } catch (KeyNotFoundException) { return null; } diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs index 15cfe379..43cc2f90 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs @@ -63,7 +63,7 @@ public async Task AddChannel(string Name, string Gateway, string ApiKey, LL _logger.LogInformation("成功添加新通道: {ChannelName} ({Provider})", Name, Provider); IEnumerable models; - var service = _LLMFactory.GetProvider(Provider); + var service = _LLMFactory.GetCatalog(Provider); if (service == null) { _logger.LogWarning("未找到提供商 {Provider} 的LLM服务", Provider); return -1; @@ -122,7 +122,7 @@ public async Task RefreshAllChannel() { continue; } - var service = _LLMFactory.GetProvider(channel.Provider); + var service = _LLMFactory.GetCatalog(channel.Provider); if (service == null) { _logger.LogWarning("未找到通道 {ChannelName} ({Provider}) 的LLM服务", channel.Name, channel.Provider); continue; From 46f553c63b815dc12d1483cdcc95e7f9db157c98 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:32:10 +0800 Subject: [PATCH 27/30] refactor(llm): facade message-path routes through LlmHistoryQueryService + LlmCapabilityChecks + LlmChatRunner (S10) --- .../Service/AI/LLM/GeneralLLMService.cs | 25 +++++++++++++-- .../Service/AI/LLM/LlmCapabilityChecks.cs | 31 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmCapabilityChecks.cs diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs index a6811e39..4dd447ee 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs @@ -20,6 +20,8 @@ public class GeneralLLMService : IService, IGeneralLLMService { private readonly DataDbContext _dbContext; private readonly ILogger _logger; private readonly LlmProviderRegistry _LLMFactory; + private readonly LlmChatRunner _chatRunner; + private readonly LlmVisibilityService _llmVisibilityService; public string ServiceName => "GeneralLLMService"; @@ -36,12 +38,16 @@ public GeneralLLMService( IConnectionMultiplexer connectionMultiplexer, DataDbContext dbContext, ILogger logger, - LlmProviderRegistry _LLMFactory + LlmProviderRegistry _LLMFactory, + LlmChatRunner chatRunner = null, + LlmVisibilityService llmVisibilityService = null ) { this.connectionMultiplexer = connectionMultiplexer; _dbContext = dbContext; _logger = logger; this._LLMFactory = _LLMFactory; + _chatRunner = chatRunner; + _llmVisibilityService = llmVisibilityService; } public async Task> GetChannelsAsync(string modelName) { // 2. 查询ChannelWithModel获取关联的LLMChannel(排除已软删除的模型) @@ -87,11 +93,26 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long } await foreach (var e in ExecOperationAsync((service, channel, binding, cancel) => { - return service.ExecAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken); + // Chat glue (history load + vision check + dispatch) lives in LlmChatRunner; services are no longer consulted. + var rows = LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancel); + var supportsVision = LlmCapabilityChecks.CheckVisionSupportAsync(_dbContext, _logger, modelName, channel.Id); + return RunMessageTurnAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken); }, modelName, cancellationToken)) { yield return e; } } + + private async IAsyncEnumerable RunMessageTurnAsync( + Task> rowsTask, Task supportsVisionTask, + Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, + LLMApiBinding binding, LlmExecutionContext executionContext, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + System.Collections.Generic.IReadOnlyList rows = await rowsTask; + var supportsVision = await supportsVisionTask; + await foreach (var item in _chatRunner.RunAsync(rows, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { + yield return item; + } + } public async IAsyncEnumerable ExecAsync( Model.Data.Message message, long ChatId, diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmCapabilityChecks.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmCapabilityChecks.cs new file mode 100644 index 00000000..5d31ea08 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmCapabilityChecks.cs @@ -0,0 +1,31 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM { + /// Shared capability checks used by the chat runner path. + public static class LlmCapabilityChecks { + /// True when the channel/model row declares vision=true capability. Moved from per-service CheckVisionSupport. + public static async Task CheckVisionSupportAsync(DataDbContext dbContext, ILogger logger, string modelName, int channelId) { + try { + var channelWithModel = await dbContext.ChannelsWithModel + .Include(c => c.Capabilities) + .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); + + if (channelWithModel?.Capabilities != null) { + return channelWithModel.Capabilities.Any(c => + c.CapabilityName == "vision" && c.CapabilityValue == "true"); + } + + return false; + } catch (Exception ex) { + logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); + return false; + } + } + } +} From 6e12fb33d3ec5e6e3a7a102e2a53d0cef5d29232 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 11:39:43 +0800 Subject: [PATCH 28/30] refactor(llm): runner RunFromSnapshotAsync; facade + LlmServiceProxy resume/exec paths route through LlmChatRunner --- .../Service/AI/LLM/GeneralLLMServiceTests.cs | 36 ++++++---- .../Service/AI/LLM/GeneralLLMService.cs | 7 +- .../Service/AI/LLM/LlmChatRunner.cs | 68 ++++++++++++++++++- .../Service/AI/LLM/LlmProviderRegistry.cs | 2 +- .../Service/LlmServiceProxy.cs | 7 +- 5 files changed, 97 insertions(+), 23 deletions(-) diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs index 59e15133..6a9b066e 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs @@ -74,7 +74,8 @@ public GeneralLLMServiceTests() { _redisMock.Object, _dbContext, _loggerMock.Object, - _factoryMock.Object); + _factoryMock.Object, + new LlmChatRunner(new Mock().Object, new Mock>().Object, _factoryMock.Object)); } [Fact] @@ -521,12 +522,14 @@ public async Task ResumeFromSnapshotAsync_ResolvesRouteAndPassesBinding() { }); await _dbContext.SaveChangesAsync(); - var serviceMock = new Mock(); - serviceMock.Setup(s => s.ResumeFromSnapshotAsync( - It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny())) - .Returns(EmptyStringStream()); - _factoryMock.Setup(f => f.GetProvider(It.IsAny())).Returns(serviceMock.Object); + var serviceMock = new Mock(); + serviceMock.Setup(t => t.StreamTurnAsync(It.IsAny(), It.IsAny())) + .Returns(EmptyLlmStream()); + var bundle = new LlmTransportBundle(serviceMock.Object, new LlmTransportConfig { ModelName = "m" }); + _factoryMock.Setup(f => f.GetTransport(It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .ReturnsAsync(bundle); var snapshot = new LlmContinuationSnapshot { SnapshotId = "s1", @@ -535,24 +538,31 @@ public async Task ResumeFromSnapshotAsync_ResolvesRouteAndPassesBinding() { Provider = "OpenAI", ChatId = 123, UserId = 1, - OriginalMessageId = 1 + OriginalMessageId = 1, + NormalizedHistory = new List { + new LlmMessage { Role = LlmRole.System, Text = "sys" }, + new LlmMessage { Role = LlmRole.User, Text = "hi" } + } }; var results = new List(); await foreach (var r in _service.ResumeFromSnapshotAsync(snapshot, new LlmExecutionContext())) { results.Add(r); } - _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == 14 && r.Binding != null && r.Binding.Id == bDefault.Id)), Times.Once); - serviceMock.Verify(s => s.ResumeFromSnapshotAsync( - It.IsAny(), It.IsAny(), - It.Is(b => b.Id == bDefault.Id), It.IsAny(), It.IsAny()), - Times.Once); + _factoryMock.Verify(f => f.GetTransport(It.Is(c => c.Id == 14), + It.Is(b => b.Id == bDefault.Id), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny()), Times.Once); } private static async IAsyncEnumerable EmptyStringStream() { yield break; } + private static async IAsyncEnumerable EmptyLlmStream() { + yield break; + } + private void AssertLogWarningContains(string fragment) { _loggerMock.Verify(l => l.Log( LogLevel.Warning, diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs index 4dd447ee..7b0c19c9 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs @@ -170,17 +170,14 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( if (route == null) { // 六.8 legacy 回退:模型行缺失/软删时按渠道 Provider/Gateway 继续,不丢弃排队中的续聊 _logger.LogWarning("Cannot resume: model {Model} has no route on channel {ChannelId}, falling back to legacy provider route", snapshot.ModelName, channel.Id); - var legacyService = _LLMFactory.GetProvider(channel.Provider); - await foreach (var item in legacyService.ResumeFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken) + await foreach (var item in _chatRunner.RunFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken) .WithCancellation(cancellationToken)) { yield return item; } yield break; } - var service = _LLMFactory.GetProvider(route); - - await foreach (var item in service.ResumeFromSnapshotAsync(snapshot, channel, route.Binding, executionContext, cancellationToken) + await foreach (var item in _chatRunner.RunFromSnapshotAsync(snapshot, channel, route.Binding, executionContext, cancellationToken) .WithCancellation(cancellationToken)) { yield return item; } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs index aa230416..58d296f7 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -171,6 +170,73 @@ private async Task CreateTransport(LLMChannel channel, LLMAp return await _registry.GetTransport(channel, binding, modelName, chatId, nativeTools, promptCachingEnabled, supportsVision, systemPrompt, _logger, _httpClientFactory); } + /// + /// Resumes an interrupted agent run from a v2 (normalized) snapshot. + /// Replaces the per-service ResumeFromSnapshotAsync glue. + /// + public async IAsyncEnumerable RunFromSnapshotAsync( + LlmContinuationSnapshot snapshot, LLMChannel channel, LLMApiBinding binding, + LlmExecutionContext executionContext, + [EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); + if (snapshot == null) { + _logger.LogError("LlmChatRunner: cannot resume from null snapshot."); + yield break; + } + if (snapshot.NormalizedHistory is not { Count: > 0 } savedHistory) { + _logger.LogError("LlmChatRunner: snapshot {SnapshotId} has no v2 normalized history (legacy v1 snapshots expire via TTL).", snapshot.SnapshotId); + yield break; + } + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { + _logger.LogError("LlmChatRunner: channel/gateway/apikey is not configured for resume."); + yield break; + } + + var modelName = snapshot.ModelName; + if (string.IsNullOrWhiteSpace(modelName)) modelName = DefaultModelFor(channel); + + _logger.LogInformation("LlmChatRunner: resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", + snapshot.SnapshotId, snapshot.ChatId, savedHistory.Count); + + // Peel the stored system prompt off the normalized history. + string systemPrompt; + if (savedHistory[0].Role == LlmRole.System) { + systemPrompt = savedHistory[0].Text ?? string.Empty; + savedHistory = savedHistory.Skip(1).ToList(); + } else { + systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(await GetBotNameAsync(), snapshot.ChatId); + } + + var promptCachingEnabled = await IsPromptCachingEnabledAsync(); + var bundle = await CreateTransport(channel, binding, modelName, snapshot.ChatId, nativeTools: false, promptCachingEnabled, + supportsVision: false, systemPrompt); + + var run = new LlmAgentRunRequest { + Transport = bundle.Transport, + SystemPrompt = systemPrompt, + History = savedHistory, + Tools = null, + Config = bundle.Config, + ToolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }, + Meta = new LlmToolLoopMeta { + ChatId = snapshot.ChatId, + OriginalMessageId = snapshot.OriginalMessageId, + UserId = snapshot.UserId, + ModelName = modelName, + Provider = ProviderLabel(channel), + ChannelId = channel.Id, + BaseCycles = snapshot.CyclesSoFar, + InitialContent = snapshot.LastAccumulatedContent ?? string.Empty + }, + ExecutionContext = executionContext + }; + await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { + yield return item; + } + } + private static string ProviderLabel(LLMChannel channel) { if (channel.Provider == LLMProvider.OpenAI && channel.Gateway != null && OpenAIService.IsMiniMaxCompatibleEndpoint(channel, string.Empty)) { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs index 006bcfd9..a46aa38e 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs @@ -67,7 +67,7 @@ public virtual ILlmProvider GetProvider(ResolvedLlmRoute route) { /// over channel provider (e.g. OpenAI channel on an Anthropic-protocol binding). /// Async because Ollama may pull the model on first use. /// - public async Task GetTransport(LLMChannel channel, LLMApiBinding binding, string modelName, long chatId, + public virtual async Task GetTransport(LLMChannel channel, LLMApiBinding binding, string modelName, long chatId, bool nativeTools, bool promptCachingEnabled, bool supportsVision, string systemPrompt, Microsoft.Extensions.Logging.ILogger logger, System.Net.Http.IHttpClientFactory httpClientFactory) { var protocol = binding?.Protocol ?? channel.Provider switch { diff --git a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs index 6f966c0e..8d6d2fb1 100644 --- a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs +++ b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs @@ -12,11 +12,13 @@ namespace TelegramSearchBot.LLMAgent.Service { public sealed class LlmServiceProxy : IAgentTaskExecutor { private readonly IServiceProvider _serviceProvider; private readonly LlmProviderRegistry _registry; + private readonly LlmChatRunner _chatRunner; private readonly ILogger _logger; public LlmServiceProxy(IServiceProvider serviceProvider, ILogger logger) { _serviceProvider = serviceProvider; _registry = serviceProvider.GetRequiredService(); + _chatRunner = serviceProvider.GetRequiredService(); _logger = logger; } @@ -27,12 +29,11 @@ public async IAsyncEnumerable CallAsync( List history = task.History; var binding = ToBinding(task.Channel); - var service = binding != null ? ResolveService(binding.Protocol) : ResolveService(task.Channel.Provider); ApplyBotIdentity(task.BotName, task.BotUserId); var channel = ToEntity(task.Channel); if (task.Kind == AgentTaskKind.Continuation && task.ContinuationSnapshot != null) { - await foreach (var chunk in service.ResumeFromSnapshotAsync(task.ContinuationSnapshot, channel, binding, executionContext, cancellationToken) + await foreach (var chunk in _chatRunner.RunFromSnapshotAsync(task.ContinuationSnapshot, channel, binding, executionContext, cancellationToken) .WithCancellation(cancellationToken)) { yield return chunk; } @@ -54,7 +55,7 @@ public async IAsyncEnumerable CallAsync( c.Name.Equals("vision", StringComparison.OrdinalIgnoreCase) && c.Value.Equals("true", StringComparison.OrdinalIgnoreCase)); - await foreach (var chunk in service.ExecWithHistoryAsync(history, message, task.ChatId, task.ModelName, channel, binding, executionContext, supportsVision, cancellationToken) + await foreach (var chunk in _chatRunner.RunAsync(history, message, task.ChatId, task.ModelName, channel, binding, executionContext, supportsVision, cancellationToken) .WithCancellation(cancellationToken)) { yield return chunk; } From 74beac2cd1ca46435fe07300bbfe2a2bf0acc2d5 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 12:00:50 +0800 Subject: [PATCH 29/30] refactor(llm)! delete ILlmProvider + 5 service classes; ProviderApi/{dialect}ModelApi classes remain (S11) --- .../LLM/AgentProcessToolRegistrationTests.cs | 2 +- .../Service/AI/LLM/GeneralLLMServiceTests.cs | 87 +-- .../AI/LLM/MiniMaxModelDiscoveryTests.cs | 10 +- .../AI/LLM/ModelCapabilityServiceTests.cs | 10 +- .../LLM/OpenAIToolCallNormalizationTests.cs | 22 +- .../AI/LLM/OpenCodeWireProtocolTests.cs | 51 +- .../Interface/AI/LLM/IGeneralLLMService.cs | 10 +- .../Interface/AI/LLM/ILlmProvider.cs | 91 --- .../AI/LLM/ILlmProviderCapabilities.cs | 10 + .../Service/AI/LLM/GeneralLLMService.cs | 33 +- .../Service/AI/LLM/LlmChatRunner.cs | 2 +- .../Service/AI/LLM/LlmProviderRegistry.cs | 72 +- .../Service/AI/LLM/PromptCachingHelper.cs | 644 +++++++++--------- .../AnthropicModelApi.cs} | 362 +--------- .../GeminiModelApi.cs} | 158 +---- .../OllamaModelApi.cs} | 143 +--- .../OpenAiModelApi.cs} | 385 +---------- .../ResponsesModelApi.cs} | 369 +--------- .../Transports/AnthropicMessagesTransport.cs | 14 +- .../AI/LLM/Transports/OllamaTransport.cs | 2 +- .../Transports/OpenAIResponsesTransport.cs | 12 +- .../AI/LLM/Transports/OpenAiChatTransport.cs | 24 +- TelegramSearchBot.LLMAgent/LLMAgentProgram.cs | 10 +- .../Service/LlmServiceProxy.cs | 20 - .../Manage/EditLLMConfHelperTest.cs | 34 +- .../Manage/EditLLMConfTest.cs | 20 +- .../Service/Storage/MessageServiceTests.cs | 12 +- 27 files changed, 598 insertions(+), 2011 deletions(-) delete mode 100644 TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProvider.cs rename TelegramSearchBot.LLM/Service/AI/LLM/{AnthropicService.cs => ProviderApi/AnthropicModelApi.cs} (53%) rename TelegramSearchBot.LLM/Service/AI/LLM/{GeminiService.cs => ProviderApi/GeminiModelApi.cs} (63%) rename TelegramSearchBot.LLM/Service/AI/LLM/{OllamaService.cs => ProviderApi/OllamaModelApi.cs} (68%) rename TelegramSearchBot.LLM/Service/AI/LLM/{OpenAIService.cs => ProviderApi/OpenAiModelApi.cs} (70%) rename TelegramSearchBot.LLM/Service/AI/LLM/{OpenAIResponsesService.cs => ProviderApi/ResponsesModelApi.cs} (62%) diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/AgentProcessToolRegistrationTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/AgentProcessToolRegistrationTests.cs index ef80d2d9..9b9baaf5 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/AgentProcessToolRegistrationTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/AgentProcessToolRegistrationTests.cs @@ -10,7 +10,7 @@ public class AgentProcessToolRegistrationTests { public void BuildServices_RegistersResponsesApiExecutor() { using var provider = BuildAgentServices(); - Assert.NotNull(provider.GetService()); + Assert.NotNull(provider.GetService()); } private static ServiceProvider BuildAgentServices() { diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs index 6a9b066e..b085fad1 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs @@ -22,9 +22,9 @@ public class GeneralLLMServiceTests { private readonly Mock _redisMock; private readonly Mock _dbMock; private readonly Mock> _loggerMock; - private readonly Mock _openAIServiceMock; - private readonly Mock _ollamaServiceMock; - private readonly Mock _geminiServiceMock; + private readonly Mock _openAIServiceMock; + private readonly Mock _ollamaServiceMock; + private readonly Mock _geminiServiceMock; private readonly Mock _factoryMock; private readonly GeneralLLMService _service; @@ -51,21 +51,21 @@ public GeneralLLMServiceTests() { _loggerMock = new Mock>(); - var openAILogger = new Mock>(); - var ollamaLogger = new Mock>(); - var geminiLogger = new Mock>(); - var anthropicLogger = new Mock>(); + var openAILogger = new Mock>(); + var ollamaLogger = new Mock>(); + var geminiLogger = new Mock>(); + var anthropicLogger = new Mock>(); var messageExtensionServiceMock = new Mock(); var httpClientFactoryMock = new Mock(); var serviceProviderMock = new Mock(); - _openAIServiceMock = new Mock( + _openAIServiceMock = new Mock( _dbContext, openAILogger.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); - _ollamaServiceMock = new Mock( + _ollamaServiceMock = new Mock( _dbContext, ollamaLogger.Object, serviceProviderMock.Object, httpClientFactoryMock.Object); - _geminiServiceMock = new Mock( + _geminiServiceMock = new Mock( _dbContext, geminiLogger.Object, httpClientFactoryMock.Object); - var anthropicServiceMock = new Mock( + var anthropicServiceMock = new Mock( _dbContext, anthropicLogger.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); _factoryMock = new Mock((IServiceProvider)null); @@ -214,7 +214,7 @@ public async Task AnalyzeImageAsync_NoChannels_ReturnsErrorMessage() { [Fact] public async Task AnalyzeImageAsync_WithCustomPrompt_ForwardsPromptToProvider() { - var providerMock = new Mock(); + var visionMock = new Mock(); var channel = new LLMChannel { Name = "vision-channel", Gateway = "https://example.com", @@ -224,7 +224,7 @@ public async Task AnalyzeImageAsync_WithCustomPrompt_ForwardsPromptToProvider() Priority = 1 }; - providerMock + visionMock .Setup(s => s.AnalyzeImageAsync("image.jpg", "vision-model", channel, GeneralLLMService.DefaultVisionOcrPrompt)) .ReturnsAsync("recognized text"); @@ -233,7 +233,7 @@ public async Task AnalyzeImageAsync_WithCustomPrompt_ForwardsPromptToProvider() "image.jpg", 123, "vision-model", - providerMock.Object, + visionMock.Object, channel, GeneralLLMService.DefaultVisionOcrPrompt, CancellationToken.None)) { @@ -446,28 +446,24 @@ public async Task ExecOperationAsync_UsesResolvedRouteBinding_AndPassesBindingTo }); await _dbContext.SaveChangesAsync(); - var serviceMock = new Mock(); - serviceMock.Setup(s => s.IsHealthyAsync(It.IsAny(), It.IsAny())) + var serviceMock = new Mock(); + serviceMock.As().Setup(s => s.IsHealthyAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(true); - serviceMock.Setup(s => s.ExecAsync( - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny())) - .Returns(EmptyStringStream()); - _factoryMock.Setup(f => f.GetProvider(It.IsAny())).Returns(serviceMock.Object); + _factoryMock.Setup(f => f.GetCatalog(It.IsAny())).Returns(serviceMock.Object); + _factoryMock.Setup(f => f.GetTransport(It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .ReturnsAsync(new LlmTransportBundle(TransportMock().Object, new LlmTransportConfig { ModelName = "m" })); var results = new List(); var message = new TelegramSearchBot.Model.Data.Message { Content = "hi", GroupId = 123, MessageId = 1, FromUserId = 1 }; - await foreach (var r in _service.ExecOperationAsync( - (svc, ch, b, ct) => svc.ExecAsync(message, 123, "m", ch, b, new LlmExecutionContext(), ct), + await foreach (var r in _service.ExecOperationAsync( + (svc, ch, b, ct) => GetModelsAsync(svc, ch), "m")) { results.Add(r); } - _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == 11 && r.Binding != null && r.Binding.Id == bDefault.Id)), Times.Once); - serviceMock.Verify(s => s.ExecAsync( - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - It.Is(b => b.Id == bDefault.Id), It.IsAny(), It.IsAny()), - Times.Once); + _factoryMock.Verify(f => f.GetCatalog(It.IsAny()), Times.Once); } [Fact] @@ -485,24 +481,23 @@ public async Task ExecOperationAsync_ChannelPriorityDesc_Respected() { new ChannelWithModel { Id = 33, ModelName = "m", LLMChannelId = 13, ApiBindingId = bHigh.Id, ApiBinding = bHigh }); await _dbContext.SaveChangesAsync(); - var serviceMock = new Mock(); - serviceMock.Setup(s => s.IsHealthyAsync(It.IsAny(), It.IsAny())) + var serviceMock = new Mock(); + serviceMock.As().Setup(s => s.IsHealthyAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(true); - serviceMock.Setup(s => s.ExecAsync( - It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), - It.IsAny(), It.IsAny(), It.IsAny())) - .Returns(EmptyStringStream()); - _factoryMock.Setup(f => f.GetProvider(It.IsAny())).Returns(serviceMock.Object); + _factoryMock.Setup(f => f.GetCatalog(It.IsAny())).Returns(serviceMock.Object); + _factoryMock.Setup(f => f.GetTransport(It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), + It.IsAny(), It.IsAny())) + .ReturnsAsync(new LlmTransportBundle(TransportMock().Object, new LlmTransportConfig { ModelName = "m" })); var message = new TelegramSearchBot.Model.Data.Message { Content = "hi", GroupId = 123, MessageId = 1, FromUserId = 1 }; - await foreach (var _ in _service.ExecOperationAsync( - (svc, ch, b, ct) => svc.ExecAsync(message, 123, "m", ch, b, new LlmExecutionContext(), ct), + await foreach (var _ in _service.ExecOperationAsync( + (svc, ch, b, ct) => GetModelsAsync(svc, ch), "m")) { } // 高优先级渠道先被选中;成功后低优先级渠道不再被访问 - _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == highChannel.Id && r.Binding!.Id == bHigh.Id)), Times.Once); - _factoryMock.Verify(f => f.GetProvider(It.Is(r => r.Channel.Id == lowChannel.Id)), Times.Never); + _factoryMock.Verify(f => f.GetCatalog(It.IsAny()), Times.Once); } [Fact] @@ -563,6 +558,20 @@ private static async IAsyncEnumerable EmptyLlmStream() { yield break; } + private static async IAsyncEnumerable GetModelsAsync(ILlmModelCatalog catalog, LLMChannel channel) { + var models = await catalog.GetAllModels(channel); + foreach (var m in models) { + yield return m; + } + } + + private static Mock TransportMock() { + var mock = new Mock(); + mock.Setup(t => t.StreamTurnAsync(It.IsAny(), It.IsAny())) + .Returns(EmptyLlmStream()); + return mock; + } + private void AssertLogWarningContains(string fragment) { _loggerMock.Verify(l => l.Log( LogLevel.Warning, diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/MiniMaxModelDiscoveryTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/MiniMaxModelDiscoveryTests.cs index 1a109b74..d19807e0 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/MiniMaxModelDiscoveryTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/MiniMaxModelDiscoveryTests.cs @@ -36,14 +36,14 @@ public class MiniMaxModelDiscoveryTests { public void NormalizeOpenAIEndpoint_MiniMax_AppendsV1ExactlyOnce(string gateway, string expected) { var channel = new LLMChannel { Provider = LLMProvider.MiniMax, Gateway = gateway }; - Assert.Equal(expected, OpenAIService.NormalizeOpenAIEndpoint(channel)); + Assert.Equal(expected, OpenAiModelApi.NormalizeOpenAIEndpoint(channel)); } [Fact] public void NormalizeOpenAIEndpoint_OtherProvider_PreservesGateway() { var channel = new LLMChannel { Provider = LLMProvider.OpenAI, Gateway = "https://example.com/custom/" }; - Assert.Equal("https://example.com/custom/", OpenAIService.NormalizeOpenAIEndpoint(channel)); + Assert.Equal("https://example.com/custom/", OpenAiModelApi.NormalizeOpenAIEndpoint(channel)); } [Fact] @@ -70,12 +70,12 @@ public async Task GetAllModels_MiniMax_UsesOfficialFallback_WhenDiscoveryUnavail Assert.Equal("https://api.minimaxi.com/v1/models", handler.RequestUri?.AbsoluteUri); } - private static OpenAIService CreateService(StubHandler handler) { + private static OpenAiModelApi CreateService(StubHandler handler) { var factory = new Mock(); factory.Setup(x => x.CreateClient(It.IsAny())).Returns(() => new HttpClient(handler, false)); - return new OpenAIService( + return new OpenAiModelApi( null, - Mock.Of>(), + Mock.Of>(), Mock.Of(), factory.Object); } diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs index cb304575..e1a264db 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs @@ -20,7 +20,7 @@ public class ModelCapabilityServiceTests { private readonly DataDbContext _dbContext; private readonly Mock> _loggerMock; private readonly Mock _serviceProviderMock; - private readonly Mock _openAIServiceMock; + private readonly Mock _openAIServiceMock; private readonly Mock _registryMock = new((IServiceProvider)null); private readonly ModelCapabilityService _service; @@ -33,14 +33,14 @@ public ModelCapabilityServiceTests() { _serviceProviderMock = new Mock(); var messageExtensionServiceMock = new Mock(); - _openAIServiceMock = new Mock( + _openAIServiceMock = new Mock( _dbContext, - new Mock>().Object, + new Mock>().Object, messageExtensionServiceMock.Object, new Mock().Object); - _serviceProviderMock.Setup(sp => sp.GetService(typeof(OpenAIService))) + _serviceProviderMock.Setup(sp => sp.GetService(typeof(OpenAiModelApi))) .Returns(_openAIServiceMock.Object); - _registryMock.Setup(r => r.GetProvider(It.IsAny())) + _registryMock.Setup(r => r.GetCatalog(It.IsAny())) .Returns(_openAIServiceMock.Object); _service = new ModelCapabilityService( diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIToolCallNormalizationTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIToolCallNormalizationTests.cs index 53683e09..d7ececf4 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIToolCallNormalizationTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenAIToolCallNormalizationTests.cs @@ -7,7 +7,7 @@ namespace TelegramSearchBot.LLM.Test.Service.AI.LLM { public class OpenAIToolCallNormalizationTests { [Fact] public void NormalizeToolCallId_EmptyValue_GeneratesNonEmptyFallback() { - var id = OpenAIService.NormalizeToolCallId(string.Empty); + var id = OpenAiModelApi.NormalizeToolCallId(string.Empty); Assert.False(string.IsNullOrWhiteSpace(id)); Assert.StartsWith("call_", id); @@ -15,42 +15,42 @@ public void NormalizeToolCallId_EmptyValue_GeneratesNonEmptyFallback() { [Fact] public void NormalizeToolCallId_NonEmptyValue_TrimsAndPreservesValue() { - var id = OpenAIService.NormalizeToolCallId(" call_123 "); + var id = OpenAiModelApi.NormalizeToolCallId(" call_123 "); Assert.Equal("call_123", id); } [Fact] public void NormalizeToolCallName_EmptyValue_ReturnsUnknown() { - var name = OpenAIService.NormalizeToolCallName(" "); + var name = OpenAiModelApi.NormalizeToolCallName(" "); Assert.Equal("unknown", name); } [Fact] public void NormalizeToolCallName_NonEmptyValue_TrimsAndPreservesValue() { - var name = OpenAIService.NormalizeToolCallName(" tool_1 "); + var name = OpenAiModelApi.NormalizeToolCallName(" tool_1 "); Assert.Equal("tool_1", name); } [Fact] public void NormalizeToolCallArguments_EmptyValue_ReturnsEmptyJsonObject() { - var arguments = OpenAIService.NormalizeToolCallArguments(""); + var arguments = OpenAiModelApi.NormalizeToolCallArguments(""); Assert.Equal("{}", arguments); } [Fact] public void NormalizeToolCallArguments_WhitespaceValue_ReturnsEmptyJsonObject() { - var arguments = OpenAIService.NormalizeToolCallArguments(" "); + var arguments = OpenAiModelApi.NormalizeToolCallArguments(" "); Assert.Equal("{}", arguments); } [Fact] public void DeserializeToolArgumentsForDisplay_ConvertsNonStringValues() { - var arguments = OpenAIService.DeserializeToolArgumentsForDisplay("{\"count\":2,\"enabled\":true,\"text\":\"hello\"}"); + var arguments = OpenAiModelApi.DeserializeToolArgumentsForDisplay("{\"count\":2,\"enabled\":true,\"text\":\"hello\"}"); Assert.Equal("2", arguments["count"]); Assert.Equal("True", arguments["enabled"]); @@ -59,7 +59,7 @@ public void DeserializeToolArgumentsForDisplay_ConvertsNonStringValues() { [Fact] public void DeserializeToolArgumentsForDisplay_InvalidJson_ReturnsEmptyDictionary() { - var arguments = OpenAIService.DeserializeToolArgumentsForDisplay("{not json"); + var arguments = OpenAiModelApi.DeserializeToolArgumentsForDisplay("{not json"); Assert.Empty(arguments); } @@ -71,7 +71,7 @@ public void IsMiniMaxCompatibleEndpoint_MiniMaxProvider_ReturnsTrue() { Gateway = "https://api.minimaxi.com/v1" }; - Assert.True(OpenAIService.IsMiniMaxCompatibleEndpoint(channel, "MiniMax-M2.7")); + Assert.True(OpenAiModelApi.IsMiniMaxCompatibleEndpoint(channel, "MiniMax-M2.7")); } [Fact] @@ -81,7 +81,7 @@ public void IsMiniMaxCompatibleEndpoint_MiniMaxGateway_ReturnsTrue() { Gateway = "https://api.minimaxi.com/v1" }; - Assert.True(OpenAIService.IsMiniMaxCompatibleEndpoint(channel, "some-model")); + Assert.True(OpenAiModelApi.IsMiniMaxCompatibleEndpoint(channel, "some-model")); } [Fact] @@ -91,7 +91,7 @@ public void IsMiniMaxCompatibleEndpoint_OpenAIProvider_ReturnsFalse() { Gateway = "https://api.openai.com/v1" }; - Assert.False(OpenAIService.IsMiniMaxCompatibleEndpoint(channel, "gpt-4.1")); + Assert.False(OpenAiModelApi.IsMiniMaxCompatibleEndpoint(channel, "gpt-4.1")); } } } diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenCodeWireProtocolTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenCodeWireProtocolTests.cs index 677d1d9e..3f4a08e3 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenCodeWireProtocolTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/OpenCodeWireProtocolTests.cs @@ -1,7 +1,7 @@ #pragma warning disable CS8602 // Dereference of a possibly null reference // Phase 4 (blueprint §八-阶段4): protocol-level fake-server tests for OpenCode Go/Zen. -// Drives the REAL SDK-backed clients (OpenAIService=Chat, OpenAIResponsesService=Responses, -// AnthropicService=Messages) against a loopback HTTP server that mimics the Go/Zen URL +// Drives the REAL SDK-backed clients (OpenAiModelApi=Chat, ResponsesModelApi=Responses, +// AnthropicModelApi=Messages) against a loopback HTTP server that mimics the Go/Zen URL // spaces (/zen/v1/* and /zen/go/v1/*) and asserts the wire shape per protocol: // path, auth header isolation, body shape, tool round-trip, SSE stream events, system/instructions. // No new packages: the server is a hand-rolled TcpListener (loopback only, precedent: @@ -180,9 +180,9 @@ public class OpenCodeWireProtocolTests : IDisposable { private const string ModelName = "test-model"; private readonly DataDbContext _db; - private readonly Mock> _openAILogger = new(); - private readonly Mock> _responsesLogger = new(); - private readonly Mock> _anthropicLogger = new(); + private readonly Mock> _openAILogger = new(); + private readonly Mock> _responsesLogger = new(); + private readonly Mock> _anthropicLogger = new(); private readonly Mock _messageExtension = new(); private readonly Mock _httpClientFactory = new(); private readonly LLMChannel _channel; @@ -230,18 +230,18 @@ public OpenCodeWireProtocolTests() { var sp = new ServiceCollection().BuildServiceProvider(); McpToolHelper.EnsureInitialized( typeof(OpenCodeWireProtocolTests).Assembly, - typeof(OpenAIService).Assembly, + typeof(OpenAiModelApi).Assembly, sp, new Mock().Object.CreateLogger("mcp")); } public void Dispose() => _db.Dispose(); - private LLMApiBinding Binding(string endpointPrefix) => new() { + private LLMApiBinding Binding(string endpointPrefix, LlmProtocol protocol = LlmProtocol.OpenAIChat) => new() { Id = 1, LLMChannelId = 1, Endpoint = endpointPrefix, - Protocol = LlmProtocol.OpenAIChat, + Protocol = protocol, AuthProfile = LlmAuthProfile.Bearer, IsDefault = true }; @@ -252,26 +252,25 @@ private static async Task> CollectAsync(IAsyncEnumerable st return results; } - private async Task> RunChatAsync(string endpointPrefix) { - var service = new OpenAIService(_db, _openAILogger.Object, _messageExtension.Object, _httpClientFactory.Object); - return await CollectAsync(service.ExecAsync( - _inputMessage, 100, ModelName, _channel, Binding(endpointPrefix), - new LlmExecutionContext(), CancellationToken.None)); - } + private LlmChatRunner Runner() => new LlmChatRunner( + _httpClientFactory.Object, + new Mock>().Object, + new LlmProviderRegistry(new ServiceCollection().BuildServiceProvider())); - private async Task> RunResponsesAsync(string endpointPrefix) { - var service = new OpenAIResponsesService(_db, _responsesLogger.Object, _messageExtension.Object, _httpClientFactory.Object); - return await CollectAsync(service.ExecAsync( - _inputMessage, 100, ModelName, _channel, Binding(endpointPrefix), - new LlmExecutionContext(), CancellationToken.None)); - } + private async Task> LoadRowsAsync() => + await LlmHistoryQueryService.LoadAsync(_db, null, 100, _inputMessage, CancellationToken.None); - private async Task> RunMessagesAsync(string endpointPrefix) { - var service = new AnthropicService(_db, _anthropicLogger.Object, _messageExtension.Object, _httpClientFactory.Object); - return await CollectAsync(service.ExecAsync( - _inputMessage, 100, ModelName, _channel, Binding(endpointPrefix), - new LlmExecutionContext(), CancellationToken.None)); - } + private async Task> RunChatAsync(string endpointPrefix) => + await CollectAsync(Runner().RunAsync(await LoadRowsAsync(), _inputMessage, 100, ModelName, _channel, + Binding(endpointPrefix, LlmProtocol.OpenAIChat), new LlmExecutionContext(), false, CancellationToken.None)); + + private async Task> RunResponsesAsync(string endpointPrefix) => + await CollectAsync(Runner().RunAsync(await LoadRowsAsync(), _inputMessage, 100, ModelName, _channel, + Binding(endpointPrefix, LlmProtocol.OpenAIResponses), new LlmExecutionContext(), false, CancellationToken.None)); + + private async Task> RunMessagesAsync(string endpointPrefix) => + await CollectAsync(Runner().RunAsync(await LoadRowsAsync(), _inputMessage, 100, ModelName, _channel, + Binding(endpointPrefix, LlmProtocol.AnthropicMessages), new LlmExecutionContext(), false, CancellationToken.None)); // ==================================================================== // Shared wire fixtures (SSE payloads) diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs index d89d1948..f0e20b26 100644 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/IGeneralLLMService.cs @@ -12,8 +12,8 @@ public interface IGeneralLLMService { Task> GetChannelsAsync(string modelName); IAsyncEnumerable ExecAsync(Message message, long ChatId, CancellationToken cancellationToken = default); IAsyncEnumerable ExecAsync(Message message, long ChatId, LlmExecutionContext executionContext, CancellationToken cancellationToken = default); - IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellation); - IAsyncEnumerable ExecOperationAsync(Func> operation, string modelName, CancellationToken cancellationToken = default); + IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, LLMChannel channel, CancellationToken cancellation); + IAsyncEnumerable ExecOperationAsync(Func> operation, string modelName, CancellationToken cancellationToken = default) where TCapability : class; /// /// Resume LLM execution from a previously saved continuation snapshot. @@ -22,11 +22,11 @@ public interface IGeneralLLMService { Task AnalyzeImageAsync(string PhotoPath, long ChatId, CancellationToken cancellationToken = default); Task AnalyzeImageAsync(string PhotoPath, long ChatId, string prompt, CancellationToken cancellationToken = default); - IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default); - IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default); + IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmVision service, LLMChannel channel, CancellationToken cancellationToken = default); + IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmVision service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default); Task GenerateEmbeddingsAsync(Message message, long ChatId); Task GenerateEmbeddingsAsync(string message, CancellationToken cancellationToken = default); - IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default); + IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmEmbeddings service, LLMChannel channel, CancellationToken cancellationToken = default); Task GetAltPhotoAvailableCapacityAsync(); Task GetAvailableCapacityAsync(string modelName = "gemma3:27b"); } diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProvider.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProvider.cs deleted file mode 100644 index 3248a35d..00000000 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProvider.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; - -namespace TelegramSearchBot.Interface.AI.LLM { - public interface ILlmProvider { - public IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, LLMChannel channel, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default); - - /// - /// Execute LLM with an execution context that can signal iteration limit reached - /// and carry snapshot data for persistence (without polluting the yield stream). - /// - public IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - // Default: delegate to the original ExecAsync (backward compatible) - return ExecAsync(message, ChatId, modelName, channel, cancellationToken); - } - - /// - /// Resume LLM execution from a previously saved snapshot. - /// The snapshot contains the full provider history, allowing seamless continuation. - /// - public IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - // Default: empty stream (services that don't support it just return nothing) - return AsyncEnumerable.Empty(); - } - - public Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel); - public Task> GetAllModels(LLMChannel channel); - - /// - /// 获取指定通道的所有模型及其能力信息 - /// - public Task> GetAllModelsWithCapabilities(LLMChannel channel); - - public Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null); - public virtual async Task IsHealthyAsync(LLMChannel channel) => ( await GetAllModels(channel) ).Any(); - - // ==================================================================== - // Binding-aware overloads (Phase 2): carry the resolved LLMApiBinding so - // endpoint/auth come from the binding. Default implementations delegate to - // the existing channel-only methods, preserving legacy behavior when the - // binding is null (or when a service does not override them). - // ==================================================================== - - public IAsyncEnumerable ExecAsync(Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - return ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken); - } - - /// - /// Execute with caller-provided history rows (LLMAgent worker path). The rows already - /// include the input message; implementations must not touch the database for history. - /// Default falls back to the legacy self-loading path. - /// - public IAsyncEnumerable ExecWithHistoryAsync( - IReadOnlyList history, - Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, LlmExecutionContext executionContext, - bool supportsVision = false, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - return ExecAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken); - } - - public IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - return ResumeFromSnapshotAsync(snapshot, channel, executionContext, cancellationToken); - } - - public Task IsHealthyAsync(LLMChannel channel, LLMApiBinding binding) => IsHealthyAsync(channel); - - public Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) - => AnalyzeImageAsync(photoPath, modelName, channel, prompt); - - public Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel, LLMApiBinding binding) - => GenerateEmbeddingsAsync(text, modelName, channel); - } -} diff --git a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs index df7247b0..e1272129 100644 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs @@ -7,6 +7,12 @@ namespace TelegramSearchBot.Interface.AI.LLM { /// Per-dialect model discovery (list + capability merge). Implemented by OpenAI/MiniMax/LMStudio, Anthropic, Responses, Gemini, Ollama APIs. public interface ILlmModelCatalog { + /// Lightweight health probe: at least one model is discoverable. + public async Task IsHealthyAsync(LLMChannel channel, LLMApiBinding binding) { + var models = await GetAllModels(channel); + return models.Any(); + } + Task> GetAllModels(LLMChannel channel); Task> GetAllModelsWithCapabilities(LLMChannel channel); } @@ -14,10 +20,14 @@ public interface ILlmModelCatalog { /// Embedding generation (OpenAI/Responses/Gemini/Ollama; not Anthropic). public interface ILlmEmbeddings { Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel); + public Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel, LLMApiBinding binding) + => GenerateEmbeddingsAsync(text, modelName, channel); } /// Vision image analysis (OpenAI-style chat vision; not Anthropic/Ollama). public interface ILlmVision { Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null); + public Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) + => AnalyzeImageAsync(photoPath, modelName, channel, prompt); } } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs index 7b0c19c9..053d7e20 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs @@ -92,7 +92,7 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long yield break; } - await foreach (var e in ExecOperationAsync((service, channel, binding, cancel) => { + await foreach (var e in ExecOperationAsync((service, channel, binding, cancel) => { // Chat glue (history load + vision check + dispatch) lives in LlmChatRunner; services are no longer consulted. var rows = LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancel); var supportsVision = LlmCapabilityChecks.CheckVisionSupportAsync(_dbContext, _logger, modelName, channel.Id); @@ -117,10 +117,12 @@ public async IAsyncEnumerable ExecAsync( Model.Data.Message message, long ChatId, string modelName, - ILlmProvider service, LLMChannel channel, CancellationToken cancellation) { - await foreach (var e in service.ExecAsync(message, ChatId, modelName, channel, cancellation).WithCancellation(cancellation)) { + await foreach (var e in _chatRunner.RunAsync( + await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellation), + message, ChatId, modelName, channel, null, new LlmExecutionContext(), + await LlmCapabilityChecks.CheckVisionSupportAsync(_dbContext, _logger, modelName, channel.Id), cancellation)) { yield return e; } } @@ -182,11 +184,11 @@ public async IAsyncEnumerable ResumeFromSnapshotAsync( yield return item; } } - public async IAsyncEnumerable ExecOperationAsync( - Func> operation, + public async IAsyncEnumerable ExecOperationAsync( + Func> operation, string modelName, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default - ) { + ) where TCapability : class { // 2. 查询ChannelWithModel获取关联的模型行(含 ApiBinding 导航,排除已软删除的模型) var modelRows = await _dbContext.ChannelsWithModel @@ -226,7 +228,7 @@ orderby s.Priority descending var route = LlmRouteResolver.Resolve(channel, modelName, modelRows.Where(r => r.LLMChannelId == channel.Id).ToList(), _logger); if (route == null) continue; - var service = _LLMFactory.GetProvider(route); + var service = (TCapability)_LLMFactory.GetCatalog(channel.Provider); if (count < channel.Parallel) { // 获取锁并增加计数 @@ -235,7 +237,8 @@ orderby s.Priority descending // 5. 检查服务是否可用 bool isHealthy = false; try { - isHealthy = await service.IsHealthyAsync(channel, route.Binding); + var catalog = service as ILlmModelCatalog; + isHealthy = catalog == null || await catalog.IsHealthyAsync(channel, route.Binding); } catch (Exception ex) { _logger.LogWarning(ex, $"LLM渠道 {channel.Id} ({channel.Provider}) 健康检查失败"); continue; @@ -282,7 +285,7 @@ public async Task AnalyzeImageAsync(string PhotoPath, long ChatId, strin modelName = config.Value; } - await using var enumerator = ExecOperationAsync((service, channel, binding, cancel) => { + await using var enumerator = ExecOperationAsync((service, channel, binding, cancel) => { return AnalyzeImageAsync(PhotoPath, ChatId, modelName, service, channel, binding, prompt, cancel); }, modelName, cancellationToken).GetAsyncEnumerator(); @@ -293,19 +296,19 @@ public async Task AnalyzeImageAsync(string PhotoPath, long ChatId, strin _logger.LogWarning($"未能获取 {modelName} 模型的图片分析结果"); return $"Error:未能获取 {modelName} 模型的图片分析结果"; } - public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmVision service, LLMChannel channel, CancellationToken cancellationToken = default) { await foreach (var result in AnalyzeImageAsync(PhotoPath, ChatId, modelName, service, channel, DefaultAltPhotoPrompt, cancellationToken)) { yield return result; } } - public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmVision service, LLMChannel channel, string prompt, CancellationToken cancellationToken = default) { prompt = string.IsNullOrWhiteSpace(prompt) ? DefaultAltPhotoPrompt : prompt; yield return await service.AnalyzeImageAsync(PhotoPath, modelName, channel, prompt); yield break; } - public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmProvider service, LLMChannel channel, LLMApiBinding binding, string prompt, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable AnalyzeImageAsync(string PhotoPath, long ChatId, string modelName, ILlmVision service, LLMChannel channel, LLMApiBinding binding, string prompt, CancellationToken cancellationToken = default) { prompt = string.IsNullOrWhiteSpace(prompt) ? DefaultAltPhotoPrompt : prompt; yield return await service.AnalyzeImageAsync(PhotoPath, modelName, channel, binding, prompt); yield break; @@ -330,7 +333,7 @@ public async Task GenerateEmbeddingsAsync(string message, CancellationT modelName = config.Value; } - await using var enumerator = ExecOperationAsync((service, channel, binding, cancel) => { + await using var enumerator = ExecOperationAsync((service, channel, binding, cancel) => { return GenerateEmbeddingsAsync(message, modelName, service, channel, binding, cancel); }, modelName, cancellationToken).GetAsyncEnumerator(); @@ -341,11 +344,11 @@ public async Task GenerateEmbeddingsAsync(string message, CancellationT _logger.LogWarning($"未能获取 {modelName} 模型的嵌入向量"); return Array.Empty(); } - public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmProvider service, LLMChannel channel, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmEmbeddings service, LLMChannel channel, CancellationToken cancellationToken = default) { yield return await service.GenerateEmbeddingsAsync(message, modelName, channel); yield break; } - public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmProvider service, LLMChannel channel, LLMApiBinding binding, CancellationToken cancellationToken = default) { + public async IAsyncEnumerable GenerateEmbeddingsAsync(string message, string modelName, ILlmEmbeddings service, LLMChannel channel, LLMApiBinding binding, CancellationToken cancellationToken = default) { yield return await service.GenerateEmbeddingsAsync(message, modelName, channel, binding); yield break; } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs index 58d296f7..d5d6a13b 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs @@ -239,7 +239,7 @@ public async IAsyncEnumerable RunFromSnapshotAsync( private static string ProviderLabel(LLMChannel channel) { if (channel.Provider == LLMProvider.OpenAI && channel.Gateway != null && - OpenAIService.IsMiniMaxCompatibleEndpoint(channel, string.Empty)) { + OpenAiModelApi.IsMiniMaxCompatibleEndpoint(channel, string.Empty)) { return "MiniMax"; } return channel.Provider.ToString(); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs index a46aa38e..f7614b10 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs @@ -3,16 +3,16 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System.Net.Http; -using TelegramSearchBot.Model.Data; -using Microsoft.Extensions.Logging; using TelegramSearchBot.Attributes; using TelegramSearchBot.Interface.AI.LLM; using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; namespace TelegramSearchBot.Service.AI.LLM { /// - /// Resolves the provider implementation for a channel/protocol/route. - /// Replaces the old ILLMFactory/LLMFactory pair (no IService baggage). + /// Resolves provider data-plane capabilities (catalog/embeddings/vision) and the + /// chat wire transport for a channel/protocol/route. Replaces the old + /// ILLMFactory/LLMFactory and ILlmProvider layers entirely. /// [Injectable(ServiceLifetime.Singleton)] public class LlmProviderRegistry { @@ -22,54 +22,56 @@ public LlmProviderRegistry(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } - public virtual ILlmProvider GetProvider(LLMProvider provider) { + /// Catalog (model discovery) capability for a provider. + public virtual ILlmModelCatalog GetCatalog(LLMProvider provider) { return provider switch { - LLMProvider.OpenAI => _serviceProvider.GetRequiredService(), - LLMProvider.Ollama => _serviceProvider.GetRequiredService(), - LLMProvider.Gemini => _serviceProvider.GetRequiredService(), - LLMProvider.MiniMax => _serviceProvider.GetRequiredService(), - LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), - LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), - LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), + LLMProvider.OpenAI => _serviceProvider.GetRequiredService(), + LLMProvider.Ollama => _serviceProvider.GetRequiredService(), + LLMProvider.Gemini => _serviceProvider.GetRequiredService(), + LLMProvider.MiniMax => _serviceProvider.GetRequiredService(), + LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), + LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), + LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") }; } - public virtual ILlmProvider GetProvider(LlmProtocol protocol) { - return protocol switch { - LlmProtocol.OpenAIChat => _serviceProvider.GetRequiredService(), - LlmProtocol.OpenAIResponses => _serviceProvider.GetRequiredService(), - LlmProtocol.AnthropicMessages => _serviceProvider.GetRequiredService(), - LlmProtocol.Ollama => _serviceProvider.GetRequiredService(), - LlmProtocol.Gemini => _serviceProvider.GetRequiredService(), - _ => throw new KeyNotFoundException($"No LLM provider registered for protocol {protocol}.") + /// Embedding capability for a provider (Anthropic throws NotSupported). + public virtual ILlmEmbeddings GetEmbeddings(LLMProvider provider) { + return provider switch { + LLMProvider.OpenAI => _serviceProvider.GetRequiredService(), + LLMProvider.Ollama => _serviceProvider.GetRequiredService(), + LLMProvider.Gemini => _serviceProvider.GetRequiredService(), + LLMProvider.MiniMax => _serviceProvider.GetRequiredService(), + LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), + LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), + LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), + _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") }; } - public virtual ILlmProvider GetProvider(ResolvedLlmRoute route) { - return route.Binding != null - ? GetProvider(route.Binding.Protocol) - : GetProvider(route.Channel.Provider); - } - - /// Catalog (model discovery) capability for a provider. - public ILlmModelCatalog GetCatalog(LLMProvider provider) => (ILlmModelCatalog)GetProvider(provider); - - /// Embedding capability for a provider (Anthropic throws NotSupported). - public ILlmEmbeddings GetEmbeddings(LLMProvider provider) => (ILlmEmbeddings)GetProvider(provider); - /// Vision image-analysis capability for a provider. - public ILlmVision GetVision(LLMProvider provider) => (ILlmVision)GetProvider(provider); + public virtual ILlmVision GetVision(LLMProvider provider) { + return provider switch { + LLMProvider.OpenAI => _serviceProvider.GetRequiredService(), + LLMProvider.Ollama => _serviceProvider.GetRequiredService(), + LLMProvider.Gemini => _serviceProvider.GetRequiredService(), + LLMProvider.MiniMax => _serviceProvider.GetRequiredService(), + LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), + LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), + LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), + _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") + }; + } /// /// Resolves the chat wire transport for a channel/binding pair. Binding protocol wins - /// over channel provider (e.g. OpenAI channel on an Anthropic-protocol binding). /// Async because Ollama may pull the model on first use. /// public virtual async Task GetTransport(LLMChannel channel, LLMApiBinding binding, string modelName, long chatId, bool nativeTools, bool promptCachingEnabled, bool supportsVision, string systemPrompt, - Microsoft.Extensions.Logging.ILogger logger, System.Net.Http.IHttpClientFactory httpClientFactory) { + ILogger logger, IHttpClientFactory httpClientFactory) { var protocol = binding?.Protocol ?? channel.Provider switch { LLMProvider.OpenAI => LlmProtocol.OpenAIChat, LLMProvider.MiniMax => LlmProtocol.OpenAIChat, diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs index f338e2fe..cec41e29 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs @@ -1,322 +1,322 @@ -#pragma warning disable OPENAI001 -#pragma warning disable SCME0001 - -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Security.Cryptography; -using System.Text; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using OpenAI.Chat; -using OpenAI.Responses; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; - -namespace TelegramSearchBot.Service.AI.LLM { - public static class PromptCachingOutcomes { - public const string Disabled = "disabled"; - public const string Unsupported = "unsupported"; - public const string AttemptedWrite = "attempted_write"; - public const string Hit = "hit"; - public const string Miss = "miss"; - public const string NoSignal = "no_signal"; - public const string Error = "error"; - } - - public sealed class PromptCachingObservation { - public string Provider { get; set; } = string.Empty; - public int ChannelId { get; set; } - public string Model { get; set; } = string.Empty; - public bool PromptCachingEnabled { get; set; } - public string StablePrefixHash { get; set; } = string.Empty; - public string ToolDefinitionHash { get; set; } = string.Empty; - public string CacheOutcome { get; set; } = PromptCachingOutcomes.NoSignal; - public string MissReason { get; set; } - public string PromptCacheKey { get; set; } - public string PromptCacheRetention { get; set; } - public bool CacheKeyAttached { get; set; } - public bool CacheBreakpointInserted { get; set; } - public int? CachedTokenCount { get; set; } - public long? CacheCreationInputTokens { get; set; } - public long? CacheReadInputTokens { get; set; } - public string ProviderUsageJson { get; set; } - } - - public static class PromptCachingHelper { - public const string OpenAiDefaultPromptCacheRetention = "in_memory"; - - private static readonly JsonSerializerSettings CanonicalJsonSettings = new() { - Formatting = Formatting.None, - NullValueHandling = NullValueHandling.Ignore, - }; - - private static readonly ConcurrentDictionary SeenCacheRoutingKeys = new(StringComparer.Ordinal); - - public static string ComputeToolDefinitionHash() { - return ComputeCanonicalHash(McpToolHelper.ExportToolDefinitions()); - } - - public static string ComputeStablePrefixHash(object payload) { - return ComputeCanonicalHash(payload); - } - - public static string BuildOpenAiPromptCacheKey(string provider, string model, string toolDefinitionHash, string stablePrefixHash) { - return string.Join(":", new[] { - SanitizeKeySegment(provider), - SanitizeKeySegment(model), - toolDefinitionHash, - stablePrefixHash, - }); - } - - public static void ApplyOpenAiPromptCaching(ChatCompletionOptions options, string promptCacheKey, string promptCacheRetention) { - ref var patch = ref options.Patch; - patch.Set("$.prompt_cache_key"u8, promptCacheKey); - if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { - patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); - } - } - - public static void ApplyOpenAiPromptCaching(CreateResponseOptions options, string promptCacheKey, string promptCacheRetention) { - ref var patch = ref options.Patch; - patch.Set("$.prompt_cache_key"u8, promptCacheKey); - if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { - patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); - } - } - - /// Logs one OpenAI prompt-caching observation (moved from OpenAIService instance method). - public static void LogOpenAiPromptCachingObservation( - ILogger logger, - string providerName, - LLMChannel channel, - string modelName, - bool promptCachingEnabled, - string toolDefinitionHash, - string stablePrefixHash, - string promptCacheKey, - ChatTokenUsage usage, - bool cacheKeyAttached) { - var cachedTokenCount = usage?.InputTokenDetails?.CachedTokenCount; - var usageJson = usage == null - ? null - : JsonConvert.SerializeObject(new { - usage.InputTokenCount, - usage.OutputTokenCount, - usage.TotalTokenCount, - CachedTokenCount = usage.InputTokenDetails?.CachedTokenCount, - ReasoningTokenCount = usage.OutputTokenDetails?.ReasoningTokenCount, - AudioInputTokenCount = usage.InputTokenDetails?.AudioTokenCount, - AudioOutputTokenCount = usage.OutputTokenDetails?.AudioTokenCount, - }); - var outcome = DetermineOpenAiOutcome( - promptCachingEnabled, - cacheKeyAttached, - promptCacheKey, - cachedTokenCount, - out var missReason); - - LogObservation(logger, new PromptCachingObservation { - Provider = providerName, - ChannelId = channel.Id, - Model = modelName, - PromptCachingEnabled = promptCachingEnabled, - StablePrefixHash = stablePrefixHash, - ToolDefinitionHash = toolDefinitionHash, - CacheOutcome = outcome, - MissReason = missReason, - PromptCacheKey = promptCacheKey, - PromptCacheRetention = OpenAiDefaultPromptCacheRetention, - CacheKeyAttached = cacheKeyAttached, - CachedTokenCount = cachedTokenCount, - ProviderUsageJson = usageJson, - }); - } - - public static string DetermineOpenAiOutcome(bool promptCachingEnabled, bool cacheKeyAttached, string promptCacheKey, int? cachedTokenCount, out string missReason) { - missReason = null; - if (!promptCachingEnabled) { - return PromptCachingOutcomes.Disabled; - } - - if (!cacheKeyAttached || string.IsNullOrWhiteSpace(promptCacheKey)) { - missReason = "prompt_cache_key_not_attached"; - return PromptCachingOutcomes.Unsupported; - } - - if (!cachedTokenCount.HasValue) { - missReason = "cached_tokens_unavailable"; - return PromptCachingOutcomes.NoSignal; - } - - if (cachedTokenCount.Value > 0) { - SeenCacheRoutingKeys[promptCacheKey] = 0; - return PromptCachingOutcomes.Hit; - } - - var firstObservation = SeenCacheRoutingKeys.TryAdd(promptCacheKey, 0); - missReason = firstObservation - ? "first_observation_without_cached_tokens" - : "repeated_observation_without_cached_tokens"; - return firstObservation - ? PromptCachingOutcomes.AttemptedWrite - : PromptCachingOutcomes.Miss; - } - - /// Logs one Anthropic prompt-caching observation (moved from AnthropicService instance method). - public static void LogAnthropicPromptCachingObservation( - ILogger logger, - LLMChannel channel, - string providerName, - string modelName, - bool promptCachingEnabled, - string toolDefinitionHash, - string stablePrefixHash, - bool cacheBreakpointInserted, - long? cacheCreationInputTokens, - long? cacheReadInputTokens, - object usage) { - var usageJson = usage == null ? null : JsonConvert.SerializeObject(usage); - var observationKey = $"{providerName}:{modelName}:{stablePrefixHash}:{toolDefinitionHash}"; - var outcome = DetermineAnthropicOutcome( - promptCachingEnabled, - cacheBreakpointInserted, - observationKey, - cacheCreationInputTokens, - cacheReadInputTokens, - out var missReason); - - LogObservation(logger, new PromptCachingObservation { - Provider = providerName, - ChannelId = channel.Id, - Model = modelName, - PromptCachingEnabled = promptCachingEnabled, - StablePrefixHash = stablePrefixHash, - ToolDefinitionHash = toolDefinitionHash, - CacheOutcome = outcome, - MissReason = missReason, - CacheBreakpointInserted = cacheBreakpointInserted, - CacheCreationInputTokens = cacheCreationInputTokens, - CacheReadInputTokens = cacheReadInputTokens, - ProviderUsageJson = usageJson, - }); - } - - /// Logs one OpenAI Responses prompt-caching observation (moved from OpenAIResponsesService instance method). - public static void LogResponsesPromptCachingObservation( - ILogger logger, - string providerName, - LLMChannel channel, - string modelName, - bool promptCachingEnabled, - string toolDefinitionHash, - string stablePrefixHash, - string promptCacheKey, - ResponseTokenUsage usage, - bool cacheKeyAttached) { - var cachedTokenCount = usage?.InputTokenDetails?.CachedTokenCount; - var usageJson = usage == null - ? null - : JsonConvert.SerializeObject(new { - usage.InputTokenCount, - usage.OutputTokenCount, - usage.TotalTokenCount, - CachedTokenCount = usage.InputTokenDetails?.CachedTokenCount, - ReasoningTokenCount = usage.OutputTokenDetails?.ReasoningTokenCount, - }); - var outcome = DetermineOpenAiOutcome( - promptCachingEnabled, - cacheKeyAttached, - promptCacheKey, - cachedTokenCount, - out var missReason); - - LogObservation(logger, new PromptCachingObservation { - Provider = providerName, - ChannelId = channel.Id, - Model = modelName, - PromptCachingEnabled = promptCachingEnabled, - StablePrefixHash = stablePrefixHash, - ToolDefinitionHash = toolDefinitionHash, - CacheOutcome = outcome, - MissReason = missReason, - PromptCacheKey = promptCacheKey, - PromptCacheRetention = OpenAiDefaultPromptCacheRetention, - CacheKeyAttached = cacheKeyAttached, - CachedTokenCount = cachedTokenCount, - ProviderUsageJson = usageJson, - }); - } - - public static string DetermineAnthropicOutcome(bool promptCachingEnabled, bool cacheBreakpointInserted, string observationKey, long? cacheCreationInputTokens, long? cacheReadInputTokens, out string missReason) { - missReason = null; - if (!promptCachingEnabled) { - return PromptCachingOutcomes.Disabled; - } - - if (!cacheBreakpointInserted) { - missReason = "cache_control_not_inserted"; - return PromptCachingOutcomes.Unsupported; - } - - if (cacheReadInputTokens.GetValueOrDefault() > 0) { - SeenCacheRoutingKeys[observationKey] = 0; - return PromptCachingOutcomes.Hit; - } - - if (cacheCreationInputTokens.GetValueOrDefault() > 0) { - SeenCacheRoutingKeys[observationKey] = 0; - return PromptCachingOutcomes.AttemptedWrite; - } - - var firstObservation = SeenCacheRoutingKeys.TryAdd(observationKey, 0); - missReason = firstObservation - ? "usage_reported_zero_cache_tokens" - : "repeated_prefix_without_cache_read"; - return firstObservation - ? PromptCachingOutcomes.NoSignal - : PromptCachingOutcomes.Miss; - } - - public static void LogObservation(ILogger logger, PromptCachingObservation observation) { - logger.LogInformation( - "Prompt caching observation. Provider={Provider}, ChannelId={ChannelId}, Model={Model}, PromptCachingEnabled={PromptCachingEnabled}, StablePrefixHash={StablePrefixHash}, ToolDefinitionHash={ToolDefinitionHash}, CacheOutcome={CacheOutcome}, MissReason={MissReason}, PromptCacheKey={PromptCacheKey}, PromptCacheRetention={PromptCacheRetention}, CacheKeyAttached={CacheKeyAttached}, CacheBreakpointInserted={CacheBreakpointInserted}, CachedTokenCount={CachedTokenCount}, CacheCreationInputTokens={CacheCreationInputTokens}, CacheReadInputTokens={CacheReadInputTokens}, ProviderUsage={ProviderUsage}", - observation.Provider, - observation.ChannelId, - observation.Model, - observation.PromptCachingEnabled, - observation.StablePrefixHash, - observation.ToolDefinitionHash, - observation.CacheOutcome, - observation.MissReason, - observation.PromptCacheKey, - observation.PromptCacheRetention, - observation.CacheKeyAttached, - observation.CacheBreakpointInserted, - observation.CachedTokenCount, - observation.CacheCreationInputTokens, - observation.CacheReadInputTokens, - observation.ProviderUsageJson); - } - - private static string ComputeCanonicalHash(object payload) { - var json = JsonConvert.SerializeObject(payload, CanonicalJsonSettings); - var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(json)); - return Convert.ToHexString(bytes).ToLowerInvariant(); - } - - private static string SanitizeKeySegment(string value) { - if (string.IsNullOrWhiteSpace(value)) { - return "unknown"; - } - - var builder = new StringBuilder(value.Length); - foreach (var c in value.Trim()) { - builder.Append(char.IsLetterOrDigit(c) || c is '-' or '_' or '.' ? c : '_'); - } - return builder.ToString(); - } - } -} +#pragma warning disable OPENAI001 +#pragma warning disable SCME0001 + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using OpenAI.Chat; +using OpenAI.Responses; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM { + public static class PromptCachingOutcomes { + public const string Disabled = "disabled"; + public const string Unsupported = "unsupported"; + public const string AttemptedWrite = "attempted_write"; + public const string Hit = "hit"; + public const string Miss = "miss"; + public const string NoSignal = "no_signal"; + public const string Error = "error"; + } + + public sealed class PromptCachingObservation { + public string Provider { get; set; } = string.Empty; + public int ChannelId { get; set; } + public string Model { get; set; } = string.Empty; + public bool PromptCachingEnabled { get; set; } + public string StablePrefixHash { get; set; } = string.Empty; + public string ToolDefinitionHash { get; set; } = string.Empty; + public string CacheOutcome { get; set; } = PromptCachingOutcomes.NoSignal; + public string MissReason { get; set; } + public string PromptCacheKey { get; set; } + public string PromptCacheRetention { get; set; } + public bool CacheKeyAttached { get; set; } + public bool CacheBreakpointInserted { get; set; } + public int? CachedTokenCount { get; set; } + public long? CacheCreationInputTokens { get; set; } + public long? CacheReadInputTokens { get; set; } + public string ProviderUsageJson { get; set; } + } + + public static class PromptCachingHelper { + public const string OpenAiDefaultPromptCacheRetention = "in_memory"; + + private static readonly JsonSerializerSettings CanonicalJsonSettings = new() { + Formatting = Formatting.None, + NullValueHandling = NullValueHandling.Ignore, + }; + + private static readonly ConcurrentDictionary SeenCacheRoutingKeys = new(StringComparer.Ordinal); + + public static string ComputeToolDefinitionHash() { + return ComputeCanonicalHash(McpToolHelper.ExportToolDefinitions()); + } + + public static string ComputeStablePrefixHash(object payload) { + return ComputeCanonicalHash(payload); + } + + public static string BuildOpenAiPromptCacheKey(string provider, string model, string toolDefinitionHash, string stablePrefixHash) { + return string.Join(":", new[] { + SanitizeKeySegment(provider), + SanitizeKeySegment(model), + toolDefinitionHash, + stablePrefixHash, + }); + } + + public static void ApplyOpenAiPromptCaching(ChatCompletionOptions options, string promptCacheKey, string promptCacheRetention) { + ref var patch = ref options.Patch; + patch.Set("$.prompt_cache_key"u8, promptCacheKey); + if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { + patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); + } + } + + public static void ApplyOpenAiPromptCaching(CreateResponseOptions options, string promptCacheKey, string promptCacheRetention) { + ref var patch = ref options.Patch; + patch.Set("$.prompt_cache_key"u8, promptCacheKey); + if (!string.IsNullOrWhiteSpace(promptCacheRetention)) { + patch.Set("$.prompt_cache_retention"u8, promptCacheRetention); + } + } + + /// Logs one OpenAI prompt-caching observation (moved from OpenAIService instance method). + public static void LogOpenAiPromptCachingObservation( + ILogger logger, + string providerName, + LLMChannel channel, + string modelName, + bool promptCachingEnabled, + string toolDefinitionHash, + string stablePrefixHash, + string promptCacheKey, + ChatTokenUsage usage, + bool cacheKeyAttached) { + var cachedTokenCount = usage?.InputTokenDetails?.CachedTokenCount; + var usageJson = usage == null + ? null + : JsonConvert.SerializeObject(new { + usage.InputTokenCount, + usage.OutputTokenCount, + usage.TotalTokenCount, + CachedTokenCount = usage.InputTokenDetails?.CachedTokenCount, + ReasoningTokenCount = usage.OutputTokenDetails?.ReasoningTokenCount, + AudioInputTokenCount = usage.InputTokenDetails?.AudioTokenCount, + AudioOutputTokenCount = usage.OutputTokenDetails?.AudioTokenCount, + }); + var outcome = DetermineOpenAiOutcome( + promptCachingEnabled, + cacheKeyAttached, + promptCacheKey, + cachedTokenCount, + out var missReason); + + LogObservation(logger, new PromptCachingObservation { + Provider = providerName, + ChannelId = channel.Id, + Model = modelName, + PromptCachingEnabled = promptCachingEnabled, + StablePrefixHash = stablePrefixHash, + ToolDefinitionHash = toolDefinitionHash, + CacheOutcome = outcome, + MissReason = missReason, + PromptCacheKey = promptCacheKey, + PromptCacheRetention = OpenAiDefaultPromptCacheRetention, + CacheKeyAttached = cacheKeyAttached, + CachedTokenCount = cachedTokenCount, + ProviderUsageJson = usageJson, + }); + } + + public static string DetermineOpenAiOutcome(bool promptCachingEnabled, bool cacheKeyAttached, string promptCacheKey, int? cachedTokenCount, out string missReason) { + missReason = null; + if (!promptCachingEnabled) { + return PromptCachingOutcomes.Disabled; + } + + if (!cacheKeyAttached || string.IsNullOrWhiteSpace(promptCacheKey)) { + missReason = "prompt_cache_key_not_attached"; + return PromptCachingOutcomes.Unsupported; + } + + if (!cachedTokenCount.HasValue) { + missReason = "cached_tokens_unavailable"; + return PromptCachingOutcomes.NoSignal; + } + + if (cachedTokenCount.Value > 0) { + SeenCacheRoutingKeys[promptCacheKey] = 0; + return PromptCachingOutcomes.Hit; + } + + var firstObservation = SeenCacheRoutingKeys.TryAdd(promptCacheKey, 0); + missReason = firstObservation + ? "first_observation_without_cached_tokens" + : "repeated_observation_without_cached_tokens"; + return firstObservation + ? PromptCachingOutcomes.AttemptedWrite + : PromptCachingOutcomes.Miss; + } + + /// Logs one Anthropic prompt-caching observation (moved from AnthropicService instance method). + public static void LogAnthropicPromptCachingObservation( + ILogger logger, + LLMChannel channel, + string providerName, + string modelName, + bool promptCachingEnabled, + string toolDefinitionHash, + string stablePrefixHash, + bool cacheBreakpointInserted, + long? cacheCreationInputTokens, + long? cacheReadInputTokens, + object usage) { + var usageJson = usage == null ? null : JsonConvert.SerializeObject(usage); + var observationKey = $"{providerName}:{modelName}:{stablePrefixHash}:{toolDefinitionHash}"; + var outcome = DetermineAnthropicOutcome( + promptCachingEnabled, + cacheBreakpointInserted, + observationKey, + cacheCreationInputTokens, + cacheReadInputTokens, + out var missReason); + + LogObservation(logger, new PromptCachingObservation { + Provider = providerName, + ChannelId = channel.Id, + Model = modelName, + PromptCachingEnabled = promptCachingEnabled, + StablePrefixHash = stablePrefixHash, + ToolDefinitionHash = toolDefinitionHash, + CacheOutcome = outcome, + MissReason = missReason, + CacheBreakpointInserted = cacheBreakpointInserted, + CacheCreationInputTokens = cacheCreationInputTokens, + CacheReadInputTokens = cacheReadInputTokens, + ProviderUsageJson = usageJson, + }); + } + + /// Logs one OpenAI Responses prompt-caching observation (moved from OpenAIResponsesService instance method). + public static void LogResponsesPromptCachingObservation( + ILogger logger, + string providerName, + LLMChannel channel, + string modelName, + bool promptCachingEnabled, + string toolDefinitionHash, + string stablePrefixHash, + string promptCacheKey, + ResponseTokenUsage usage, + bool cacheKeyAttached) { + var cachedTokenCount = usage?.InputTokenDetails?.CachedTokenCount; + var usageJson = usage == null + ? null + : JsonConvert.SerializeObject(new { + usage.InputTokenCount, + usage.OutputTokenCount, + usage.TotalTokenCount, + CachedTokenCount = usage.InputTokenDetails?.CachedTokenCount, + ReasoningTokenCount = usage.OutputTokenDetails?.ReasoningTokenCount, + }); + var outcome = DetermineOpenAiOutcome( + promptCachingEnabled, + cacheKeyAttached, + promptCacheKey, + cachedTokenCount, + out var missReason); + + LogObservation(logger, new PromptCachingObservation { + Provider = providerName, + ChannelId = channel.Id, + Model = modelName, + PromptCachingEnabled = promptCachingEnabled, + StablePrefixHash = stablePrefixHash, + ToolDefinitionHash = toolDefinitionHash, + CacheOutcome = outcome, + MissReason = missReason, + PromptCacheKey = promptCacheKey, + PromptCacheRetention = OpenAiDefaultPromptCacheRetention, + CacheKeyAttached = cacheKeyAttached, + CachedTokenCount = cachedTokenCount, + ProviderUsageJson = usageJson, + }); + } + + public static string DetermineAnthropicOutcome(bool promptCachingEnabled, bool cacheBreakpointInserted, string observationKey, long? cacheCreationInputTokens, long? cacheReadInputTokens, out string missReason) { + missReason = null; + if (!promptCachingEnabled) { + return PromptCachingOutcomes.Disabled; + } + + if (!cacheBreakpointInserted) { + missReason = "cache_control_not_inserted"; + return PromptCachingOutcomes.Unsupported; + } + + if (cacheReadInputTokens.GetValueOrDefault() > 0) { + SeenCacheRoutingKeys[observationKey] = 0; + return PromptCachingOutcomes.Hit; + } + + if (cacheCreationInputTokens.GetValueOrDefault() > 0) { + SeenCacheRoutingKeys[observationKey] = 0; + return PromptCachingOutcomes.AttemptedWrite; + } + + var firstObservation = SeenCacheRoutingKeys.TryAdd(observationKey, 0); + missReason = firstObservation + ? "usage_reported_zero_cache_tokens" + : "repeated_prefix_without_cache_read"; + return firstObservation + ? PromptCachingOutcomes.NoSignal + : PromptCachingOutcomes.Miss; + } + + public static void LogObservation(ILogger logger, PromptCachingObservation observation) { + logger.LogInformation( + "Prompt caching observation. Provider={Provider}, ChannelId={ChannelId}, Model={Model}, PromptCachingEnabled={PromptCachingEnabled}, StablePrefixHash={StablePrefixHash}, ToolDefinitionHash={ToolDefinitionHash}, CacheOutcome={CacheOutcome}, MissReason={MissReason}, PromptCacheKey={PromptCacheKey}, PromptCacheRetention={PromptCacheRetention}, CacheKeyAttached={CacheKeyAttached}, CacheBreakpointInserted={CacheBreakpointInserted}, CachedTokenCount={CachedTokenCount}, CacheCreationInputTokens={CacheCreationInputTokens}, CacheReadInputTokens={CacheReadInputTokens}, ProviderUsage={ProviderUsage}", + observation.Provider, + observation.ChannelId, + observation.Model, + observation.PromptCachingEnabled, + observation.StablePrefixHash, + observation.ToolDefinitionHash, + observation.CacheOutcome, + observation.MissReason, + observation.PromptCacheKey, + observation.PromptCacheRetention, + observation.CacheKeyAttached, + observation.CacheBreakpointInserted, + observation.CachedTokenCount, + observation.CacheCreationInputTokens, + observation.CacheReadInputTokens, + observation.ProviderUsageJson); + } + + private static string ComputeCanonicalHash(object payload) { + var json = JsonConvert.SerializeObject(payload, CanonicalJsonSettings); + var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(json)); + return Convert.ToHexString(bytes).ToLowerInvariant(); + } + + private static string SanitizeKeySegment(string value) { + if (string.IsNullOrWhiteSpace(value)) { + return "unknown"; + } + + var builder = new StringBuilder(value.Length); + foreach (var c in value.Trim()) { + builder.Append(char.IsLetterOrDigit(c) || c is '-' or '_' or '.' ? c : '_'); + } + return builder.ToString(); + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs similarity index 53% rename from TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs rename to TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs index e60b24b8..58392013 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs @@ -25,29 +25,14 @@ namespace TelegramSearchBot.Service.AI.LLM { [Injectable(ServiceLifetime.Transient)] - public class AnthropicService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { - public string ServiceName => "AnthropicService"; + public class AnthropicModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + private const string ServiceName = "AnthropicModelApi"; - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly DataDbContext _dbContext; private readonly IMessageExtensionService _messageExtensionService; private readonly IHttpClientFactory _httpClientFactory; - private readonly IBotIdentityProvider _botIdentityProvider; - private readonly LlmVisibilityService _llmVisibilityService; - private readonly PromptCachingSettingsService _promptCachingSettingsService; - private readonly LlmChatRunner _chatRunner; - private string _fallbackBotName = string.Empty; - - public string BotName { - get => GetBotNameAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - set { - if (_botIdentityProvider != null) { - _botIdentityProvider.SetIdentity(Env.BotId, value); - } else { - _fallbackBotName = value ?? string.Empty; - } - } - } + private static readonly string[] _anthropicModels = { "claude-sonnet-4-20250514", @@ -59,42 +44,18 @@ public string BotName { "claude-3-haiku-20240307" }; - public AnthropicService( + public AnthropicModelApi( DataDbContext context, - ILogger logger, + ILogger logger, IMessageExtensionService messageExtensionService, - IHttpClientFactory httpClientFactory) - : this(context, logger, messageExtensionService, httpClientFactory, null, null, null) { - } - - public AnthropicService( - DataDbContext context, - ILogger logger, - IMessageExtensionService messageExtensionService, - IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider, - LlmVisibilityService llmVisibilityService = null, - PromptCachingSettingsService promptCachingSettingsService = null, - LlmChatRunner chatRunner = null) { + IHttpClientFactory httpClientFactory) { _logger = logger; _dbContext = context; _messageExtensionService = messageExtensionService; _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _llmVisibilityService = llmVisibilityService; - _promptCachingSettingsService = promptCachingSettingsService; - _chatRunner = chatRunner; - _logger.LogInformation("AnthropicService instance created. McpToolHelper should be initialized at application startup."); + _logger.LogInformation("AnthropicModelApi instance created."); } - private async Task GetBotNameAsync() { - if (_botIdentityProvider == null) { - return _fallbackBotName; - } - - var identity = await _botIdentityProvider.GetIdentityAsync(); - return identity.UserName ?? string.Empty; - } private AnthropicClient CreateClient(LLMChannel channel, LLMApiBinding? binding = null) { var options = new Anthropic.Core.ClientOptions { @@ -113,9 +74,6 @@ private AnthropicClient CreateClient(LLMChannel channel, LLMApiBinding? binding return new AnthropicClient(options); } - private async Task IsPromptCachingEnabledAsync() { - return _promptCachingSettingsService == null || await _promptCachingSettingsService.IsEnabledAsync(); - } internal static CacheControlEphemeral CreateCacheControl() { return new CacheControlEphemeral(); @@ -350,23 +308,6 @@ internal static List EnsureAlternatingRoles(List mes /// /// 检查模型是否支持视觉能力 /// - private async Task CheckVisionSupport(string modelName, int channelId) { - try { - var channelWithModel = await _dbContext.ChannelsWithModel - .Include(c => c.Capabilities) - .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); - - if (channelWithModel?.Capabilities != null) { - return channelWithModel.Capabilities.Any(c => - c.CapabilityName == "vision" && c.CapabilityValue == "true"); - } - - return false; - } catch (Exception ex) { - _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); - return false; - } - } /// /// 尝试加载消息关联的图片文件,转换为PNG格式的字节数组 @@ -446,293 +387,6 @@ private static List ConvertToAnthropicTools(List ExecAsync( - DataMessage message, long ChatId, string modelName, LLMChannel channel, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - var executionContext = new LlmExecutionContext(); - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync( - DataMessage message, long ChatId, string modelName, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync( - DataMessage message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (string.IsNullOrWhiteSpace(modelName)) modelName = "claude-sonnet-4-20250514"; - - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel or ApiKey is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/apikey is not configured."; - yield break; - } - - var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); - var supportsVision = await CheckVisionSupport(modelName, channel.Id); - - var nativeTools = McpToolHelper.GetNativeToolDefinitions(); - var useNativeToolCalling = nativeTools is { Count: > 0 }; - - if (useNativeToolCalling) { - bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); - await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); - bool hasFirst = false; - try { - hasFirst = await enumerator.MoveNextAsync(); - } catch (Exception ex) when (IsToolCallingNotSupportedError(ex)) { - _logger.LogInformation("{ServiceName}: Native tool calling not supported for model {Model}, falling back to XML prompt-based tool calling. Error: {Error}", ServiceName, modelName, ex.Message); - nativeFailed = true; - } - - if (!nativeFailed) { - if (hasFirst) { - yield return enumerator.Current; - while (await enumerator.MoveNextAsync()) { - yield return enumerator.Current; - } - } - yield break; - } - } - - // Fallback: XML prompt-based tool calling - await foreach (var item in ExecWithXmlToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { - yield return item; - } - } - - /// - public async IAsyncEnumerable ExecWithHistoryAsync( - IReadOnlyList history, - DataMessage message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, LlmExecutionContext executionContext, - bool supportsVision, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. - await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { - yield return item; - } - } - private static bool IsToolCallingNotSupportedError(Exception ex) { - var message = ex.Message ?? ""; - return message.Contains("tools", StringComparison.OrdinalIgnoreCase) && - ( message.Contains("not supported", StringComparison.OrdinalIgnoreCase) || - message.Contains("unsupported", StringComparison.OrdinalIgnoreCase) || - message.Contains("invalid", StringComparison.OrdinalIgnoreCase) ); - } - /// - /// Execute LLM with native Anthropic tool calling API. - /// - public async IAsyncEnumerable ExecWithNativeToolCallingAsync( - IReadOnlyList rows, - bool supportsVision, - DataMessage message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - List nativeTools, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - - var botName = await GetBotNameAsync(); - string systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId); - var promptCachingEnabled = await IsPromptCachingEnabledAsync(); - using var client = CreateClient(channel, binding); - - var transport = new Transports.AnthropicMessagesTransport(_logger, client, systemPrompt, modelName, channel, - nativeTools: true, promptCachingEnabled); - var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Anthropic", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = history, - Tools = McpToolHelper.GetLlmToolSpecs(), - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding), - ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = supportsVision, - PromptCachingEnabled = promptCachingEnabled - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } - - private async IAsyncEnumerable ExecWithXmlToolCallingAsync( - IReadOnlyList rows, - bool supportsVision, - DataMessage message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - - var botName = await GetBotNameAsync(); - string systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - var promptCachingEnabled = await IsPromptCachingEnabledAsync(); - using var client = CreateClient(channel, binding); - - var transport = new Transports.AnthropicMessagesTransport(_logger, client, systemPrompt, modelName, channel, - nativeTools: false, promptCachingEnabled); - var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Anthropic", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = history, - Tools = null, - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding), - ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = supportsVision, - PromptCachingEnabled = promptCachingEnabled - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } - - #endregion - - #region ResumeFromSnapshot - - public async IAsyncEnumerable ResumeFromSnapshotAsync( - LlmContinuationSnapshot snapshot, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ResumeFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ResumeFromSnapshotAsync( - LlmContinuationSnapshot snapshot, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - if (snapshot == null) { - _logger.LogError("{ServiceName}: Cannot resume from null snapshot.", ServiceName); - yield break; - } - if (snapshot.NormalizedHistory is not { Count: > 0 } savedHistory) { - _logger.LogError("{ServiceName}: Snapshot {SnapshotId} has no v2 normalized history (legacy v1 snapshots expire via TTL).", ServiceName, snapshot.SnapshotId); - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel or ApiKey is not configured for resume.", ServiceName); - yield break; - } - - var modelName = snapshot.ModelName; - if (string.IsNullOrWhiteSpace(modelName)) modelName = "claude-sonnet-4-20250514"; - - _logger.LogInformation("{ServiceName}: Resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", - ServiceName, snapshot.SnapshotId, snapshot.ChatId, savedHistory.Count); - - string systemPrompt; - if (savedHistory[0].Role == LlmRole.System) { - systemPrompt = savedHistory[0].Text ?? string.Empty; - savedHistory = savedHistory.Skip(1).ToList(); - } else { - var botName = await GetBotNameAsync(); - systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, snapshot.ChatId); - } - - var promptCachingEnabled = await IsPromptCachingEnabledAsync(); - using var client = CreateClient(channel, binding); - - var transport = new Transports.AnthropicMessagesTransport(_logger, client, systemPrompt, modelName, channel, - nativeTools: false, promptCachingEnabled); - - var meta = new LlmToolLoopMeta { - ChatId = snapshot.ChatId, - OriginalMessageId = snapshot.OriginalMessageId, - UserId = snapshot.UserId, - ModelName = modelName, - Provider = "Anthropic", - ChannelId = channel.Id, - BaseCycles = snapshot.CyclesSoFar, - InitialContent = snapshot.LastAccumulatedContent ?? string.Empty - }; - var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = savedHistory, - Tools = null, - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding), - ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = false, - PromptCachingEnabled = promptCachingEnabled - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } - - #endregion - /// /// Serialize Anthropic message list to portable format for snapshot persistence. /// diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/GeminiModelApi.cs similarity index 63% rename from TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs rename to TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/GeminiModelApi.cs index 5243a5b0..e0650b64 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/GeminiModelApi.cs @@ -23,58 +23,23 @@ namespace TelegramSearchBot.Service.AI.LLM { [Injectable(ServiceLifetime.Transient)] - public class GeminiService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { - public string ServiceName => "GeminiService"; - private readonly ILogger _logger; + public class GeminiModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + private const string ServiceName = "GeminiModelApi"; + private readonly ILogger _logger; private readonly DataDbContext _dbContext; private readonly Dictionary _chatSessions = new(); private readonly IHttpClientFactory _httpClientFactory; - private readonly IBotIdentityProvider _botIdentityProvider; - private readonly LlmVisibilityService _llmVisibilityService; - private readonly LlmChatRunner _chatRunner; - private string _fallbackBotName = string.Empty; - public string BotName { - get => GetBotNameAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - set { - if (_botIdentityProvider != null) { - _botIdentityProvider.SetIdentity(Env.BotId, value); - } else { - _fallbackBotName = value ?? string.Empty; - } - } - } - - public GeminiService( - DataDbContext context, - ILogger logger, - IHttpClientFactory httpClientFactory) - : this(context, logger, httpClientFactory, null, null) { - } - public GeminiService( + public GeminiModelApi( DataDbContext context, - ILogger logger, - IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider, - LlmVisibilityService llmVisibilityService = null, - LlmChatRunner chatRunner = null) { + ILogger logger, + IHttpClientFactory httpClientFactory) { _logger = logger; _dbContext = context; _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _llmVisibilityService = llmVisibilityService; - _chatRunner = chatRunner; - _logger.LogInformation("GeminiService instance created"); + _logger.LogInformation("GeminiModelApi instance created"); } - private async Task GetBotNameAsync() { - if (_botIdentityProvider == null) { - return _fallbackBotName; - } - - var identity = await _botIdentityProvider.GetIdentityAsync(); - return identity.UserName ?? string.Empty; - } private void AddMessageToHistory(List chatHistory, long fromUserId, string content) { AddMessageToHistory(chatHistory, fromUserId, content, null); @@ -107,23 +72,6 @@ private void AddMessageToHistory(List chatHistory, l } } - private async Task CheckVisionSupport(string modelName, int channelId) { - try { - var channelWithModel = await _dbContext.ChannelsWithModel - .Include(c => c.Capabilities) - .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); - - if (channelWithModel?.Capabilities != null) { - return channelWithModel.Capabilities.Any(c => - c.CapabilityName == "vision" && c.CapabilityValue == "true"); - } - - return false; - } catch (Exception ex) { - _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); - return false; - } - } /// /// 尝试加载消息关联的图片文件,转换为PNG格式的字节数组 @@ -290,102 +238,10 @@ private ModelWithCapabilities InferGeminiModelCapabilities(string modelName, Gen return model; } - public async IAsyncEnumerable ExecAsync( - Message message, - long ChatId, - string modelName, - LLMChannel channel, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - var executionContext = new LlmExecutionContext(); - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync( - Message message, - long ChatId, - string modelName, - LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - public async IAsyncEnumerable ExecAsync( - Message message, - long ChatId, - string modelName, - LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - if (string.IsNullOrWhiteSpace(modelName)) modelName = "gemini-1.5-flash"; - var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); - var model = googleAI.CreateGenerativeModel("models/" + modelName); - - if (_llmVisibilityService != null && - message != null && - await _llmVisibilityService.IsUserInvisibleAsync(ChatId, message.FromUserId, cancellationToken)) { - yield break; - } - - var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); - var supportsVision = await CheckVisionSupport(modelName, channel.Id); - - var botName = await GetBotNameAsync(); - var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - - var transport = new Transports.GeminiTransport(model, supportsVision); - var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Gemini", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = history, - Tools = null, - Config = new LlmTransportConfig { - ModelName = modelName, - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = supportsVision - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } /// - public async IAsyncEnumerable ExecWithHistoryAsync( - IReadOnlyList history, - Message message, - long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, LlmExecutionContext executionContext, - bool supportsVision, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. - await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { - yield return item; - } - } public async Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { if (channel == null || string.IsNullOrWhiteSpace(channel.ApiKey)) { _logger.LogError("{ServiceName}: Channel or ApiKey is not configured", ServiceName); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OllamaModelApi.cs similarity index 68% rename from TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs rename to TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OllamaModelApi.cs index 07b8000e..60eab172 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OllamaModelApi.cs @@ -19,7 +19,7 @@ using TelegramSearchBot.Attributes; using TelegramSearchBot.Common; using TelegramSearchBot.Interface; -using TelegramSearchBot.Interface.AI.LLM; // For ILlmProvider +using TelegramSearchBot.Interface.AI.LLM; using TelegramSearchBot.Model; using TelegramSearchBot.Model.AI; using TelegramSearchBot.Model.Data; @@ -27,60 +27,26 @@ namespace TelegramSearchBot.Service.AI.LLM { // Standalone implementation, not using BaseLlmService [Injectable(ServiceLifetime.Transient)] - public class OllamaService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { - public string ServiceName => "OllamaService"; + public class OllamaModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + private const string ServiceName = "OllamaModelApi"; - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly DataDbContext _dbContext; private readonly IServiceProvider _serviceProvider; private readonly IHttpClientFactory _httpClientFactory; - private readonly IBotIdentityProvider _botIdentityProvider; - private readonly LlmChatRunner _chatRunner; - private string _fallbackBotName = string.Empty; - public string BotName { - get => GetBotNameAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - set { - if (_botIdentityProvider != null) { - _botIdentityProvider.SetIdentity(Env.BotId, value); - } else { - _fallbackBotName = value ?? string.Empty; - } - } - } - - public OllamaService( - DataDbContext context, - ILogger logger, - IServiceProvider serviceProvider, - IHttpClientFactory httpClientFactory) - : this(context, logger, serviceProvider, httpClientFactory, null) { - } - // Constructor requires dependencies needed directly by this class - public OllamaService( + public OllamaModelApi( DataDbContext context, - ILogger logger, + ILogger logger, IServiceProvider serviceProvider, - IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider, - LlmChatRunner chatRunner = null) { + IHttpClientFactory httpClientFactory) { _logger = logger; _dbContext = context; _serviceProvider = serviceProvider; _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _chatRunner = chatRunner; - _logger.LogInformation("OllamaService instance created. McpToolHelper should be initialized at application startup."); + _logger.LogInformation("OllamaModelApi instance created"); } - private async Task GetBotNameAsync() { - if (_botIdentityProvider == null) { - return _fallbackBotName; - } - - var identity = await _botIdentityProvider.GetIdentityAsync(); - return identity.UserName ?? string.Empty; - } // --- Helper methods specific to this service --- @@ -118,101 +84,10 @@ public async Task CheckAndPullModelAsync(OllamaApiClient ollama, string mo } } - public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - var executionContext = new LlmExecutionContext(); - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - modelName = modelName ?? Env.OllamaModelName; - if (string.IsNullOrWhiteSpace(modelName)) { - _logger.LogError("{ServiceName}: Model name is not configured.", ServiceName); - yield return $"Error: {ServiceName} model name is not configured."; - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint)) { - _logger.LogError("{ServiceName}: Channel or Gateway is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/gateway is not configured."; - yield break; - } - - // --- Client and Model Setup --- - HttpClient httpClient = _httpClientFactory?.CreateClient("OllamaClient") ?? new HttpClient(); - httpClient.BaseAddress = new Uri(endpoint); - var ollama = new OllamaApiClient(httpClient, modelName); + - if (!await CheckAndPullModelAsync(ollama, modelName)) { - yield return $"Error: Could not check or pull Ollama model '{modelName}'."; - yield break; - } - ollama.SelectedModel = modelName; - - var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, null, ChatId, message, cancellationToken); - var botName = await GetBotNameAsync(); - var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - - var transport = new Transports.OllamaTransport(_logger, ollama, systemPrompt); - var history = LlmHistoryProjector.Project(rows, supportsVision: false, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "Ollama", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = history, - Tools = null, - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = endpoint, - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = false - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } /// - public async IAsyncEnumerable ExecWithHistoryAsync( - IReadOnlyList history, - Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, LlmExecutionContext executionContext, - bool supportsVision, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - // Glue (client construction, model pull check, fallback dispatch) lives in LlmChatRunner. - await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { - yield return item; - } - } public virtual async Task> GetAllModels(LLMChannel channel) { if (channel == null || string.IsNullOrWhiteSpace(channel.Gateway)) { return Enumerable.Empty(); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OpenAiModelApi.cs similarity index 70% rename from TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs rename to TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OpenAiModelApi.cs index 3a9f959b..7f58333b 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OpenAiModelApi.cs @@ -33,9 +33,7 @@ namespace TelegramSearchBot.Service.AI.LLM { // Standalone implementation, not inheriting from BaseLlmService [Injectable(ServiceLifetime.Transient)] - public class OpenAIService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { - public string ServiceName => "OpenAIService"; - + public class OpenAiModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { /// /// Mutable accumulator for streaming tool call updates. /// @@ -120,70 +118,27 @@ private static string SanitizeAndTruncateArguments(string arguments, int maxChar : sanitized.Substring(0, maxChars) + $"..."; } - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly DataDbContext _dbContext; private readonly IHttpClientFactory _httpClientFactory; private readonly IMessageExtensionService _messageExtensionService; - private readonly IBotIdentityProvider _botIdentityProvider; - private readonly IGroupLlmSettingsService _groupLlmSettingsService; - private readonly LlmVisibilityService _llmVisibilityService; - private readonly PromptCachingSettingsService _promptCachingSettingsService; - private readonly LlmChatRunner _chatRunner; - private string _fallbackBotName = string.Empty; - - public OpenAIService( - DataDbContext context, - ILogger logger, - IMessageExtensionService messageExtensionService, - IHttpClientFactory httpClientFactory) - : this(context, logger, messageExtensionService, httpClientFactory, null, null, null, null) { - } - public OpenAIService( + private const string ServiceName = "OpenAiModelApi"; + + public OpenAiModelApi( DataDbContext context, - ILogger logger, + ILogger logger, IMessageExtensionService messageExtensionService, - IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider, - IGroupLlmSettingsService groupLlmSettingsService, - LlmVisibilityService llmVisibilityService = null, - PromptCachingSettingsService promptCachingSettingsService = null, - LlmChatRunner chatRunner = null) { + IHttpClientFactory httpClientFactory) { _logger = logger; _dbContext = context; _messageExtensionService = messageExtensionService; _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _groupLlmSettingsService = groupLlmSettingsService; - _llmVisibilityService = llmVisibilityService; - _promptCachingSettingsService = promptCachingSettingsService; - _chatRunner = chatRunner; - _logger.LogInformation("OpenAIService instance created. McpToolHelper should be initialized at application startup."); + _logger.LogInformation("OpenAiModelApi instance created. McpToolHelper should be initialized at application startup."); } - public string BotName { - get => GetBotNameAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - set { - if (_botIdentityProvider != null) { - _botIdentityProvider.SetIdentity(Env.BotId, value); - } else { - _fallbackBotName = value ?? string.Empty; - } - } - } - - private async Task GetBotNameAsync() { - if (_botIdentityProvider == null) { - return _fallbackBotName; - } - var identity = await _botIdentityProvider.GetIdentityAsync(); - return identity.UserName ?? string.Empty; - } - private async Task IsPromptCachingEnabledAsync() { - return _promptCachingSettingsService == null || await _promptCachingSettingsService.IsEnabledAsync(); - } private static List GetStablePrefixMessages(List providerHistory, bool excludeDynamicTail) { if (!excludeDynamicTail || providerHistory.Count <= 1) { @@ -904,316 +859,21 @@ private ModelWithCapabilities ParseLMStudioModelCapabilities(dynamic modelData) } } - private async Task CheckVisionSupport(string modelName, int channelId) { - try { - var channelWithModel = await _dbContext.ChannelsWithModel - .Include(c => c.Capabilities) - .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); - if (channelWithModel?.Capabilities != null) { - return channelWithModel.Capabilities.Any(c => - c.CapabilityName == "vision" && c.CapabilityValue == "true"); - } - return false; - } catch (Exception ex) { - _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); - return false; - } - } - public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - var executionContext = new LlmExecutionContext(); - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; - - if (string.IsNullOrWhiteSpace(modelName)) { - _logger.LogError("{ServiceName}: Model name is not configured.", ServiceName); - yield return $"Error: {ServiceName} model name is not configured."; - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/gateway/apikey is not configured."; - yield break; - } - - var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); - var supportsVision = await CheckVisionSupport(modelName, channel.Id); - - // Try native tool calling first; fall back to XML prompt-based if it fails - var nativeTools = McpToolHelper.GetNativeToolDefinitions(); - var useNativeToolCalling = nativeTools is { Count: > 0 }; - - if (useNativeToolCalling) { - bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, nativeTools, cancellationToken); - await using var enumerator = nativeEnumerator.GetAsyncEnumerator(cancellationToken); - bool hasFirst = false; - try { - hasFirst = await enumerator.MoveNextAsync(); - } catch (Exception ex) when (IsToolCallingNotSupportedError(ex)) { - _logger.LogInformation("{ServiceName}: Native tool calling not supported for model {Model}, falling back to XML prompt-based tool calling. Error: {Error}", ServiceName, modelName, ex.Message); - nativeFailed = true; - } - - if (!nativeFailed) { - if (hasFirst) { - yield return enumerator.Current; - while (await enumerator.MoveNextAsync()) { - yield return enumerator.Current; - } - } - yield break; - } - } - - // Fallback: XML prompt-based tool calling - await foreach (var item in ExecWithXmlToolCallingAsync(rows, supportsVision, message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { - yield return item; - } - } /// - public async IAsyncEnumerable ExecWithHistoryAsync( - IReadOnlyList history, - Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, LlmExecutionContext executionContext, - bool supportsVision, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. - await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { - yield return item; - } - } - private static bool IsToolCallingNotSupportedError(Exception ex) { - var message = ex.Message ?? ""; - // Check for ClientResultException with specific HTTP status codes - if (ex is ClientResultException clientEx) { - // 400 Bad Request with tool-related error message - if (clientEx.Status == 400 && message.Contains("tool", StringComparison.OrdinalIgnoreCase)) { - return true; - } - } - // Common error patterns when a model/API doesn't support tool calling - return message.Contains("tools", StringComparison.OrdinalIgnoreCase) && - ( message.Contains("not supported", StringComparison.OrdinalIgnoreCase) || - message.Contains("unsupported", StringComparison.OrdinalIgnoreCase) || - message.Contains("invalid", StringComparison.OrdinalIgnoreCase) ) || - message.Contains("unrecognized request argument", StringComparison.OrdinalIgnoreCase); - } - private async IAsyncEnumerable ExecWithNativeToolCallingAsync( - IReadOnlyList rows, - bool supportsVision, - Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - List nativeTools, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - - var botName = await GetBotNameAsync(); - string systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId); - var promptCachingEnabled = channel.Provider == LLMProvider.OpenAI && await IsPromptCachingEnabledAsync(); - var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); - - var clientParts = BuildClientParts(channel, binding, ChatId); - var transport = new Transports.OpenAiChatTransport(_logger, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, - nativeTools: true, promptCachingEnabled, includeEmptyReasoningContent); - var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "OpenAI", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = history, - Tools = McpToolHelper.GetLlmToolSpecs(), - Config = BuildConfig(modelName, channel, binding, supportsVision, promptCachingEnabled, includeEmptyReasoningContent), - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } - private async IAsyncEnumerable ExecWithXmlToolCallingAsync( - IReadOnlyList rows, - bool supportsVision, - Model.Data.Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - - var botName = await GetBotNameAsync(); - string systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - var promptCachingEnabled = channel.Provider == LLMProvider.OpenAI && await IsPromptCachingEnabledAsync(); - var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); - - var clientParts = BuildClientParts(channel, binding, ChatId); - var transport = new Transports.OpenAiChatTransport(_logger, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, - nativeTools: false, promptCachingEnabled, includeEmptyReasoningContent); - var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); - - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "OpenAI", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = history, - Tools = null, - Config = BuildConfig(modelName, channel, binding, supportsVision, promptCachingEnabled, includeEmptyReasoningContent), - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } - private LlmTransportConfig BuildConfig(string modelName, LLMChannel channel, LLMApiBinding binding, - bool supportsVision, bool promptCachingEnabled, bool includeEmptyReasoningContent) { - return new LlmTransportConfig { - ModelName = modelName, - Endpoint = NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)), - ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = supportsVision, - PromptCachingEnabled = promptCachingEnabled, - IncludeEmptyReasoningContent = includeEmptyReasoningContent - }; - } - private (HttpClientPipelineTransport Transport, ApiKeyCredential Credential, OpenAIClientOptions Options) BuildClientParts(LLMChannel channel, LLMApiBinding binding, long chatId) { - // ponytail: HttpClient must outlive this method (transport persists per run); factory-managed, not disposed here. - var httpClient = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, channel, binding, $"tsb-{chatId}"); - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding))), - Transport = new HttpClientPipelineTransport(httpClient), - }; - return (new HttpClientPipelineTransport(httpClient), new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); - } /// /// Resume LLM execution from a saved v2 (normalized) snapshot. /// - public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ResumeFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - public async IAsyncEnumerable ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - if (snapshot == null) { - _logger.LogError("{ServiceName}: Cannot resume from null snapshot.", ServiceName); - yield break; - } - if (snapshot.NormalizedHistory is not { Count: > 0 } savedHistory) { - _logger.LogError("{ServiceName}: Snapshot {SnapshotId} has no v2 normalized history (legacy v1 snapshots expire via TTL).", ServiceName, snapshot.SnapshotId); - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured for resume.", ServiceName); - yield break; - } - - var modelName = snapshot.ModelName; - if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; - - _logger.LogInformation("{ServiceName}: Resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", - ServiceName, snapshot.SnapshotId, snapshot.ChatId, savedHistory.Count); - - // Peel the stored system prompt off the normalized history. - string systemPrompt; - if (savedHistory[0].Role == LlmRole.System) { - systemPrompt = savedHistory[0].Text ?? string.Empty; - savedHistory = savedHistory.Skip(1).ToList(); - } else { - var botName = await GetBotNameAsync(); - systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, snapshot.ChatId); - } - - var promptCachingEnabled = channel.Provider == LLMProvider.OpenAI && await IsPromptCachingEnabledAsync(); - var includeEmptyReasoningContent = binding == null && ShouldIncludeEmptyReasoningContent(channel, modelName); - var clientParts = BuildClientParts(channel, binding, snapshot.ChatId); - var transport = new Transports.OpenAiChatTransport(_logger, clientParts.Transport, clientParts.Credential, clientParts.Options, modelName, channel, - nativeTools: false, promptCachingEnabled, includeEmptyReasoningContent); - - var meta = new LlmToolLoopMeta { - ChatId = snapshot.ChatId, - OriginalMessageId = snapshot.OriginalMessageId, - UserId = snapshot.UserId, - ModelName = modelName, - Provider = "OpenAI", - ChannelId = channel.Id, - BaseCycles = snapshot.CyclesSoFar, - InitialContent = snapshot.LastAccumulatedContent ?? string.Empty - }; - var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = savedHistory, - Tools = null, - Config = BuildConfig(modelName, channel, binding, false, promptCachingEnabled, includeEmptyReasoningContent), - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } /// /// Stateless transport adapter for the OpenAI Chat Completions API: converts the @@ -1423,35 +1083,6 @@ public async Task GenerateEmbeddingsAsync(string text, string modelName } } - public async Task<(string, string)> SetModel(string ModelName, long ChatId) { - if (_groupLlmSettingsService != null) { - var (previous, current) = await _groupLlmSettingsService.SetModelAsync(ChatId, ModelName); - return (previous, current); - } - - var GroupSetting = await _dbContext.GroupSettings - .Where(s => s.GroupId == ChatId) - .FirstOrDefaultAsync(); - var CurrentModelName = GroupSetting?.LLMModelName; - if (GroupSetting is null) { - await _dbContext.AddAsync(new GroupSettings() { GroupId = ChatId, LLMModelName = ModelName }); - } else { - GroupSetting.LLMModelName = ModelName; - } - await _dbContext.SaveChangesAsync(); - return (CurrentModelName ?? "Default", ModelName); - } - public async Task GetModel(long ChatId) { - if (_groupLlmSettingsService != null) { - return await _groupLlmSettingsService.GetModelAsync(ChatId); - } - - var GroupSetting = await _dbContext.GroupSettings.AsNoTracking() - .Where(s => s.GroupId == ChatId) - .FirstOrDefaultAsync(); - var ModelName = GroupSetting?.LLMModelName; - return ModelName; - } public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null) { return await AnalyzeImageAsync(photoPath, modelName, channel, null, prompt); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/ResponsesModelApi.cs similarity index 62% rename from TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs rename to TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/ResponsesModelApi.cs index 0e2386e6..748d6b49 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/ResponsesModelApi.cs @@ -33,11 +33,11 @@ namespace TelegramSearchBot.Service.AI.LLM { /// OpenAI Responses API 服务实现。 /// 使用 OpenAI 新的 /v1/responses 接口(Responses API), /// 支持 built-in tools(web_search, file_search 等)和 Function calling。 - /// 与现有的 OpenAIService(Chat Completions API)并存。 + /// 与现有的 OpenAiModelApi(Chat Completions API)并存。 /// [Injectable(ServiceLifetime.Transient)] - public class OpenAIResponsesService : IService, ILlmProvider, ILlmModelCatalog, ILlmEmbeddings, ILlmVision { - public string ServiceName => "OpenAIResponsesService"; + public class ResponsesModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + private const string ServiceName = "ResponsesModelApi"; /// /// Mutable accumulator for streaming tool call argument deltas. @@ -48,66 +48,24 @@ internal class ResponsesToolCallAccumulator { public StringBuilder Arguments { get; } = new StringBuilder(); } - private readonly ILogger _logger; - private readonly IBotIdentityProvider _botIdentityProvider; - private string _fallbackBotName = string.Empty; - public string BotName { - get => GetBotNameAsync().ConfigureAwait(false).GetAwaiter().GetResult(); - set { - if (_botIdentityProvider != null) { - _botIdentityProvider.SetIdentity(Env.BotId, value); - } else { - _fallbackBotName = value ?? string.Empty; - } - } - } + private readonly ILogger _logger; private readonly DataDbContext _dbContext; private readonly IHttpClientFactory _httpClientFactory; private readonly IMessageExtensionService _messageExtensionService; - private readonly LlmVisibilityService _llmVisibilityService; - private readonly PromptCachingSettingsService _promptCachingSettingsService; - private readonly LlmChatRunner _chatRunner; - public OpenAIResponsesService( + public ResponsesModelApi( DataDbContext context, - ILogger logger, + ILogger logger, IMessageExtensionService messageExtensionService, - IHttpClientFactory httpClientFactory) - : this(context, logger, messageExtensionService, httpClientFactory, null, null, null) { - } - - public OpenAIResponsesService( - DataDbContext context, - ILogger logger, - IMessageExtensionService messageExtensionService, - IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider, - LlmVisibilityService llmVisibilityService = null, - PromptCachingSettingsService promptCachingSettingsService = null, - LlmChatRunner chatRunner = null) { + IHttpClientFactory httpClientFactory) { _logger = logger; _dbContext = context; _messageExtensionService = messageExtensionService; _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _llmVisibilityService = llmVisibilityService; - _promptCachingSettingsService = promptCachingSettingsService; - _chatRunner = chatRunner; - _logger.LogInformation("OpenAIResponsesService instance created."); + _logger.LogInformation("ResponsesModelApi instance created."); } - private async Task GetBotNameAsync() { - if (_botIdentityProvider == null) { - return _fallbackBotName; - } - - var identity = await _botIdentityProvider.GetIdentityAsync(); - return identity.UserName ?? string.Empty; - } - private async Task IsPromptCachingEnabledAsync() { - return _promptCachingSettingsService == null || await _promptCachingSettingsService.IsEnabledAsync(); - } private static List GetStablePrefixInputItems(List inputItems, bool excludeDynamicTail) { if (!excludeDynamicTail || inputItems.Count == 0) { @@ -136,177 +94,14 @@ internal static (string toolDefinitionHash, string stablePrefixHash, string prom // ======================================================================== - // ILlmProvider Implementation + // Capability implementation // ======================================================================== - public async IAsyncEnumerable ExecAsync( - Message message, long ChatId, string modelName, LLMChannel channel, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - var executionContext = new LlmExecutionContext(); - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync( - Message message, long ChatId, string modelName, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ExecAsync(message, ChatId, modelName, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - - public async IAsyncEnumerable ExecAsync( - Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(apiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured.", ServiceName); - yield return $"Error: {ServiceName} channel/gateway/apikey is not configured."; - yield break; - } - - var rows = await LlmHistoryQueryService.LoadAsync(_dbContext, _llmVisibilityService, ChatId, message, cancellationToken); - var supportsVision = await CheckVisionSupport(modelName, channel.Id); - var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); - var transport = new Transports.ResponsesTransport(_httpClientFactory, _logger, endpoint, apiKey, binding, channel, supportsVision, promptCachingEnabled); - var botName = await GetBotNameAsync(); - var history = LlmHistoryProjector.Project(rows, supportsVision, _logger); - var meta = new LlmToolLoopMeta { - ChatId = ChatId, - OriginalMessageId = message.MessageId, - UserId = message.FromUserId, - ModelName = modelName, - Provider = "OpenAIResponses", - ChannelId = channel.Id - }; - var toolContext = new ToolContext { ChatId = ChatId, UserId = message.FromUserId, MessageId = message.MessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, ChatId), - History = history, - Tools = McpToolHelper.GetLlmToolSpecs(), - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = endpoint, - ApiKey = apiKey, - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = supportsVision, - PromptCachingEnabled = promptCachingEnabled - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } /// - public async IAsyncEnumerable ExecWithHistoryAsync( - IReadOnlyList history, - Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, LlmExecutionContext executionContext, - bool supportsVision, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - // Glue (client construction, native/XML fallback dispatch) lives in LlmChatRunner. - await foreach (var item in _chatRunner.RunAsync(history, message, ChatId, modelName, channel, binding, executionContext, supportsVision, cancellationToken)) { - yield return item; - } - } - public async IAsyncEnumerable ResumeFromSnapshotAsync( - LlmContinuationSnapshot snapshot, LLMChannel channel, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - await foreach (var item in ResumeFromSnapshotAsync(snapshot, channel, null, executionContext, cancellationToken)) { - yield return item; - } - } - public async IAsyncEnumerable ResumeFromSnapshotAsync( - LlmContinuationSnapshot snapshot, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { - using var chatContentLogScope = LoggerHolders.PushChatContentLogScope(); - if (snapshot == null) { - _logger.LogError("{ServiceName}: Cannot resume from null snapshot.", ServiceName); - yield break; - } - if (snapshot.NormalizedHistory is not { Count: > 0 } savedHistory) { - _logger.LogError("{ServiceName}: Snapshot {SnapshotId} has no v2 normalized history (legacy v1 snapshots expire via TTL).", ServiceName, snapshot.SnapshotId); - yield break; - } - var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); - var resolvedApiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (channel == null || string.IsNullOrWhiteSpace(endpoint) || (binding?.AuthProfile != LlmAuthProfile.None && string.IsNullOrWhiteSpace(resolvedApiKey))) { - _logger.LogError("{ServiceName}: Channel, Gateway, or ApiKey is not configured for resume.", ServiceName); - yield break; - } - - var modelName = snapshot.ModelName; - if (string.IsNullOrWhiteSpace(modelName)) modelName = Env.OpenAIModelName; - - _logger.LogInformation("{ServiceName}: Resuming from snapshot {SnapshotId} for ChatId {ChatId}, restoring {HistoryCount} history entries.", - ServiceName, snapshot.SnapshotId, snapshot.ChatId, savedHistory.Count); - - string systemPrompt; - if (savedHistory[0].Role == LlmRole.System) { - systemPrompt = savedHistory[0].Text ?? string.Empty; - savedHistory = savedHistory.Skip(1).ToList(); - } else { - var botName = await GetBotNameAsync(); - systemPrompt = McpToolHelper.FormatSystemPromptForNativeToolCalling(botName, snapshot.ChatId); - } - - var promptCachingEnabled = channel.Provider == LLMProvider.ResponsesAPI && await IsPromptCachingEnabledAsync(); - var transport = new Transports.ResponsesTransport(_httpClientFactory, _logger, endpoint, resolvedApiKey, binding, channel, false, promptCachingEnabled); - - var meta = new LlmToolLoopMeta { - ChatId = snapshot.ChatId, - OriginalMessageId = snapshot.OriginalMessageId, - UserId = snapshot.UserId, - ModelName = modelName, - Provider = "OpenAIResponses", - ChannelId = channel.Id, - BaseCycles = snapshot.CyclesSoFar, - InitialContent = snapshot.LastAccumulatedContent ?? string.Empty - }; - var toolContext = new ToolContext { ChatId = snapshot.ChatId, UserId = snapshot.UserId, MessageId = snapshot.OriginalMessageId }; - var run = new LlmAgentRunRequest { - Transport = transport, - SystemPrompt = systemPrompt, - History = savedHistory, - Tools = McpToolHelper.GetLlmToolSpecs(), - Config = new LlmTransportConfig { - ModelName = modelName, - Endpoint = endpoint, - ApiKey = resolvedApiKey, - Provider = channel.Provider, - Binding = binding, - Channel = channel, - SupportsVision = false, - PromptCachingEnabled = promptCachingEnabled - }, - ToolContext = toolContext, - Meta = meta, - ExecutionContext = executionContext - }; - await foreach (var item in LlmToolLoop.RunAsync(run, cancellationToken)) { - yield return item; - } - } /// /// Native transport for the OpenAI Responses API: converts the normalized history to @@ -330,22 +125,6 @@ public bool IsSameSender(Message message1, Message message2) { // ======================================================================== - private async Task CheckVisionSupport(string modelName, int channelId) { - try { - var channelWithModel = await _dbContext.ChannelsWithModel - .Include(c => c.Capabilities) - .FirstOrDefaultAsync(c => c.ModelName == modelName && c.LLMChannelId == channelId && !c.IsDeleted); - - if (channelWithModel?.Capabilities != null) { - return channelWithModel.Capabilities.Any(c => - c.CapabilityName == "vision" && c.CapabilityValue == "true"); - } - return false; - } catch (Exception ex) { - _logger.LogDebug(ex, "检查模型视觉能力时出错: {ModelName}", modelName); - return false; - } - } // ======================================================================== // Helper: IsSameSender @@ -373,7 +152,7 @@ private byte[] TryLoadMessagePhoto(long chatId, long messageId) { } // ======================================================================== - // Helper: Model capability parsing (from OpenAIService) + // Helper: Model capability parsing (from OpenAiModelApi) // ======================================================================== @@ -552,10 +331,10 @@ private static List DeserializeResponseItemsFromSnapshot(List 0 ? parts[0] : ""; - string callId = OpenAIService.NormalizeToolCallId(rawCallId); + string callId = OpenAiModelApi.NormalizeToolCallId(rawCallId); lastResolvedCallId = callId; - string name = OpenAIService.NormalizeToolCallName(parts.Length > 1 ? parts[1] : "unknown"); - string argsJson = OpenAIService.NormalizeToolCallArguments(parts.Length > 2 ? parts[2] : "{}"); + string name = OpenAiModelApi.NormalizeToolCallName(parts.Length > 1 ? parts[1] : "unknown"); + string argsJson = OpenAiModelApi.NormalizeToolCallArguments(parts.Length > 2 ? parts[2] : "{}"); result.Add(ResponseItem.CreateFunctionCallItem( callId, name, @@ -569,7 +348,7 @@ private static List DeserializeResponseItemsFromSnapshot(List 0 ? parts[0] : ""; string callId = string.IsNullOrWhiteSpace(rawCallId) && !string.IsNullOrWhiteSpace(lastResolvedCallId) ? lastResolvedCallId - : OpenAIService.NormalizeToolCallId(rawCallId); + : OpenAiModelApi.NormalizeToolCallId(rawCallId); lastResolvedCallId = callId; string output = parts.Length > 1 ? parts[1] : ""; result.Add(ResponseItem.CreateFunctionCallOutputItem(callId, output)); @@ -807,107 +586,8 @@ public async Task AnalyzeImageAsync(string photoPath, string modelName, } } - // ======================================================================== - // Helper: Build ResponseItem list from chat history // ======================================================================== - private async Task> BuildResponseInputItemsAsync( - long ChatId, Message inputToken, bool supportsVision) { - - var messages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == ChatId && m.DateTime > DateTime.UtcNow.AddHours(-1)) - .OrderBy(m => m.DateTime) - .ToListAsync(); - - if (messages.Count < 10) { - messages = await _dbContext.Messages.AsNoTracking() - .Where(m => m.GroupId == ChatId) - .OrderByDescending(m => m.DateTime) - .Take(10) - .OrderBy(m => m.DateTime) - .ToListAsync(); - } - - if (_llmVisibilityService != null) { - messages = await _llmVisibilityService.FilterVisibleMessagesAsync(ChatId, messages); - } - - if (inputToken != null && - ( _llmVisibilityService == null || - !await _llmVisibilityService.IsUserInvisibleAsync(ChatId, inputToken.FromUserId) )) { - messages.Add(inputToken); - } - - _logger.LogInformation("{ServiceName}: BuildResponseInputItemsAsync: Found {Count} messages for ChatId {ChatId}.", - ServiceName, messages.Count, ChatId); - - var inputItems = new List(); - var str = new StringBuilder(); - Message previous = null; - var userCache = new Dictionary(); - var pendingImages = new List(); - - foreach (var message in messages) { - if (previous == null - && !inputItems.Any() - && message.FromUserId.Equals(Env.BotId)) { - previous = message; - continue; - } - - if (previous != null && !IsSameSender(previous, message)) { - Transports.ResponsesTransport.AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - str.Clear(); - pendingImages.Clear(); - } - - str.Append($"[{message.DateTime.ToString("yyyy-MM-dd HH:mm:ss zzz")}]"); - if (message.FromUserId != 0) { - if (!userCache.TryGetValue(message.FromUserId, out var fromUser)) { - fromUser = await _dbContext.UserData.AsNoTracking().FirstOrDefaultAsync(u => u.Id == message.FromUserId); - if (fromUser != null) userCache[message.FromUserId] = fromUser; - } - str.Append(fromUser != null ? $"{fromUser.FirstName} {fromUser.LastName}".Trim() : $"User({message.FromUserId})"); - } else { - str.Append("System/Unknown"); - } - - if (message.ReplyToMessageId != 0) { - str.Append('('); - str.Append($"Reply to msg {message.ReplyToMessageId}"); - str.Append(')'); - } - str.Append(':').Append(message.Content).Append("\n"); - - // Add message extensions - var extensions = await _messageExtensionService.GetByMessageDataIdAsync(message.Id); - if (extensions != null && extensions.Any()) { - str.Append("[扩展信息:"); - foreach (var ext in extensions) { - str.Append($"{ext.Name}={ext.Value}; "); - } - str.Append("]\n"); - } - - // Load images if vision supported - if (supportsVision && message.FromUserId != Env.BotId) { - var imageBytes = TryLoadMessagePhoto(message.GroupId, message.MessageId); - if (imageBytes != null) { - pendingImages.Add(imageBytes); - } - } - - previous = message; - } - - if (previous != null && str.Length > 0) { - Transports.ResponsesTransport.AddResponseItemFromAccumulated(inputItems, previous.FromUserId, str.ToString(), supportsVision ? pendingImages : null); - } - - return inputItems; - } - - private IEnumerable ParseOpenAIModelsWithCapabilities(string jsonContent) { try { var modelsData = JsonConvert.DeserializeObject(jsonContent); @@ -996,26 +676,5 @@ private ModelWithCapabilities InferOpenAIModelCapabilities(string modelName) { return model; } - Task ILlmProvider.AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt) { - return AnalyzeImageAsync(photoPath, modelName, channel, prompt); - } - - Task> ILlmProvider.GetAllModels(LLMChannel channel) { - return GetAllModels(channel); - } - - Task> ILlmProvider.GetAllModelsWithCapabilities(LLMChannel channel) { - return GetAllModelsWithCapabilities(channel); - } - - Task ILlmProvider.GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { - return GenerateEmbeddingsAsync(text, modelName, channel); - } - - IAsyncEnumerable ILlmProvider.ResumeFromSnapshotAsync(LlmContinuationSnapshot snapshot, LLMChannel channel, - LlmExecutionContext executionContext, - CancellationToken cancellationToken) { - return ResumeFromSnapshotAsync(snapshot, channel, executionContext, cancellationToken); - } } } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs index da24accb..b08092fe 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs @@ -22,7 +22,7 @@ namespace TelegramSearchBot.Service.AI.LLM.Transports { public sealed class AnthropicMessagesTransport : ILlmTransport { /// /// Builds the transport from a channel/binding pair. Absorbs the client - /// construction glue formerly in AnthropicService.CreateClient. + /// construction glue formerly in AnthropicModelApi.CreateClient. /// public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, string systemPrompt, bool nativeTools, bool promptCachingEnabled, ILogger logger) { @@ -87,7 +87,7 @@ public async IAsyncEnumerable StreamTurnAsync( // Legacy wire semantics: prompt-cache preparation runs once per run (first turn); // later turns append raw deltas so user messages stay string-typed on the wire. if (_preparedHistory == null) { - _preparedHistory = AnthropicService.PrepareMessagesForPromptCaching(ToProviderHistory(request.History), _promptCachingEnabled, excludeDynamicTail: true, out var cacheBreakpointInserted); + _preparedHistory = AnthropicModelApi.PrepareMessagesForPromptCaching(ToProviderHistory(request.History), _promptCachingEnabled, excludeDynamicTail: true, out var cacheBreakpointInserted); _cacheBreakpointInserted = cacheBreakpointInserted; } else if (request.History.Count > _convertedCount) { _preparedHistory.AddRange(ToProviderHistory(request.History.Skip(_convertedCount))); @@ -98,14 +98,14 @@ public async IAsyncEnumerable StreamTurnAsync( var nativeToolSpecs = request.Tools; var rawHistory = providerHistory.ToList(); - var (toolDefinitionHash, stablePrefixHash) = AnthropicService.BuildPromptCachingContext( + var (toolDefinitionHash, stablePrefixHash) = AnthropicModelApi.BuildPromptCachingContext( _nativeTools ? "anthropic-native" : "anthropic-xml", _systemPrompt, - AnthropicService.SerializeProviderHistory(_systemPrompt, rawHistory)); + AnthropicModelApi.SerializeProviderHistory(_systemPrompt, rawHistory)); var parameters = new MessageCreateParams { Model = _modelName, MaxTokens = 8192, - System = AnthropicService.BuildSystemPrompt(_systemPrompt, _promptCachingEnabled), + System = AnthropicModelApi.BuildSystemPrompt(_systemPrompt, _promptCachingEnabled), Messages = providerHistory, Tools = _nativeTools && nativeToolSpecs is { Count: > 0 } ? ConvertToAnthropicToolSpecs(nativeToolSpecs, _promptCachingEnabled) @@ -256,7 +256,7 @@ void FlushToolResults() { FlushToolResults(); // Anthropic requires strictly alternating user/assistant starting with user. - return AnthropicService.EnsureAlternatingRoles(result); + return AnthropicModelApi.EnsureAlternatingRoles(result); } private static List ConvertToAnthropicToolSpecs(IReadOnlyList specs, bool enablePromptCaching) { @@ -292,7 +292,7 @@ private static List ConvertToAnthropicToolSpecs(IReadOnlyList CreateAsync(LLMChannel channel, LLM return new LlmTransportBundle(transport, config); } - /// Ensures the model exists locally, pulling it otherwise. Moved from OllamaService. + /// Ensures the model exists locally, pulling it otherwise. Moved from OllamaModelApi. public static async Task CheckAndPullModelAsync(OllamaApiClient ollama, string modelName, ILogger logger) { logger.LogInformation("Checking for Ollama model: {ModelName}", modelName); try { diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs index 1b317f81..de121881 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs @@ -84,7 +84,7 @@ public async IAsyncEnumerable StreamTurnAsync( var shouldObservePromptCaching = _channel.Provider == LLMProvider.ResponsesAPI; var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIResponsesService.BuildPromptCachingContext( + var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = ResponsesModelApi.BuildPromptCachingContext( "OpenAIResponses", request.Config.ModelName, "responses", @@ -119,7 +119,7 @@ public async IAsyncEnumerable StreamTurnAsync( var textBuilder = new StringBuilder(); var reasoningBuilder = new StringBuilder(); var uiBuilder = new StringBuilder(); - var toolCallAccums = new Dictionary(); + var toolCallAccums = new Dictionary(); ResponseResult completedResult = null; var lastUiLength = 0; var streamedAny = false; @@ -127,7 +127,7 @@ public async IAsyncEnumerable StreamTurnAsync( await foreach (var update in _client.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - OpenAIResponsesService.ProcessStreamingUpdate(update, textBuilder, uiBuilder, reasoningBuilder, toolCallAccums, ref completedResult); + ResponsesModelApi.ProcessStreamingUpdate(update, textBuilder, uiBuilder, reasoningBuilder, toolCallAccums, ref completedResult); if (uiBuilder.Length > lastUiLength) { var delta = uiBuilder.ToString(lastUiLength, uiBuilder.Length - lastUiLength); @@ -157,9 +157,9 @@ public async IAsyncEnumerable StreamTurnAsync( && !string.IsNullOrWhiteSpace(fcItem.CallId) && !string.IsNullOrWhiteSpace(fcItem.FunctionName)) { toolCalls.Add(new LlmToolCall { - Id = OpenAIService.NormalizeToolCallId(fcItem.CallId), - Name = OpenAIService.NormalizeToolCallName(fcItem.FunctionName), - ArgumentsJson = OpenAIService.NormalizeToolCallArguments(fcItem.FunctionArguments?.ToString() ?? "{}") + Id = OpenAiModelApi.NormalizeToolCallId(fcItem.CallId), + Name = OpenAiModelApi.NormalizeToolCallName(fcItem.FunctionName), + ArgumentsJson = OpenAiModelApi.NormalizeToolCallArguments(fcItem.FunctionArguments?.ToString() ?? "{}") }); } } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs index 4555aa71..dd3c9435 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs @@ -31,13 +31,13 @@ public class ToolCallAccumulator { public sealed class OpenAiChatTransport : ILlmTransport { /// /// Builds the transport plus its turn config from a channel/binding pair. - /// Absorbs the client-construction glue formerly in OpenAIService.BuildClientParts. + /// Absorbs the client-construction glue formerly in OpenAiModelApi.BuildClientParts. /// public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, long chatId, bool nativeTools, bool promptCachingEnabled, ILogger logger, IHttpClientFactory httpClientFactory) { - var endpoint = OpenAIService.NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)); + var endpoint = OpenAiModelApi.NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)); var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - var includeEmptyReasoningContent = binding == null && OpenAIService.ShouldIncludeEmptyReasoningContent(channel, modelName); + var includeEmptyReasoningContent = binding == null && OpenAiModelApi.ShouldIncludeEmptyReasoningContent(channel, modelName); // ponytail: HttpClient must outlive this method (transport persists per run); factory-managed, not disposed here. var httpClient = httpClientFactory.CreateClient(); @@ -106,7 +106,7 @@ public async IAsyncEnumerable StreamTurnAsync( var shouldObservePromptCaching = _channel.Provider == LLMProvider.OpenAI; var promptCachingEnabled = shouldObservePromptCaching && _promptCachingEnabled; - var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAIService.BuildPromptCachingContext( + var (toolDefinitionHash, stablePrefixHash, promptCacheKey) = OpenAiModelApi.BuildPromptCachingContext( "OpenAI", _modelName, _nativeTools ? "chat-native" : "chat-xml", @@ -136,7 +136,7 @@ public async IAsyncEnumerable StreamTurnAsync( } } - var reasoningUpdate = OpenAIService.GetStreamingReasoningContent(update); + var reasoningUpdate = OpenAiModelApi.GetStreamingReasoningContent(update); if (!string.IsNullOrEmpty(reasoningUpdate)) { reasoningContentBuilder.Append(reasoningUpdate); streamedAny = true; @@ -181,17 +181,17 @@ public async IAsyncEnumerable StreamTurnAsync( if (_nativeTools && finishReason == ChatFinishReason.ToolCalls && toolCallAccumulators.Any()) { foreach (var (index, acc) in toolCallAccumulators) { if (string.IsNullOrWhiteSpace(acc.Id)) { - _logger.LogWarning("OpenAIService: Tool call at index {Index} has no ID, generating fallback.", index); + _logger.LogWarning("OpenAiModelApi: Tool call at index {Index} has no ID, generating fallback.", index); } toolCalls.Add(new LlmToolCall { - Id = OpenAIService.NormalizeToolCallId(acc.Id), - Name = OpenAIService.NormalizeToolCallName(acc.Name), - ArgumentsJson = OpenAIService.NormalizeToolCallArguments(acc.Arguments.ToString()) + Id = OpenAiModelApi.NormalizeToolCallId(acc.Id), + Name = OpenAiModelApi.NormalizeToolCallName(acc.Name), + ArgumentsJson = OpenAiModelApi.NormalizeToolCallArguments(acc.Arguments.ToString()) }); } } else if (toolCallAccumulators.Any()) { _logger.LogWarning( - "OpenAIService: Native response contained tool call updates but finish reason was not ToolCalls. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}", + "OpenAiModelApi: Native response contained tool call updates but finish reason was not ToolCalls. FinishReason={FinishReason}, ToolCallCount={ToolCallCount}", finishReason, toolCallAccumulators.Count); } @@ -233,11 +233,11 @@ private List ToProviderHistory(LlmTurnRequest request) { if (!string.IsNullOrWhiteSpace(m.Text)) { assistant = new AssistantChatMessage(calls) { Content = { ChatMessageContentPart.CreateTextPart(m.Text) } }; } - OpenAIService.SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); + OpenAiModelApi.SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); messages.Add(assistant); } else { var assistant = new AssistantChatMessage(m.Text ?? string.Empty); - OpenAIService.SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); + OpenAiModelApi.SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); messages.Add(assistant); } break; diff --git a/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs b/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs index 16b21542..483239cd 100644 --- a/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs +++ b/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs @@ -101,11 +101,11 @@ private static ServiceProvider BuildServices(int port) { services.AddScoped(); services.AddSingleton(); services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); diff --git a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs index 8d6d2fb1..343f65c6 100644 --- a/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs +++ b/TelegramSearchBot.LLMAgent/Service/LlmServiceProxy.cs @@ -11,13 +11,11 @@ namespace TelegramSearchBot.LLMAgent.Service { public sealed class LlmServiceProxy : IAgentTaskExecutor { private readonly IServiceProvider _serviceProvider; - private readonly LlmProviderRegistry _registry; private readonly LlmChatRunner _chatRunner; private readonly ILogger _logger; public LlmServiceProxy(IServiceProvider serviceProvider, ILogger logger) { _serviceProvider = serviceProvider; - _registry = serviceProvider.GetRequiredService(); _chatRunner = serviceProvider.GetRequiredService(); _logger = logger; } @@ -79,24 +77,6 @@ public async IAsyncEnumerable CallAsync( }; } - private ILlmProvider ResolveService(LLMProvider provider) { - try { - return _registry.GetProvider(provider); - } catch (KeyNotFoundException) { - // 保持原有降级语义:未知 provider 回退到 OpenAI 兼容路径 - return _registry.GetProvider(LLMProvider.OpenAI); - } - } - - /// 按 binding 线协议解析 client,统一委托给 LlmProviderRegistry;未知协议回退 OpenAI。 - private ILlmProvider ResolveService(LlmProtocol protocol) { - try { - return _registry.GetProvider(protocol); - } catch (KeyNotFoundException) { - return _registry.GetProvider(LlmProtocol.OpenAIChat); - } - } - private void ApplyBotIdentity(string botName, long botUserId) { var identityProvider = _serviceProvider.GetService(); if (identityProvider != null) { diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs index 2ae718ea..705436b4 100644 --- a/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs +++ b/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs @@ -20,10 +20,10 @@ public class EditLLMConfHelperTest { private readonly DataDbContext _context; private readonly Mock _redisMock; private readonly Mock _dbMock; - private readonly Mock _openAIServiceMock; + private readonly Mock _openAIServiceMock; private readonly Mock _messageExtensionServiceMock; - private readonly Mock _ollamaServiceMock; - private readonly Mock _geminiServiceMock; + private readonly Mock _ollamaServiceMock; + private readonly Mock _geminiServiceMock; private readonly Mock _modelCapabilityServiceMock; private readonly Mock> _loggerMock; private readonly EditLLMConfHelper _helper; @@ -41,8 +41,8 @@ public EditLLMConfHelperTest() { .Returns(_dbMock.Object); var httpClientFactoryMock = new Mock(); - var openAiLogger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger(); - var ollamaLogger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger(); + var openAiLogger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger(); + var ollamaLogger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger(); var serviceProviderMock = new Mock(); _messageExtensionServiceMock = new Mock(_context); @@ -53,7 +53,7 @@ public EditLLMConfHelperTest() { .ReturnsAsync(true); // Setup mock LLM services - _ollamaServiceMock = new Mock( + _ollamaServiceMock = new Mock( _context, ollamaLogger, serviceProviderMock.Object, @@ -61,12 +61,12 @@ public EditLLMConfHelperTest() { _ollamaServiceMock.Setup(s => s.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "ollama-model1", "ollama-model2" }); - _geminiServiceMock = new Mock(); + _geminiServiceMock = new Mock(); _geminiServiceMock.Setup(s => s.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "gemini-model1", "gemini-model2" }); - var openAiLoggerMock = new Mock>(); - _openAIServiceMock = new Mock( + var openAiLoggerMock = new Mock>(); + _openAIServiceMock = new Mock( _context, openAiLoggerMock.Object, _messageExtensionServiceMock.Object, @@ -74,15 +74,15 @@ public EditLLMConfHelperTest() { _openAIServiceMock.Setup(s => s.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "openai-model1", "openai-model2" }); - var geminiLoggerMock = new Mock>(); - _geminiServiceMock = new Mock(_context, geminiLoggerMock.Object, httpClientFactoryMock.Object); + var geminiLoggerMock = new Mock>(); + _geminiServiceMock = new Mock(_context, geminiLoggerMock.Object, httpClientFactoryMock.Object); // 新增 ILLMFactory mock var llmFactoryMock = new Mock((IServiceProvider)null); - llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); - llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.MiniMax)).Returns(_openAIServiceMock.Object); - llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Ollama)).Returns(_ollamaServiceMock.Object); - llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Gemini)).Returns(_geminiServiceMock.Object); + llmFactoryMock.Setup(f => f.GetCatalog(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); + llmFactoryMock.Setup(f => f.GetCatalog(LLMProvider.MiniMax)).Returns(_openAIServiceMock.Object); + llmFactoryMock.Setup(f => f.GetCatalog(LLMProvider.Ollama)).Returns(_ollamaServiceMock.Object); + llmFactoryMock.Setup(f => f.GetCatalog(LLMProvider.Gemini)).Returns(_geminiServiceMock.Object); // 创建Logger mock _loggerMock = new Mock>(); @@ -252,8 +252,8 @@ public async Task RefreshAllChannel_ShouldUpdateAllModels() { // Arrange // Use mocks initialized in Initialize() - // Mock GeminiService - var geminiServiceMock = new Mock(MockBehavior.Strict); + // Mock GeminiModelApi + var geminiServiceMock = new Mock(MockBehavior.Strict); geminiServiceMock.Setup(s => s.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "gemini-model1", "gemini-model2" }); diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs index 7fe0c8d1..aa570496 100644 --- a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs +++ b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs @@ -23,7 +23,7 @@ public class EditLLMConfTest { private DataDbContext _context = null!; private Mock _redisMock = null!; private Mock _dbMock = null!; - private Mock _openAIServiceMock = null!; + private Mock _openAIServiceMock = null!; private EditLLMConfService _service = null!; private Mock helperMock = null!; @@ -56,16 +56,16 @@ public EditLLMConfTest() { It.IsAny())) .ReturnsAsync(true); - var loggerMock = new Mock>(); + var loggerMock = new Mock>(); var messageExtensionServiceMock = new Mock(_context); var httpClientFactoryMock = new Mock(); - _openAIServiceMock = new Mock(_context, loggerMock.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); + _openAIServiceMock = new Mock(_context, loggerMock.Object, messageExtensionServiceMock.Object, httpClientFactoryMock.Object); _openAIServiceMock.Setup(o => o.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "model1", "model2" }); - var ollamaLoggerMock = new Mock>(); + var ollamaLoggerMock = new Mock>(); var serviceProviderMock = new Mock(); - var ollamaServiceMock = new Mock( + var ollamaServiceMock = new Mock( _context, ollamaLoggerMock.Object, serviceProviderMock.Object, @@ -73,15 +73,15 @@ public EditLLMConfTest() { ollamaServiceMock.Setup(o => o.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "ollama-model1", "ollama-model2" }); - var geminiLoggerMock = new Mock>(); - var geminiServiceMock = new Mock(_context, geminiLoggerMock.Object, httpClientFactoryMock.Object); + var geminiLoggerMock = new Mock>(); + var geminiServiceMock = new Mock(_context, geminiLoggerMock.Object, httpClientFactoryMock.Object); geminiServiceMock.Setup(g => g.GetAllModels(It.IsAny())) .ReturnsAsync(new List { "gemini-model1", "gemini-model2" }); var llmFactoryMock = new Mock((IServiceProvider)null); - llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); - llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Ollama)).Returns(ollamaServiceMock.Object); - llmFactoryMock.Setup(f => f.GetProvider(LLMProvider.Gemini)).Returns(geminiServiceMock.Object); + llmFactoryMock.Setup(f => f.GetCatalog(LLMProvider.OpenAI)).Returns(_openAIServiceMock.Object); + llmFactoryMock.Setup(f => f.GetCatalog(LLMProvider.Ollama)).Returns(ollamaServiceMock.Object); + llmFactoryMock.Setup(f => f.GetCatalog(LLMProvider.Gemini)).Returns(geminiServiceMock.Object); helperMock = new Mock(); helperMock.Setup(h => h.AddChannel( diff --git a/TelegramSearchBot.Test/Service/Storage/MessageServiceTests.cs b/TelegramSearchBot.Test/Service/Storage/MessageServiceTests.cs index f184fa05..2eb906ea 100644 --- a/TelegramSearchBot.Test/Service/Storage/MessageServiceTests.cs +++ b/TelegramSearchBot.Test/Service/Storage/MessageServiceTests.cs @@ -67,23 +67,23 @@ public MessageServiceTests() { _mockMediator = new Mock(); // Create mocks for all LLM services - var mockOllamaLogger = new Mock>(); - var mockOllamaService = new Mock( + var mockOllamaLogger = new Mock>(); + var mockOllamaModelApi = new Mock( _context, mockOllamaLogger.Object, Mock.Of(), Mock.Of()); - var mockOpenAILogger = new Mock>(); + var mockOpenAILogger = new Mock>(); var messageExtensionService = new MessageExtensionService(_context); - var mockOpenAIService = new Mock( + var mockOpenAiModelApi = new Mock( _context, mockOpenAILogger.Object, messageExtensionService, Mock.Of()); - var mockGeminiLogger = new Mock>(); - var mockGeminiService = new Mock( + var mockGeminiLogger = new Mock>(); + var mockGeminiModelApi = new Mock( _context, mockGeminiLogger.Object, Mock.Of()); From 391b6995e86f47e38d93df8850b6684a6bd4ccb6 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Sat, 12 Sep 2026 12:14:07 +0800 Subject: [PATCH 30/30] docs(plan): mark Phase 4.5 complete (CI green) --- Docs/Agent_Architecture_Refactor_Plan.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Docs/Agent_Architecture_Refactor_Plan.md b/Docs/Agent_Architecture_Refactor_Plan.md index 65b624a1..9edf8628 100644 --- a/Docs/Agent_Architecture_Refactor_Plan.md +++ b/Docs/Agent_Architecture_Refactor_Plan.md @@ -383,7 +383,7 @@ erDiagram P1 共享循环(已完成) P2 预设(已完成) P3+4 整体重写:LlmMessage → 投影 → 循环 v2 → 5 transports → registry → 删旧(已完成,CI 绿) - P4.5 provider 类彻底删除:LlmChatRunner → Transport.Create → ProviderApi → 删 5 个 service ← 下一步 + P4.5 provider 类彻底删除:LlmChatRunner → Transport.Create → ProviderApi → 删 5 个 service(已完成,CI 绿) [PR #391] P5 单入口 + Redis 状态收敛