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/ diff --git a/Docs/Agent_Architecture_Refactor_Plan.md b/Docs/Agent_Architecture_Refactor_Plan.md new file mode 100644 index 00000000..9edf8628 --- /dev/null +++ b/Docs/Agent_Architecture_Refactor_Plan.md @@ -0,0 +1,393 @@ +# 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 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) + +**范围** +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 行),代价是失去进程隔离。 +- 或保留现状(推荐默认,隔离价值真实)。 + +--- + +## 七、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`)写入的就是这三张表,预设只是元数据,不自动建行 + +--- + +## 五、兼容性与风险 + +| 风险 | 缓解 | +|---|---| +| 投影行为漂移(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 → 删旧(已完成,CI 绿) + P4.5 provider 类彻底删除:LlmChatRunner → Transport.Create → ProviderApi → 删 5 个 service(已完成,CI 绿) + +[PR #391] + P5 单入口 + Redis 状态收敛 + +[PR #392?] + P6 worker 合并评估(默认不动) +``` 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. 机器人回复: `请输入渠道的名称` 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 new file mode 100644 index 00000000..e789fa4f --- /dev/null +++ b/TelegramSearchBot.Common/Model/AI/Llm/LlmTurnModels.cs @@ -0,0 +1,10 @@ +using TelegramSearchBot.Model.AI; + +namespace TelegramSearchBot.Model.AI { + /// 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; + public sealed record TurnCompleted(LlmTurnResult Turn) : 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/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 608d9d21..b085fad1 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/GeneralLLMServiceTests.cs @@ -22,10 +22,10 @@ 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 _factoryMock; + private readonly Mock _openAIServiceMock; + private readonly Mock _ollamaServiceMock; + private readonly Mock _geminiServiceMock; + private readonly Mock _factoryMock; private readonly GeneralLLMService _service; public GeneralLLMServiceTests() { @@ -51,30 +51,31 @@ 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(); + _factoryMock = new Mock((IServiceProvider)null); _service = new GeneralLLMService( _redisMock.Object, _dbContext, _loggerMock.Object, - _factoryMock.Object); + _factoryMock.Object, + new LlmChatRunner(new Mock().Object, new Mock>().Object, _factoryMock.Object)); } [Fact] @@ -213,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", @@ -223,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"); @@ -232,7 +233,7 @@ public async Task AnalyzeImageAsync_WithCustomPrompt_ForwardsPromptToProvider() "image.jpg", 123, "vision-model", - providerMock.Object, + visionMock.Object, channel, GeneralLLMService.DefaultVisionOcrPrompt, CancellationToken.None)) { @@ -445,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.GetLLMService(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.GetLLMService(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] @@ -484,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.GetLLMService(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.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.GetCatalog(It.IsAny()), Times.Once); } [Fact] @@ -521,12 +517,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.GetLLMService(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 +533,45 @@ 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.GetLLMService(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 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/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.Test/Service/AI/LLM/LlmToolLoopTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs new file mode 100644 index 00000000..c9794b43 --- /dev/null +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmToolLoopTests.cs @@ -0,0 +1,247 @@ +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): 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 }; + + private static LlmToolLoopMeta Meta() => new LlmToolLoopMeta { + ChatId = 1, + OriginalMessageId = 3, + UserId = 2, + ModelName = "test-model", + Provider = "Test", + ChannelId = 9 + }; + + /// 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 Requests { get; } = new(); + + public void EnqueueTurn(Func> turnFactory) => _turns.Enqueue(turnFactory); + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + Requests.Add(request); + foreach (var evt in _turns.Dequeue()(request)) { + yield return evt; + } + await Task.CompletedTask; + } + } + + 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_AppendsAssistantAndFeedback() { + 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 } + } + }) + }, + async (server, tool, args) => { await Task.CompletedTask; return "mock:v"; }); + try { + const string xml = "v"; + 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(); + 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, transport.Requests.Count); + Assert.Contains("final answer text", yielded[^1]); + // 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)>(), + async (server, tool, args) => { await Task.CompletedTask; return string.Empty; }); + } + } + + [Fact] + public async Task NativeProtocol_LoopOwnsHistory_AppendsToolResults() { + 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 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(); + 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, 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)>(), + async (server, tool, args) => { await Task.CompletedTask; return string.Empty; }); + } + } + + [Fact] + public async Task IterationLimit_BuildsV2SnapshotWithNormalizedHistory() { + 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 transport = new ScriptedTransport { SupportsNativeTools = true }; + for (var i = 0; i < 3; i++) { + var callId = $"call-{i}"; + transport.EnqueueTurn(Turn("working", new List { + new() { Id = callId, Name = "mcp_test-server_endlessTool", ArgumentsJson = "{}" } + })); + } + + var executionContext = new LlmExecutionContext(); + 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); + var snapshot = executionContext.SnapshotData; + Assert.NotNull(snapshot); + Assert.Equal(2, snapshot.CyclesSoFar); + Assert.Equal("Test", snapshot.Provider); + Assert.Equal(9, snapshot.ChannelId); + 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( + new List<(string, McpToolHelper.ExternalToolInfo)>(), + async (server, tool, args) => { await Task.CompletedTask; return string.Empty; }); + } + } + + [Fact] + public async Task ResumeMode_YieldsOnlyNewContentButSnapshotsFullContent() { + 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(); + 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); + } + + Assert.All(yielded, y => Assert.DoesNotContain("old content", y)); + Assert.Contains("continued text", yielded[0]); + } + } +} 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 7397fca3..e1a264db 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/ModelCapabilityServiceTests.cs @@ -20,7 +20,8 @@ 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; public ModelCapabilityServiceTests() { @@ -32,18 +33,20 @@ 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.GetCatalog(It.IsAny())) .Returns(_openAIServiceMock.Object); _service = new ModelCapabilityService( _loggerMock.Object, _dbContext, - _serviceProviderMock.Object); + _registryMock.Object); } [Fact] 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.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 e26cf990..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, ILLMService 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, 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, 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, ILLMService 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/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/ILLMService.cs deleted file mode 100644 index 55732c73..00000000 --- a/TelegramSearchBot.LLM/Interface/AI/LLM/ILLMService.cs +++ /dev/null @@ -1,77 +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 ILLMService { - 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); - } - - 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 new file mode 100644 index 00000000..e1272129 --- /dev/null +++ b/TelegramSearchBot.LLM/Interface/AI/LLM/ILlmProviderCapabilities.cs @@ -0,0 +1,33 @@ +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 { + /// 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); + } + + /// 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/AnthropicService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs deleted file mode 100644 index e8d20f78..00000000 --- a/TelegramSearchBot.LLM/Service/AI/LLM/AnthropicService.cs +++ /dev/null @@ -1,1432 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -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.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using SkiaSharp; -using TelegramSearchBot.Attributes; -using TelegramSearchBot.Common; -using TelegramSearchBot.Interface; -using TelegramSearchBot.Interface.AI.LLM; -using TelegramSearchBot.Model; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; -// Alias to resolve ambiguity between TelegramSearchBot.Model.Data.Message and Anthropic.Models.Messages.Message -using DataMessage = TelegramSearchBot.Model.Data.Message; - -namespace TelegramSearchBot.Service.AI.LLM { - [Injectable(ServiceLifetime.Transient)] - public class AnthropicService : IService, ILLMService { - public string ServiceName => "AnthropicService"; - - 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 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", - "claude-opus-4-20250514", - "claude-3-5-sonnet-20241022", - "claude-3-5-haiku-20241022", - "claude-3-opus-20240229", - "claude-3-sonnet-20240229", - "claude-3-haiku-20240307" - }; - - public AnthropicService( - DataDbContext context, - 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) { - _logger = logger; - _dbContext = context; - _messageExtensionService = messageExtensionService; - _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _llmVisibilityService = llmVisibilityService; - _promptCachingSettingsService = promptCachingSettingsService; - _logger.LogInformation("AnthropicService instance created. McpToolHelper should be initialized at application startup."); - } - - 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 { - ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), - }; - 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; - } - return new AnthropicClient(options); - } - - private async Task IsPromptCachingEnabledAsync() { - return _promptCachingSettingsService == null || await _promptCachingSettingsService.IsEnabledAsync(); - } - - private static CacheControlEphemeral CreateCacheControl() { - return new CacheControlEphemeral(); - } - - private static MessageCreateParamsSystem BuildSystemPrompt(string systemPrompt, bool enablePromptCaching) { - if (!enablePromptCaching) { - return systemPrompt; - } - - return new MessageCreateParamsSystem([ - new TextBlockParam(systemPrompt) { - CacheControl = CreateCacheControl(), - } - ], null); - } - - private static MessageParam CloneMessageForCachePrefix(MessageParam message, bool markCacheControl) { - if (message.Content.TryPickString(out var text)) { - return new MessageParam { - Role = message.Role, - Content = new List { - new(new TextBlockParam(text) { - CacheControl = markCacheControl ? CreateCacheControl() : null, - }, null) - } - }; - } - - if (!message.Content.TryPickContentBlockParams(out var blocks)) { - return new MessageParam { - Role = message.Role, - Content = message.Content, - }; - } - - var clonedBlocks = new List(); - for (int i = 0; i < blocks.Count; i++) { - var isLastBlock = markCacheControl && i == blocks.Count - 1; - var block = blocks[i]; - if (block.TryPickText(out var textBlock)) { - clonedBlocks.Add(new ContentBlockParam(new TextBlockParam(textBlock.Text) { - CacheControl = isLastBlock ? CreateCacheControl() : textBlock.CacheControl, - Citations = textBlock.Citations, - }, null)); - continue; - } - - if (block.TryPickImage(out var imageBlock)) { - clonedBlocks.Add(new ContentBlockParam(new ImageBlockParam { - Source = imageBlock.Source, - CacheControl = isLastBlock ? CreateCacheControl() : imageBlock.CacheControl, - }, null)); - continue; - } - - clonedBlocks.Add(block); - } - - return new MessageParam { - Role = message.Role, - Content = clonedBlocks, - }; - } - - private static List PrepareMessagesForPromptCaching(List providerHistory, bool enablePromptCaching, bool excludeDynamicTail, out bool cacheBreakpointInserted) { - cacheBreakpointInserted = enablePromptCaching; - if (!enablePromptCaching || providerHistory.Count == 0) { - return providerHistory; - } - - var stableCount = excludeDynamicTail - ? Math.Max(providerHistory.Count - 1, 0) - : providerHistory.Count; - if (stableCount == 0) { - return providerHistory; - } - - var clonedMessages = providerHistory - .Select((message, index) => CloneMessageForCachePrefix(message, index == stableCount - 1)) - .ToList(); - cacheBreakpointInserted = true; - return clonedMessages; - } - - private static (string toolDefinitionHash, string stablePrefixHash) BuildPromptCachingContext(string mode, string systemPrompt, List stableHistory) { - var toolDefinitionHash = PromptCachingHelper.ComputeToolDefinitionHash(); - var stablePrefixHash = PromptCachingHelper.ComputeStablePrefixHash(new { - Mode = mode, - SystemPrompt = systemPrompt, - StableHistory = stableHistory, - }); - return (toolDefinitionHash, stablePrefixHash); - } - - private void LogPromptCachingObservation( - 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 = PromptCachingHelper.DetermineAnthropicOutcome( - promptCachingEnabled, - cacheBreakpointInserted, - observationKey, - cacheCreationInputTokens, - cacheReadInputTokens, - 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, - CacheBreakpointInserted = cacheBreakpointInserted, - CacheCreationInputTokens = cacheCreationInputTokens, - CacheReadInputTokens = cacheReadInputTokens, - ProviderUsageJson = usageJson, - }); - } - - #region Models - - public virtual Task> GetAllModels(LLMChannel channel) { - return Task.FromResult>(_anthropicModels); - } - - public virtual Task> GetAllModelsWithCapabilities(LLMChannel channel) { - var results = new List(); - foreach (var modelName in _anthropicModels) { - results.Add(InferAnthropicModelCapabilities(modelName)); - } - return Task.FromResult>(results); - } - - private ModelWithCapabilities InferAnthropicModelCapabilities(string modelName) { - var model = new ModelWithCapabilities { ModelName = modelName }; - var lowerName = modelName.ToLower(); - - model.SetCapability("streaming", true); - model.SetCapability("function_calling", true); - model.SetCapability("tool_calls", true); - model.SetCapability("chat", true); - model.SetCapability("model_family", "Claude"); - - // All Claude 3+ models support vision - model.SetCapability("vision", true); - model.SetCapability("image_content", true); - - // Claude 3.5+ and Claude 4+ are multimodal - if (lowerName.Contains("claude-sonnet-4") || lowerName.Contains("claude-opus-4") || - lowerName.Contains("claude-3-5") || lowerName.Contains("claude-3.5")) { - model.SetCapability("multimodal", true); - model.SetCapability("advanced_reasoning", true); - } - - if (lowerName.Contains("opus")) { - model.SetCapability("advanced_reasoning", true); - model.SetCapability("complex_tasks", true); - } - - if (lowerName.Contains("haiku")) { - model.SetCapability("fast_response", true); - model.SetCapability("optimized", true); - } - - return model; - } - - #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); - } - - _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); - } - } - - 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() ?? "" - }); - } - } - - /// - /// Ensures message list starts with user and alternates between user/assistant. - /// Merges consecutive same-role messages. - /// - private static List EnsureAlternatingRoles(List messages) { - if (!messages.Any()) return messages; - - var result = new List(); - - foreach (var msg in messages) { - if (result.Count > 0 && result.Last().Role == msg.Role) { - // Merge with previous message of same role - var prev = result.Last(); - var prevContent = ExtractTextFromContent(prev.Content); - var curContent = ExtractTextFromContent(msg.Content); - result[result.Count - 1] = new MessageParam { - Role = prev.Role, - Content = prevContent + "\n" + curContent - }; - } else { - result.Add(msg); - } - } - - // Ensure starts with user - if (result.Any() && result.First().Role == Role.Assistant) { - result.Insert(0, new MessageParam { - Role = Role.User, - Content = "(conversation start)" - }); - } - - 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 - - /// - /// 检查模型是否支持视觉能力 - /// - 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格式的字节数组 - /// - 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; - } - } - - #endregion - - #region Tool Conversion - - /// - /// Convert OpenAI ChatTool definitions to Anthropic Tool objects. - /// - private static List ConvertToAnthropicTools(List openAiTools, bool enablePromptCaching) { - var tools = new List(); - for (int index = 0; index < openAiTools.Count; index++) { - var chatTool = openAiTools[index]; - try { - var toolName = chatTool.FunctionName; - var toolDescription = chatTool.FunctionDescription; - var paramsJson = chatTool.FunctionParameters?.ToString() ?? "{}"; - var schemaDoc = System.Text.Json.JsonDocument.Parse(paramsJson); - 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 = toolName, - Description = toolDescription, - InputSchema = inputSchema, - CacheControl = enablePromptCaching && index == openAiTools.Count - 1 ? CreateCacheControl() : null, - }, null)); - } catch (Exception) { - } - } - - return tools; - } - - #endregion - - #region ExecAsync - - public async IAsyncEnumerable 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; - } - - // Try native tool calling first; fall back to XML prompt-based if it fails - bool useNativeToolCalling = true; - var nativeTools = McpToolHelper.GetNativeToolDefinitions(); - - if (nativeTools == null || !nativeTools.Any()) { - useNativeToolCalling = false; - } - - if (useNativeToolCalling) { - bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(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(message, ChatId, modelName, channel, binding, executionContext, 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. - /// - private async IAsyncEnumerable ExecWithNativeToolCallingAsync( - 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); - 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); - - 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, - }; - } - } - - /// - /// Execute LLM with XML prompt-based tool calling (fallback). - /// - private async IAsyncEnumerable ExecWithXmlToolCallingAsync( - 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); - 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); - - 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, - }; - } - } - - #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; - } - 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, snapshot.ProviderHistory?.Count ?? 0); - - var (systemPrompt, providerHistory) = DeserializeProviderHistory(snapshot.ProviderHistory); - var promptCachingEnabled = await IsPromptCachingEnabledAsync(); - var (toolDefinitionHash, stablePrefixHash) = BuildPromptCachingContext( - "anthropic-resume", - systemPrompt ?? string.Empty, - snapshot.ProviderHistory ?? []); - providerHistory = PrepareMessagesForPromptCaching(providerHistory, promptCachingEnabled, excludeDynamicTail: false, out var cacheBreakpointInserted); - - using var client = CreateClient(channel, binding); - - var fullContentBuilder = new StringBuilder(snapshot.LastAccumulatedContent ?? ""); - var newContentBuilder = new StringBuilder(); - - int maxToolCycles = Env.MaxToolCycles; - var trackedHistory = snapshot.ProviderHistory ?? new List(); - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - var parameters = new MessageCreateParams { - Model = modelName, - MaxTokens = 8192, - System = BuildSystemPrompt(systemPrompt ?? string.Empty, 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)) { - fullContentBuilder.Append(textDelta.Text); - newContentBuilder.Append(textDelta.Text); - llmResponseBuilder.Append(textDelta.Text); - var newContent = newContentBuilder.ToString(); - if (newContent.Length > 10) { - yield return newContent; - } - } - } - } - - 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); - - var toolIndicator = McpToolHelper.FormatToolCallDisplay(parsedToolName, firstToolCall.arguments); - newContentBuilder.Append(toolIndicator); - fullContentBuilder.Append(toolIndicator); - yield return newContentBuilder.ToString(); - - string toolResultString; - bool isError = false; - 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); - } catch (Exception ex) { - isError = true; - _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()}."; - } - - 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 - }); - trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = 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 = "Anthropic", - ChannelId = channel.Id, - LastAccumulatedContent = fullContentBuilder.ToString(), - CyclesSoFar = snapshot.CyclesSoFar + maxToolCycles, - ProviderHistory = trackedHistory, - }; - } - } - - #endregion - - #region Serialization - - /// - /// Serialize Anthropic message list to portable format for snapshot persistence. - /// - public static List SerializeProviderHistory(string systemPrompt, List history) { - var result = new List(); - - if (!string.IsNullOrWhiteSpace(systemPrompt)) { - result.Add(new SerializedChatMessage { Role = "system", Content = systemPrompt }); - } - - foreach (var msg in history) { - string role = msg.Role == Role.Assistant ? "assistant" : "user"; - string content = ExtractTextFromContent(msg.Content); - result.Add(new SerializedChatMessage { Role = role, Content = content }); - } - - return result; - } - - /// - /// Deserialize portable format back to Anthropic message list. - /// Returns (systemPrompt, messages). - /// - public static (string systemPrompt, List messages) DeserializeProviderHistory(List serialized) { - string systemPrompt = null; - var messages = new List(); - if (serialized == null) return (systemPrompt, messages); - - foreach (var msg in serialized) { - switch (msg.Role?.ToLowerInvariant()) { - case "system": - systemPrompt = msg.Content ?? ""; - break; - case "assistant": - messages.Add(new MessageParam { - Role = Role.Assistant, - Content = msg.Content ?? "" - }); - break; - case "user": - default: - messages.Add(new MessageParam { - Role = Role.User, - Content = msg.Content ?? "" - }); - break; - } - } - - messages = EnsureAlternatingRoles(messages); - return (systemPrompt, messages); - } - - #endregion - - #region Embeddings - - public Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { - throw new NotSupportedException($"{ServiceName}: Anthropic does not natively support embeddings. Use a different provider for embedding generation."); - } - - #endregion - - #region 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 AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) { - if (string.IsNullOrWhiteSpace(modelName)) { - modelName = "claude-sonnet-4-20250514"; - } - - prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; - - 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); - return $"Error: {ServiceName} channel/apikey is not configured."; - } - - using var client = CreateClient(channel, binding); - - try { - using var fileStream = File.OpenRead(photoPath); - var bitmap = SKBitmap.Decode(fileStream); - var imgData = bitmap.Encode(SKEncodedImageFormat.Png, 99); - var imgArray = imgData.ToArray(); - var base64Image = Convert.ToBase64String(imgArray); - - var imageSource = new Base64ImageSource { - Data = base64Image, - MediaType = MediaType.ImagePng, - }; - - var contentBlocks = new List { - new ImageBlockParam(imageSource), - new TextBlockParam(prompt), - }; - - var messages = new List { - new MessageParam { - Role = Role.User, - Content = contentBlocks - } - }; - - var parameters = new MessageCreateParams { - Model = modelName, - MaxTokens = 4096, - Messages = messages, - }; - - var responseBuilder = new StringBuilder(); - await foreach (var rawEvent in client.Messages.CreateStreaming(parameters)) { - if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { - if (deltaEvent.Delta.TryPickText(out var textDelta)) { - responseBuilder.Append(textDelta.Text); - } - } - } - - return responseBuilder.ToString(); - } catch (Exception ex) { - _logger.LogError(ex, "Error analyzing image with Anthropic"); - return $"Error analyzing image: {ex.Message}"; - } - } - - #endregion - } -} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs deleted file mode 100644 index 81658bf7..00000000 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeminiService.cs +++ /dev/null @@ -1,551 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using GenerativeAI; -using GenerativeAI.Types; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using SkiaSharp; -using TelegramSearchBot.Attributes; -using TelegramSearchBot.Common; -using TelegramSearchBot.Interface; -using TelegramSearchBot.Interface.AI.LLM; -using TelegramSearchBot.Model; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; - -namespace TelegramSearchBot.Service.AI.LLM { - [Injectable(ServiceLifetime.Transient)] - public class GeminiService : ILLMService, IService { - public string ServiceName => "GeminiService"; - 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 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( - DataDbContext context, - ILogger logger, - IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider, - LlmVisibilityService llmVisibilityService = null) { - _logger = logger; - _dbContext = context; - _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _llmVisibilityService = llmVisibilityService; - _logger.LogInformation("GeminiService 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); - } - - 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"); - } - - var role = fromUserId == Env.BotId ? Roles.Model : Roles.User; - - if (images != null && images.Count > 0 && role == Roles.User) { - var parts = new List(); - if (!string.IsNullOrWhiteSpace(content)) { - parts.Add(new Part { Text = content.Trim() }); - } - foreach (var imageBytes in images) { - parts.Add(new Part { - InlineData = new GenerativeAI.Types.Blob { - MimeType = "image/png", - Data = Convert.ToBase64String(imageBytes) - } - }); - } - chatHistory.Add(new Content { Parts = parts, Role = role }); - } else { - chatHistory.Add(new Content(content?.Trim() ?? "", role)); - } - } - - 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 - .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格式的字节数组 - /// - 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; - } - } - - public virtual async Task> GetAllModels(LLMChannel channel) { - if (channel.Provider.Equals(LLMProvider.Ollama)) { - return new List(); - } - - try { - var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); - var modelsResponse = await googleAI.ListModelsAsync(); - return modelsResponse.Models.Select(m => m.Name.Replace("models/", "")); - } catch (Exception ex) { - _logger.LogError(ex, "Failed to list Gemini models"); - return new List(); - } - } - - /// - /// 获取Gemini模型及其能力信息 - /// - public virtual async Task> GetAllModelsWithCapabilities(LLMChannel channel) { - if (channel.Provider.Equals(LLMProvider.Ollama)) { - return new List(); - } - - try { - var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); - var modelsResponse = await googleAI.ListModelsAsync(); - var results = new List(); - - foreach (var model in modelsResponse.Models) { - var modelName = model.Name.Replace("models/", ""); - var modelWithCaps = InferGeminiModelCapabilities(modelName, model); - results.Add(modelWithCaps); - } - - _logger.LogInformation("Retrieved {Count} Gemini models with capabilities", results.Count); - return results; - } catch (Exception ex) { - _logger.LogError(ex, "Failed to list Gemini models with capabilities"); - return new List(); - } - } - - /// - /// 根据Gemini模型名称和信息推断能力 - /// - private ModelWithCapabilities InferGeminiModelCapabilities(string modelName, GenerativeAI.Types.Model modelInfo) { - var model = new ModelWithCapabilities { ModelName = modelName }; - var lowerName = modelName.ToLower(); - - // 基本能力设置 - model.SetCapability("streaming", true); // Gemini API支持流式响应 - - // 从Gemini API模型信息中获取支持的方法 - if (modelInfo.SupportedGenerationMethods != null) { - foreach (var method in modelInfo.SupportedGenerationMethods) { - if (method.ToLower().Contains("generatecontent")) { - model.SetCapability("chat", true); - } else if (method.ToLower().Contains("embed")) { - model.SetCapability("embedding", true); - } - } - } - - // 基于模型名称推断能力 - if (lowerName.Contains("gemini")) { - // Gemini模型系列能力 - model.SetCapability("function_calling", true); - model.SetCapability("tool_calls", true); - model.SetCapability("response_json_object", true); - - // Gemini 2.0和Pro模型支持更多功能 - if (lowerName.Contains("2.0") || lowerName.Contains("pro")) { - model.SetCapability("vision", true); - model.SetCapability("multimodal", true); - model.SetCapability("image_content", true); - model.SetCapability("audio_content", true); - model.SetCapability("video_content", true); - model.SetCapability("file_upload", true); - } - // Gemini 1.5系列 - else if (lowerName.Contains("1.5")) { - model.SetCapability("vision", true); - model.SetCapability("multimodal", true); - model.SetCapability("image_content", true); - - if (lowerName.Contains("pro")) { - model.SetCapability("long_context", true); - model.SetCapability("file_upload", true); - model.SetCapability("audio_content", true); - } - } - // Flash模型 - 更快的响应 - if (lowerName.Contains("flash")) { - model.SetCapability("fast_response", true); - model.SetCapability("optimized", true); - } - - // Pro模型 - 更强的能力 - if (lowerName.Contains("pro")) { - model.SetCapability("advanced_reasoning", true); - model.SetCapability("complex_tasks", true); - } - } - - // 嵌入模型检测 - if (lowerName.Contains("embedding") || lowerName.Contains("embed")) { - model.SetCapability("embedding", true); - model.SetCapability("text_embedding", true); - model.SetCapability("function_calling", false); - model.SetCapability("vision", false); - model.SetCapability("chat", false); - } - - // 文本生成模型 - if (lowerName.Contains("text") && !lowerName.Contains("embedding")) { - model.SetCapability("text_generation", true); - model.SetCapability("chat", true); - } - - // 从模型信息中提取输入/输出token限制 - if (modelInfo.InputTokenLimit > 0) { - model.SetCapability("input_token_limit", modelInfo.InputTokenLimit.ToString()); - } - - if (modelInfo.OutputTokenLimit > 0) { - model.SetCapability("output_token_limit", modelInfo.OutputTokenLimit.ToString()); - } - - // 设置模型版本信息 - model.SetCapability("model_version", modelInfo.Version ?? "unknown"); - model.SetCapability("model_family", "Gemini"); - - // 基于模型名称的特殊能力 - if (lowerName.Contains("code")) { - model.SetCapability("code_generation", true); - model.SetCapability("code_completion", true); - } - - 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) { - 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); - } - - if (!_chatSessions.TryGetValue(ChatId, out var chatSession)) { - chatSession = model.StartChat(history: history); - _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 }); - - await foreach (var chunk in chatSession.StreamContentAsync(message.Content)) { - currentMessageBuilder.Append(chunk.Text); - fullResponseBuilder.Append(chunk.Text); - yield return currentMessageBuilder.ToString(); - } - - 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}]"; - - message.Content = feedback; - continue; - } - - 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 = "Gemini", - ChannelId = channel.Id, - LastAccumulatedContent = currentMessageBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = trackedHistory, - }; - } - } - - 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); - throw new ArgumentException("Channel or ApiKey is not configured"); - } - - try { - var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); - var embeddings = googleAI.CreateEmbeddingModel("models/embedding-001"); - var response = await embeddings.EmbedContentAsync(text); -#pragma warning disable CS8602 // 解引用可能出现空引用。 - return response.Embedding.Values.Select(v => ( float ) v).ToArray(); -#pragma warning restore CS8602 // 解引用可能出现空引用。 - } catch (Exception ex) { - _logger.LogError(ex, "Failed to generate embeddings"); - throw; - } - } - - public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null) { - if (string.IsNullOrWhiteSpace(modelName)) { - modelName = "gpt-4-vision-preview"; - } - - prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; - - if (channel == null || string.IsNullOrWhiteSpace(channel.Gateway) || string.IsNullOrWhiteSpace(channel.ApiKey)) { - _logger.LogError("{ServiceName}: Channel, Gateway or ApiKey is not configured.", ServiceName); - return $"Error: {ServiceName} channel/gateway/apikey is not configured."; - } - - var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); - var model = googleAI.CreateGenerativeModel("models/" + modelName); - try { - var chat = model.StartChat(); - - - var request = new GenerateContentRequest(); - request.AddText(prompt); - // Attach a local file - request.AddInlineFile(photoPath); - // Generate the content with attached files - var response = await chat.GenerateContentAsync(request); - return response.Text; - } catch (Exception ex) { - _logger.LogError(ex, "Error analyzing image with OpenAI"); - return $"Error analyzing image: {ex.Message}"; - } - } - } -} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs index 4a39f57d..053d7e20 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/GeneralLLMService.cs @@ -19,7 +19,9 @@ 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; + private readonly LlmChatRunner _chatRunner; + private readonly LlmVisibilityService _llmVisibilityService; public string ServiceName => "GeneralLLMService"; @@ -36,12 +38,16 @@ public GeneralLLMService( IConnectionMultiplexer connectionMultiplexer, DataDbContext dbContext, ILogger logger, - ILLMFactory _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(排除已软删除的模型) @@ -86,20 +92,37 @@ public async IAsyncEnumerable ExecAsync(Model.Data.Message message, long yield break; } - await foreach (var e in ExecOperationAsync((service, channel, binding, cancel) => { - return service.ExecAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken); + 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); + 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, string modelName, - ILLMService 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; } } @@ -149,26 +172,23 @@ 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); - 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.GetLLMService(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; } } - 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 @@ -208,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.GetLLMService(route); + var service = (TCapability)_LLMFactory.GetCatalog(channel.Provider); if (count < channel.Parallel) { // 获取锁并增加计数 @@ -217,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; @@ -264,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(); @@ -275,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, ILLMService 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, ILLMService 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, ILLMService 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; @@ -312,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(); @@ -323,11 +344,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, 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, ILLMService 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/LLMFactory.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LLMFactory.cs deleted file mode 100644 index 397155d4..00000000 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LLMFactory.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Collections.Generic; -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"; - - private readonly IServiceProvider _serviceProvider; - private readonly ILogger _logger; - - public LLMFactory( - IServiceProvider serviceProvider, - ILogger logger - ) { - _logger = logger; - _serviceProvider = serviceProvider; - } - - public ILLMService GetLLMService(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 service registered for provider {provider}.") - }; - } - - public ILLMService GetLLMService(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}.") - }; - } - - public ILLMService GetLLMService(ResolvedLlmRoute route) { - return route.Binding != null - ? GetLLMService(route.Binding.Protocol) - : GetLLMService(route.Channel.Provider); - } - - } -} 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; + } + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs new file mode 100644 index 00000000..d5d6a13b --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmChatRunner.cs @@ -0,0 +1,270 @@ +using System; +using System.Collections.Generic; +using System.Linq; +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); + } + + /// + /// 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 && + OpenAiModelApi.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/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 new file mode 100644 index 00000000..be397354 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmHistoryQueryService.cs @@ -0,0 +1,164 @@ +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. + /// + public static class LlmHistoryQueryService { + /// Rows ordered chronologically; the input message is appended last (when visible), + /// matching the legacy per-provider GetChatHistory behavior. + 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) + .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(_dbContext, messages, cancellationToken); + var extensionsByMessageId = await ResolveExtensionsAsync(_dbContext, 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 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() + .Where(x => userIds.Contains(x.Id)) + .ToDictionaryAsync(x => x.Id, 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() + .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 new file mode 100644 index 00000000..cbb68e6f --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs @@ -0,0 +1,106 @@ +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, + /// Null = gateway must be entered during creation (e.g. user-owned OpenCode Go gateway). + 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), + 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) => + 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.LLM/Service/AI/LLM/LlmProviderRegistry.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs new file mode 100644 index 00000000..f7614b10 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using System.Net.Http; +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM { + /// + /// 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 { + private readonly IServiceProvider _serviceProvider; + + public LlmProviderRegistry(IServiceProvider serviceProvider) { + _serviceProvider = serviceProvider; + } + + /// 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(), + _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") + }; + } + + /// 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}.") + }; + } + + /// Vision image-analysis capability for a 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, + ILogger logger, 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/LlmToolLoop.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs new file mode 100644 index 00000000..3122b3ad --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmToolLoop.cs @@ -0,0 +1,170 @@ +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 + resume 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; + } + + /// 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. 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( + LlmAgentRunRequest run, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + var maxToolCycles = Env.MaxToolCycles; + var contentBuilder = new StringBuilder(run.Meta.InitialContent ?? string.Empty); + var baseline = contentBuilder.Length; + + string NewContent() => contentBuilder.ToString(baseline, contentBuilder.Length - baseline); + 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 run.Transport.StreamTurnAsync(request, 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 (IsNative()) { + if (turn.ToolCalls.Count > 0) { + 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, 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(); + continue; + } + + if (turn.MalformedToolCall) { + // Transport reported malformed metadata; ask the model to self-correct. + run.History.Add(LlmMessage.User(MalformedToolCallCorrection)); + continue; + } + + run.History.Add(LlmMessage.Assistant(turnText, turn.Reasoning)); + yield break; + } + + // 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) { + 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, run.ToolContext); + contentBuilder.Append(McpToolHelper.FormatToolCallDisplay(first.toolName, first.arguments)); + yield return NewContent(); + run.History.Add(LlmMessage.User(isError + ? $"[Tool '{first.toolName}' Execution Failed. Error: {resultString}]" + : $"[Executed Tool '{first.toolName}'. Result: {resultString}]")); + continue; + } + + yield break; + } + + // 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 = 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 = run.Meta.BaseCycles + maxToolCycles, + NormalizedHistory = new List(run.History) + }; + } + } + + 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); + } + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs new file mode 100644 index 00000000..06a7b234 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmTransportTypes.cs @@ -0,0 +1,45 @@ +using TelegramSearchBot.Model.AI; +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; + 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/ModelCapabilityService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ModelCapabilityService.cs index 42d08acf..15f58267 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; } /// @@ -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; @@ -279,19 +279,14 @@ private string GetCapabilityDescription(string capabilityName) { } /// - /// 根据提供商获取对应的LLM服务 + /// 根据提供商获取对应的LLM服务;未知提供商返回 null(保持原有的降级语义)。 /// - private ILLMService GetLLMService(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, - _ => null - }; + private ILlmModelCatalog GetProvider(LLMProvider provider) { + try { + return _registry.GetCatalog(provider); + } catch (KeyNotFoundException) { + return null; + } } /// @@ -305,7 +300,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/OpenAIResponsesService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs deleted file mode 100644 index e36110f8..00000000 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIResponsesService.cs +++ /dev/null @@ -1,1525 +0,0 @@ -#pragma warning disable OPENAI001 // OpenAI Responses API is experimental/evaluation only - -using System; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using OpenAI; -using OpenAI.Chat; -using OpenAI.Responses; -using SkiaSharp; -using TelegramSearchBot.Attributes; -using TelegramSearchBot.Common; -using TelegramSearchBot.Interface; -using TelegramSearchBot.Interface.AI.LLM; -using TelegramSearchBot.Model; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; -using TelegramSearchBot.Model.Tools; - -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)并存。 - /// - [Injectable(ServiceLifetime.Transient)] - public class OpenAIResponsesService : IService, ILLMService { - public string ServiceName => "OpenAIResponsesService"; - - /// - /// Mutable accumulator for streaming tool call argument deltas. - /// - private class ResponsesToolCallAccumulator { - public string CallId { get; set; } - public string Name { get; set; } - 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 DataDbContext _dbContext; - private readonly IHttpClientFactory _httpClientFactory; - private readonly IMessageExtensionService _messageExtensionService; - private readonly LlmVisibilityService _llmVisibilityService; - private readonly PromptCachingSettingsService _promptCachingSettingsService; - - public OpenAIResponsesService( - DataDbContext context, - 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) { - _logger = logger; - _dbContext = context; - _messageExtensionService = messageExtensionService; - _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _llmVisibilityService = llmVisibilityService; - _promptCachingSettingsService = promptCachingSettingsService; - _logger.LogInformation("OpenAIResponsesService 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) { - return inputItems.ToList(); - } - - return inputItems.Take(inputItems.Count - 1).ToList(); - } - - private static (string toolDefinitionHash, string stablePrefixHash, string promptCacheKey) BuildPromptCachingContext( - string providerName, - string modelName, - string mode, - string instructions, - List inputItems, - bool excludeDynamicTail) { - var toolDefinitionHash = PromptCachingHelper.ComputeToolDefinitionHash(); - var stablePrefixHash = PromptCachingHelper.ComputeStablePrefixHash(new { - Mode = mode, - Instructions = instructions, - StableHistory = SerializeInputItems(GetStablePrefixInputItems(inputItems, excludeDynamicTail)), - }); - var promptCacheKey = PromptCachingHelper.BuildOpenAiPromptCacheKey(providerName, modelName, toolDefinitionHash, stablePrefixHash); - 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 - // ======================================================================== - - 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) { - 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; - } - - await foreach (var item in ExecWithResponsesApiAsync(message, ChatId, modelName, channel, binding, executionContext, cancellationToken)) { - yield return item; - } - } - - // ======================================================================== - // Core Responses API Execution - // ======================================================================== - - private async IAsyncEnumerable ExecWithResponsesApiAsync( - Message message, long ChatId, string modelName, LLMChannel channel, - LLMApiBinding binding, - LlmExecutionContext executionContext, - [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); - } - } - - 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); - - // --- 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); - } - } - - // ======================================================================== - // Resume From 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; - } - 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, snapshot.ProviderHistory?.Count ?? 0); - - 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); - } - } - - 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 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; - - bool resumeStreamingError = false; - - await foreach (var update in responsesClient.CreateResponseStreamingAsync(options, cancellationToken).WithCancellation(cancellationToken)) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - 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; - } - - // Yield progressive text outside try-catch - if (newContentBuilder.Length > 10) { - yield return newContentBuilder.ToString(); - } - } - - if (resumeStreamingError) { - yield break; - } - - if (shouldObservePromptCaching) { - LogPromptCachingObservation( - "OpenAIResponses", - channel, - modelName, - promptCachingEnabled, - toolDefinitionHash, - stablePrefixHash, - promptCacheKey, - completedResult?.Usage, - cacheKeyAttached); - } - - 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 (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; - } - - 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 toolIndicator = McpToolHelper.FormatToolCallDisplay(name, argsDict); - newContentBuilder.Append(toolIndicator); - fullContentBuilder.Append(toolIndicator); - yield return newContentBuilder.ToString(); - - inputItems.Add(ResponseItem.CreateFunctionCallOutputItem(callId, toolResultString)); - } - continue; - } - - // No tool calls - yield final content - if (!string.IsNullOrWhiteSpace(responseText)) { - yield return newContentBuilder.ToString(); - } - 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), - }; - } - } finally { - // No cleanup needed - } - } - - // ======================================================================== - // Embeddings (same approach as OpenAIService) - // ======================================================================== - - 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); - - 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> 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> 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(); - } - - // binding 路由:确定性 endpoint,不做品牌/URL 猜测(blueprint §六.7) - try { - var handler = new HttpClientHandler { - Proxy = WebRequest.DefaultWebProxy, - UseProxy = true - }; - using var httpClient = new HttpClient(handler); - OpencodeSessionHeaders.Apply(httpClient, channel, binding); - - 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); - } catch (Exception ex) { - _logger.LogError(ex, "Error getting OpenAI model list (Gateway: {Gateway})", LlmBindingSupport.ResolveEndpoint(channel, binding)); - return new List(); - } - } - - public async Task IsHealthyAsync(LLMChannel channel, LLMApiBinding binding) { - var models = await GetAllModels(channel, binding); - return models.Any(); - } - - public async Task> GetAllModelsWithCapabilities(LLMChannel channel) { - using var httpClient = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); - - try { - var internalApiUrl = channel.Gateway.TrimEnd('/') + "/dashboard/onboarding/models"; - httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); - - var response = await httpClient.GetAsync(internalApiUrl); - if (response.IsSuccessStatusCode) { - var content = await response.Content.ReadAsStringAsync(); - return ParseOpenAIModelsWithCapabilities(content); - } - - _logger.LogInformation("Internal API failed, falling back to standard models API with hardcoded capabilities"); - - 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(m => InferOpenAIModelCapabilities(m.Id)); - } catch (Exception ex) { - _logger.LogError(ex, "Error getting OpenAI models with capabilities"); - return new List(); - } - } - - // ======================================================================== - // 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 AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) { - if (string.IsNullOrWhiteSpace(modelName)) { - modelName = "gpt-4o"; - } - prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; - - 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); - return $"Error: {ServiceName} channel/gateway/apikey is not configured."; - } - - using var httpClient = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, channel, binding); - - // For image analysis, use Chat Completions API (vision support is more mature) - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(LlmBindingSupport.ResolveEndpoint(channel, binding)), - Transport = new HttpClientPipelineTransport(httpClient), - }; - var chatClient = new ChatClient(model: modelName, credential: new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); - - try { - using var fileStream = File.OpenRead(photoPath); - var tg_img = SKBitmap.Decode(fileStream); - var tg_img_data = tg_img.Encode(SKEncodedImageFormat.Png, 99); - var tg_img_arr = tg_img_data.ToArray(); - - var messages = new List { - new UserChatMessage(new List { - ChatMessageContentPart.CreateTextPart(prompt), - ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(tg_img_arr), "image/png"), - }), - }; - - var responseBuilder = new StringBuilder(); - await foreach (var update in chatClient.CompleteChatStreamingAsync(messages)) { - foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { - if (updatePart?.Text != null) responseBuilder.Append(updatePart.Text); - } - } - return responseBuilder.ToString(); - } catch (Exception ex) { - _logger.LogError(ex, "Error analyzing image with OpenAI Responses Service"); - return $"Error analyzing image: {ex.Message}"; - } - } - - // ======================================================================== - // 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)) { - 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) { - 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; - - var encoded = bitmap.Encode(SKEncodedImageFormat.Png, 90); - return encoded?.ToArray(); - } catch (Exception ex) { - _logger.LogDebug(ex, "无法加载消息图片: ChatId={ChatId}, MessageId={MessageId}", chatId, messageId); - return null; - } - } - - // ======================================================================== - // Helper: Model capability parsing (from OpenAIService) - // ======================================================================== - - private IEnumerable ParseOpenAIModelsWithCapabilities(string jsonContent) { - try { - var modelsData = JsonConvert.DeserializeObject(jsonContent); - var results = new List(); - - if (modelsData?.data != null) { - foreach (var modelData in modelsData.data) { - var modelWithCaps = new ModelWithCapabilities { - ModelName = modelData.id?.ToString() ?? "" - }; - - if (modelData.features != null) { - foreach (var feature in modelData.features) { - string featureName = feature?.ToString() ?? ""; - modelWithCaps.SetCapability(featureName, true); - } - } - - if (modelData.capabilities != null) { - foreach (var capability in modelData.capabilities) { - string capName = capability.Name?.ToString() ?? ""; - string capValue = capability.Value?.ToString() ?? ""; - modelWithCaps.SetCapability(capName, capValue); - } - } - - results.Add(modelWithCaps); - } - } - - return results; - } catch (Exception ex) { - _logger.LogError(ex, "Error parsing OpenAI models capabilities JSON"); - return new List(); - } - } - - private ModelWithCapabilities InferOpenAIModelCapabilities(string modelName) { - var model = new ModelWithCapabilities { ModelName = modelName }; - var lowerName = modelName.ToLower(); - - if (lowerName.Contains("embedding") || lowerName.Contains("ada")) { - model.SetCapability("embedding", true); - model.SetCapability("function_calling", false); - model.SetCapability("vision", false); - } else if (lowerName.StartsWith("gpt-4")) { - model.SetCapability("function_calling", true); - model.SetCapability("streaming", true); - model.SetCapability("response_json_object", true); - - if (lowerName.Contains("vision") || lowerName.Contains("4o") || lowerName.Contains("4-turbo")) { - model.SetCapability("vision", true); - model.SetCapability("image_content", true); - model.SetCapability("multimodal", true); - } - - if (lowerName.Contains("4o") || lowerName.Contains("4-turbo") || lowerName.Contains("1106") || lowerName.Contains("0125")) { - model.SetCapability("parallel_tool_calls", true); - model.SetCapability("response_json_schema", true); - } - } else if (lowerName.StartsWith("gpt-3.5")) { - model.SetCapability("function_calling", true); - model.SetCapability("streaming", true); - if (lowerName.Contains("1106") || lowerName.Contains("0125")) { - model.SetCapability("response_json_object", true); - } - } else if (ModelWithCapabilities.IsKnownImageGenerationModelName(modelName)) { - model.SetCapability("image_generation", true); - model.SetCapability("text_to_image", true); - model.SetCapability("function_calling", false); - } else if (lowerName.Contains("whisper")) { - model.SetCapability("audio_transcription", true); - model.SetCapability("function_calling", false); - } else if (lowerName.Contains("tts")) { - model.SetCapability("text_to_speech", true); - model.SetCapability("function_calling", false); - } - - // Responses API capabilities - only for modern models that support them - if (lowerName.Contains("4o") || lowerName.Contains("o1") || lowerName.Contains("o3")) { - model.SetCapability("responses_api", true); - model.SetCapability("web_search", true); - model.SetCapability("file_search", true); - } - - 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); - } - - Task> ILLMService.GetAllModels(LLMChannel channel) { - return GetAllModels(channel); - } - - Task> ILLMService.GetAllModelsWithCapabilities(LLMChannel channel) { - return GetAllModelsWithCapabilities(channel); - } - - Task ILLMService.GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { - return GenerateEmbeddingsAsync(text, modelName, channel); - } - - IAsyncEnumerable ILLMService.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 deleted file mode 100644 index 2c7efabd..00000000 --- a/TelegramSearchBot.LLM/Service/AI/LLM/OpenAIService.cs +++ /dev/null @@ -1,2166 +0,0 @@ -using System; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Http; // Added for IHttpClientFactory -using System.Reflection; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading; // For CancellationToken -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using OpenAI; -using OpenAI.Chat; -using OpenAI.Embeddings; -using SkiaSharp; // Added for image processing -using TelegramSearchBot.Attributes; -using TelegramSearchBot.Common; -using TelegramSearchBot.Interface; -using TelegramSearchBot.Interface.AI.LLM; -using TelegramSearchBot.Model; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Data; -using TelegramSearchBot.Model.Tools; // For BraveSearchResult -// Using alias for the common internal ChatMessage format -using CommonChat = OpenAI.Chat; - -namespace TelegramSearchBot.Service.AI.LLM { - // Standalone implementation, not inheriting from BaseLlmService - [Injectable(ServiceLifetime.Transient)] - public class OpenAIService : IService, ILLMService { - public string ServiceName => "OpenAIService"; - - /// - /// Mutable accumulator for streaming tool call updates. - /// - private class ToolCallAccumulator { - public string Id { get; set; } - public string Name { get; set; } - public StringBuilder Arguments { get; } = new StringBuilder(); - } - - internal static string NormalizeToolCallId(string toolCallId) { - return string.IsNullOrWhiteSpace(toolCallId) - ? $"call_{Guid.NewGuid():N}" - : toolCallId.Trim(); - } - - internal static string NormalizeToolCallName(string toolCallName) { - return string.IsNullOrWhiteSpace(toolCallName) - ? "unknown" - : toolCallName.Trim(); - } - - internal static string NormalizeToolCallArguments(string argumentsJson) { - return string.IsNullOrWhiteSpace(argumentsJson) ? "{}" : argumentsJson; - } - - internal static Dictionary DeserializeToolArgumentsForDisplay(string argumentsJson) { - try { - var normalized = NormalizeToolCallArguments(argumentsJson); - var values = JsonConvert.DeserializeObject>(normalized); - return values?.ToDictionary( - kvp => kvp.Key, - kvp => kvp.Value?.ToString() ?? string.Empty) - ?? new Dictionary(); - } catch { - return new Dictionary(); - } - } - - internal static bool IsMiniMaxCompatibleEndpoint(LLMChannel channel, string modelName) { - if (channel?.Provider == LLMProvider.MiniMax) { - return true; - } - - var gateway = channel?.Gateway ?? string.Empty; - var model = modelName ?? string.Empty; - return gateway.Contains("minimax", StringComparison.OrdinalIgnoreCase) || - model.Contains("minimax", StringComparison.OrdinalIgnoreCase); - } - - internal static string NormalizeOpenAIEndpoint(LLMChannel channel, string endpoint = null) { - // endpoint 为 binding 解析后的地址(无 binding 时回退 channel.Gateway,blueprint §六.1) - var gateway = endpoint ?? channel?.Gateway ?? string.Empty; - if (channel?.Provider != LLMProvider.MiniMax) { - return gateway; - } - - gateway = gateway.TrimEnd('/'); - return !string.IsNullOrEmpty(gateway) && - !gateway.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) - ? $"{gateway}/v1" - : gateway; - } - - private static string SanitizeAndTruncateArguments(string arguments, int maxChars = 2048) { - if (string.IsNullOrWhiteSpace(arguments)) { - return string.Empty; - } - - var sanitized = arguments; - var sensitiveKeys = new[] { "api_key", "apikey", "apiKey", "token", "password", "secret", "authorization" }; - foreach (var key in sensitiveKeys) { - sanitized = Regex.Replace( - sanitized, - $"(\"{Regex.Escape(key)}\"\\s*:\\s*\")[^\"]*(\")", - "$1***$2", - RegexOptions.IgnoreCase); - } - - sanitized = sanitized.Replace("\r", "\\r").Replace("\n", "\\n"); - return sanitized.Length <= maxChars - ? sanitized - : sanitized.Substring(0, maxChars) + $"..."; - } - - 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 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( - DataDbContext context, - ILogger logger, - IMessageExtensionService messageExtensionService, - IHttpClientFactory httpClientFactory, - IBotIdentityProvider botIdentityProvider, - IGroupLlmSettingsService groupLlmSettingsService, - LlmVisibilityService llmVisibilityService = null, - PromptCachingSettingsService promptCachingSettingsService = null) { - _logger = logger; - _dbContext = context; - _messageExtensionService = messageExtensionService; - _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _groupLlmSettingsService = groupLlmSettingsService; - _llmVisibilityService = llmVisibilityService; - _promptCachingSettingsService = promptCachingSettingsService; - _logger.LogInformation("OpenAIService 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) { - return providerHistory.ToList(); - } - - return providerHistory.Take(providerHistory.Count - 1).ToList(); - } - - private static (string toolDefinitionHash, string stablePrefixHash, string promptCacheKey) BuildPromptCachingContext( - string providerName, - string modelName, - string mode, - List providerHistory, - bool excludeDynamicTail) { - var toolDefinitionHash = PromptCachingHelper.ComputeToolDefinitionHash(); - var stablePrefixHash = PromptCachingHelper.ComputeStablePrefixHash(new { - Mode = mode, - StableHistory = SerializeProviderHistory(GetStablePrefixMessages(providerHistory, excludeDynamicTail)), - }); - var promptCacheKey = PromptCachingHelper.BuildOpenAiPromptCacheKey(providerName, modelName, toolDefinitionHash, stablePrefixHash); - return (toolDefinitionHash, stablePrefixHash, promptCacheKey); - } - - private void LogOpenAiPromptCachingObservation( - 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 = 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, - }); - } - - public virtual async Task> GetAllModels(LLMChannel channel) { - if (channel.Provider.Equals(LLMProvider.Ollama)) { - return new List(); - } - - if (channel.Provider == LLMProvider.MiniMax) { - var models = await GetGenericOpenAICompatibleModels(channel); - return models.Any() ? models : _miniMaxModels; - } - - // 检查是否为OpenRouter - if (IsOpenRouter(channel.Gateway)) { - return await GetOpenRouterModels(channel); - } - - // 首先尝试使用通用 HTTP 方式获取模型列表(兼容 MiniMax 等非标准 OpenAI 兼容 API) - var genericModels = await GetGenericOpenAICompatibleModels(channel); - if (genericModels.Any()) { - return genericModels; - } - - // 回退到 OpenAI SDK - try { - var handler = new HttpClientHandler { - Proxy = WebRequest.DefaultWebProxy, - UseProxy = true - }; - - using var httpClient = new HttpClient(handler); - OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); - - // --- Client Setup --- - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel)), - 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 from s in models.Value - select s.Id; - } catch (Exception ex) { - _logger.LogError(ex, "使用 OpenAI SDK 获取模型列表失败 (Gateway: {Gateway})", channel.Gateway); - return new List(); - } - } - - public virtual async Task> GetAllModels(LLMChannel channel, LLMApiBinding binding) { - if (channel == null) return new List(); - if (binding == null) return await GetAllModels(channel); - - // binding 路由:确定性 endpoint,不做品牌/URL 猜测(blueprint §六.7) - var genericModels = await GetGenericOpenAICompatibleModels(channel, binding); - if (genericModels.Any()) { - return genericModels; - } - - try { - var handler = new HttpClientHandler { - Proxy = WebRequest.DefaultWebProxy, - UseProxy = true - }; - - using var httpClient = new HttpClient(handler); - - // --- Client Setup --- - 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 from s in models.Value - select s.Id; - } catch (Exception ex) { - _logger.LogError(ex, "使用 OpenAI SDK 获取模型列表失败 (Gateway: {Gateway})", LlmBindingSupport.ResolveEndpoint(channel, binding)); - return new List(); - } - } - - public async Task IsHealthyAsync(LLMChannel channel, LLMApiBinding binding) { - var models = await GetAllModels(channel, binding); - return models.Any(); - } - - /// - /// 使用通用 HTTP GET 方式获取 OpenAI 兼容 API 的模型列表,兼容 MiniMax、DeepSeek 等提供商 - /// - private async Task> GetGenericOpenAICompatibleModels(LLMChannel channel, LLMApiBinding? binding = null) { - try { - var httpClient = _httpClientFactory.CreateClient(); - var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); - if (!string.IsNullOrEmpty(apiKey)) { - httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); - } - OpencodeSessionHeaders.Apply(httpClient, channel, binding); - - // 构建模型列表 URL,确保路径正确 - var gatewayBase = NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)).TrimEnd('/'); - var modelsUrl = gatewayBase.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) - ? $"{gatewayBase}/models" - : $"{gatewayBase}/v1/models"; - - var response = await httpClient.GetAsync(modelsUrl); - if (!response.IsSuccessStatusCode && channel.Provider != LLMProvider.MiniMax) { - // 尝试不带 /v1 的路径 - var altUrl = $"{gatewayBase}/models"; - if (altUrl != modelsUrl) { - response = await httpClient.GetAsync(altUrl); - } - } - - if (response.IsSuccessStatusCode) { - var content = await response.Content.ReadAsStringAsync(); - var modelsData = JsonConvert.DeserializeObject(content); - - var models = new List(); - if (modelsData?.data != null) { - foreach (var model in modelsData.data) { - string modelId = model.id?.ToString(); - if (!string.IsNullOrEmpty(modelId)) { - models.Add(modelId); - } - } - } - - if (models.Any()) { - _logger.LogInformation("通用 HTTP 方式获取到 {Count} 个模型 (Gateway: {Gateway})", models.Count, channel.Gateway); - } - return models; - } else { - _logger.LogDebug("通用 HTTP 方式获取模型列表失败: {StatusCode} (Gateway: {Gateway})", response.StatusCode, channel.Gateway); - return new List(); - } - } catch (Exception ex) { - _logger.LogDebug(ex, "通用 HTTP 方式获取模型列表出错 (Gateway: {Gateway})", channel.Gateway); - return new List(); - } - } - - /// - /// 检查是否为OpenRouter服务 - /// - private bool IsOpenRouter(string gateway) { - return !string.IsNullOrEmpty(gateway) && - ( gateway.Contains("openrouter.ai") || gateway.Contains("openrouter") ); - } - - /// - /// MiniMax OpenAI-compatible text model fallback snapshot. - /// - private static readonly string[] _miniMaxModels = { - "MiniMax-M3", - "MiniMax-M2.7", - "MiniMax-M2.7-highspeed", - "MiniMax-M2.5", - "MiniMax-M2.5-highspeed", - "MiniMax-M2.1", - "MiniMax-M2.1-highspeed", - "MiniMax-M2" - }; - - /// - /// 获取OpenRouter模型列表 - /// - private async Task> GetOpenRouterModels(LLMChannel channel) { - try { - var httpClient = _httpClientFactory.CreateClient(); - httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); - - var response = await httpClient.GetAsync("https://openrouter.ai/api/v1/models"); - if (response.IsSuccessStatusCode) { - var content = await response.Content.ReadAsStringAsync(); - var modelsData = JsonConvert.DeserializeObject(content); - - var models = new List(); - if (modelsData?.data != null) { - foreach (var model in modelsData.data) { - string modelId = model.id?.ToString(); - if (!string.IsNullOrEmpty(modelId)) { - models.Add(modelId); - } - } - } - - _logger.LogInformation("获取到 {Count} 个OpenRouter模型", models.Count); - return models; - } else { - _logger.LogWarning("获取OpenRouter模型失败: {StatusCode}", response.StatusCode); - return new List(); - } - } catch (Exception ex) { - _logger.LogError(ex, "获取OpenRouter模型时出错"); - return new List(); - } - } - - /// - /// 获取OpenAI模型及其能力信息 - /// - public virtual async Task> GetAllModelsWithCapabilities(LLMChannel channel) { - if (channel.Provider.Equals(LLMProvider.Ollama)) { - return new List(); - } - - if (channel.Provider == LLMProvider.MiniMax) { - var models = await GetAllModels(channel); - return models.Select(InferOpenAIModelCapabilities); - } - - // 检查是否为OpenRouter - if (IsOpenRouter(channel.Gateway)) { - return await GetOpenRouterModelsWithCapabilities(channel); - } - - // 检查是否为LMStudio(通过capabilities字段检测) - if (channel.Provider == LLMProvider.LMStudio) { - return await GetLMStudioModelsWithCapabilities(channel); - } - - - using var httpClient = _httpClientFactory.CreateClient(); - - try { - // 尝试使用OpenAI内部API获取模型能力信息 - var internalApiUrl = channel.Gateway.TrimEnd('/') + "/dashboard/onboarding/models"; - httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); - OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); - - var response = await httpClient.GetAsync(internalApiUrl); - if (response.IsSuccessStatusCode) { - var content = await response.Content.ReadAsStringAsync(); - var modelsWithCapabilities = ParseOpenAIModelsWithCapabilities(content); - if (modelsWithCapabilities.Any()) { - _logger.LogInformation("Successfully retrieved {Count} OpenAI models with capabilities from internal API", modelsWithCapabilities.Count()); - return modelsWithCapabilities; - } - } - - _logger.LogInformation("Internal API failed, falling back to standard models API with hardcoded capabilities"); - - // 如果内部API失败,使用标准API并根据模型名称推断能力 - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel)), - 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(m => InferOpenAIModelCapabilities(m.Id)); - } catch (Exception ex) { - _logger.LogError(ex, "Error getting OpenAI models with capabilities"); - return new List(); - } - } - - /// - /// 解析OpenAI内部API返回的模型能力信息 - /// - private IEnumerable ParseOpenAIModelsWithCapabilities(string jsonContent) { - try { - var modelsData = JsonConvert.DeserializeObject(jsonContent); - var results = new List(); - - if (modelsData?.data != null) { - foreach (var modelData in modelsData.data) { - var modelWithCaps = new ModelWithCapabilities { - ModelName = modelData.id?.ToString() ?? "" - }; - - // 解析features数组 - if (modelData.features != null) { - foreach (var feature in modelData.features) { - string featureName = feature?.ToString() ?? ""; - modelWithCaps.SetCapability(featureName, true); - } - } - - // 解析其他能力字段 - if (modelData.capabilities != null) { - foreach (var capability in modelData.capabilities) { - string capName = capability.Name?.ToString() ?? ""; - string capValue = capability.Value?.ToString() ?? ""; - modelWithCaps.SetCapability(capName, capValue); - } - } - - results.Add(modelWithCaps); - } - } - - return results; - } catch (Exception ex) { - _logger.LogError(ex, "Error parsing OpenAI models capabilities JSON"); - return new List(); - } - } - - /// - /// 根据OpenAI模型名称推断能力 - /// - private ModelWithCapabilities InferOpenAIModelCapabilities(string modelName) { - var model = new ModelWithCapabilities { ModelName = modelName }; - - // 基于模型名称的能力推断 - var lowerName = modelName.ToLower(); - - // 嵌入模型 - if (lowerName.Contains("embedding") || lowerName.Contains("ada")) { - model.SetCapability("embedding", true); - model.SetCapability("function_calling", false); - model.SetCapability("vision", false); - } - // GPT-4系列模型 - else if (lowerName.StartsWith("gpt-4")) { - model.SetCapability("function_calling", true); - model.SetCapability("streaming", true); - model.SetCapability("response_json_object", true); - - // GPT-4 Vision模型 - if (lowerName.Contains("vision") || lowerName.Contains("4o") || lowerName.Contains("4-turbo")) { - model.SetCapability("vision", true); - model.SetCapability("image_content", true); - model.SetCapability("multimodal", true); - } - - // 较新的模型支持并行工具调用 - if (lowerName.Contains("4o") || lowerName.Contains("4-turbo") || lowerName.Contains("1106") || lowerName.Contains("0125")) { - model.SetCapability("parallel_tool_calls", true); - model.SetCapability("response_json_schema", true); - } - } - // GPT-3.5系列模型 - else if (lowerName.StartsWith("gpt-3.5")) { - model.SetCapability("function_calling", true); - model.SetCapability("streaming", true); - - if (lowerName.Contains("1106") || lowerName.Contains("0125")) { - model.SetCapability("response_json_object", true); - } - } - // 图片生成模型 - else if (ModelWithCapabilities.IsKnownImageGenerationModelName(modelName)) { - model.SetCapability("image_generation", true); - model.SetCapability("text_to_image", true); - model.SetCapability("function_calling", false); - } - // Whisper模型 - else if (lowerName.Contains("whisper")) { - model.SetCapability("audio_transcription", true); - model.SetCapability("function_calling", false); - } - // TTS模型 - else if (lowerName.Contains("tts")) { - model.SetCapability("text_to_speech", true); - model.SetCapability("function_calling", false); - } - - return model; - } - - /// - /// 获取OpenRouter模型及其能力信息 - /// - private async Task> GetOpenRouterModelsWithCapabilities(LLMChannel channel) { - try { - var httpClient = _httpClientFactory.CreateClient(); - httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); - - var response = await httpClient.GetAsync("https://openrouter.ai/api/v1/models"); - if (response.IsSuccessStatusCode) { - var content = await response.Content.ReadAsStringAsync(); - var modelsData = JsonConvert.DeserializeObject(content); - - var results = new List(); - if (modelsData?.data != null) { - foreach (var modelData in modelsData.data) { - var modelWithCaps = ParseOpenRouterModelCapabilities(modelData); - if (modelWithCaps != null) { - results.Add(modelWithCaps); - } - } - } - - _logger.LogInformation("获取到 {Count} 个OpenRouter模型及其能力信息", results.Count); - return results; - } else { - _logger.LogWarning("获取OpenRouter模型能力失败: {StatusCode}", response.StatusCode); - return new List(); - } - } catch (Exception ex) { - _logger.LogError(ex, "获取OpenRouter模型能力时出错"); - return new List(); - } - } - - /// - /// 解析OpenRouter模型能力信息 - /// - private ModelWithCapabilities ParseOpenRouterModelCapabilities(dynamic modelData) { - try { - string modelId = modelData.id?.ToString(); - if (string.IsNullOrEmpty(modelId)) { - return null; - } - - var model = new ModelWithCapabilities { ModelName = modelId }; - - // 基本信息 - if (modelData.name != null) { - model.SetCapability("display_name", modelData.name.ToString()); - } - - if (modelData.description != null) { - model.SetCapability("description", modelData.description.ToString()); - } - - if (modelData.context_length != null) { - model.SetCapability("context_length", modelData.context_length.ToString()); - } - - // 架构信息 - if (modelData.architecture != null) { - var architecture = modelData.architecture; - - // 输入模态 - if (architecture.input_modalities != null) { - bool supportsText = false; - bool supportsImage = false; - - foreach (var modality in architecture.input_modalities) { - string modalityStr = modality.ToString().ToLower(); - if (modalityStr == "text") { - supportsText = true; - } else if (modalityStr == "image") { - supportsImage = true; - } - } - - model.SetCapability("text_input", supportsText); - model.SetCapability("vision", supportsImage); - model.SetCapability("image_content", supportsImage); - model.SetCapability("multimodal", supportsImage); - } - - // 输出模态 - if (architecture.output_modalities != null) { - foreach (var modality in architecture.output_modalities) { - string modalityStr = modality.ToString().ToLower(); - if (modalityStr == "text") { - model.SetCapability("text_output", true); - } - } - } - - if (architecture.tokenizer != null) { - model.SetCapability("tokenizer", architecture.tokenizer.ToString()); - } - } - - // 定价信息 - if (modelData.pricing != null) { - var pricing = modelData.pricing; - if (pricing.prompt != null) { - model.SetCapability("prompt_price", pricing.prompt.ToString()); - } - if (pricing.completion != null) { - model.SetCapability("completion_price", pricing.completion.ToString()); - } - if (pricing.image != null) { - model.SetCapability("image_price", pricing.image.ToString()); - } - } - - // 支持的参数 - if (modelData.supported_parameters != null) { - var supportedParams = new List(); - foreach (var param in modelData.supported_parameters) { - string paramStr = param.ToString(); - supportedParams.Add(paramStr); - - // 检查工具调用支持 - if (paramStr.ToLower().Contains("tool") || paramStr.ToLower().Contains("function")) { - model.SetCapability("function_calling", true); - model.SetCapability("tool_calls", true); - } - - // 检查流式支持 - if (paramStr.ToLower().Contains("stream")) { - model.SetCapability("streaming", true); - } - - // 检查JSON格式支持 - if (paramStr.ToLower().Contains("response_format")) { - model.SetCapability("response_json_object", true); - } - } - - model.SetCapability("supported_parameters", string.Join(", ", supportedParams)); - } - - // 基于模型名称的额外推断 - var lowerModelId = modelId.ToLower(); - - // 推断提供商 - if (lowerModelId.Contains("openai/") || lowerModelId.Contains("gpt")) { - model.SetCapability("provider", "OpenAI"); - } else if (lowerModelId.Contains("anthropic/") || lowerModelId.Contains("claude")) { - model.SetCapability("provider", "Anthropic"); - } else if (lowerModelId.Contains("google/") || lowerModelId.Contains("gemini")) { - model.SetCapability("provider", "Google"); - } else if (lowerModelId.Contains("meta/") || lowerModelId.Contains("llama")) { - model.SetCapability("provider", "Meta"); - } else if (lowerModelId.Contains("mistral/")) { - model.SetCapability("provider", "Mistral"); - } - - // 默认能力设置 - model.SetCapability("chat", true); - - // 如果没有明确的工具调用信息,基于模型名称推断 - if (!model.GetCapabilityBool("function_calling")) { - if (lowerModelId.Contains("gpt-4") || lowerModelId.Contains("gpt-3.5") || - lowerModelId.Contains("claude") || lowerModelId.Contains("gemini")) { - model.SetCapability("function_calling", true); - model.SetCapability("tool_calls", true); - } - } - - return model; - } catch (Exception ex) { - string modelDataStr = modelData?.ToString() ?? "null"; - _logger.LogError(ex, "解析OpenRouter模型能力时出错: {ModelData}", modelDataStr); - return null; - } - } - - // --- LMStudio Capability Detection --- - - /// - /// 获取LMStudio模型及其能力信息 - /// LMStudio在/v1/models返回的模型对象中包含capabilities数组 - /// - private async Task> GetLMStudioModelsWithCapabilities(LLMChannel channel) { - try { - var httpClient = _httpClientFactory.CreateClient(); - if (!string.IsNullOrEmpty(channel.ApiKey)) { - httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); - } - - var gatewayBase = channel.Gateway.TrimEnd('/'); - var modelsUrl = gatewayBase.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) - ? $"{gatewayBase}/models" - : $"{gatewayBase}/v1/models"; - - var response = await httpClient.GetAsync(modelsUrl); - if (response.IsSuccessStatusCode) { - var content = await response.Content.ReadAsStringAsync(); - var modelsData = JsonConvert.DeserializeObject(content); - - var results = new List(); - if (modelsData?.data != null) { - foreach (var modelData in modelsData.data) { - var modelWithCaps = ParseLMStudioModelCapabilities(modelData); - if (modelWithCaps != null) { - results.Add(modelWithCaps); - } - } - } - - _logger.LogInformation("获取到 {Count} 个LMStudio模型及其能力信息", results.Count); - return results; - } else { - _logger.LogWarning("获取LMStudio模型失败: {StatusCode}", response.StatusCode); - // 回退到通用方式 - var genericModels = await GetGenericOpenAICompatibleModels(channel); - return genericModels.Select(m => InferOpenAIModelCapabilities(m)); - } - } catch (Exception ex) { - _logger.LogError(ex, "获取LMStudio模型能力时出错"); - return new List(); - } - } - - /// - /// 解析LMStudio模型能力信息 - /// LMStudio模型包含capabilities数组,如["tool_use", "vision", "chat", "text-completion"] - /// - private ModelWithCapabilities ParseLMStudioModelCapabilities(dynamic modelData) { - try { - string modelId = modelData.id?.ToString(); - if (string.IsNullOrEmpty(modelId)) { - return null; - } - - var model = new ModelWithCapabilities { ModelName = modelId }; - - // 基本能力 - model.SetCapability("streaming", true); - model.SetCapability("chat", true); - - // 解析LMStudio的capabilities数组 - if (modelData.capabilities != null) { - foreach (var capability in modelData.capabilities) { - string capStr = capability.ToString().ToLower(); - switch (capStr) { - case "tool_use": - model.SetCapability("function_calling", true); - model.SetCapability("tool_calls", true); - break; - case "vision": - model.SetCapability("vision", true); - model.SetCapability("multimodal", true); - model.SetCapability("image_content", true); - break; - case "embeddings": - model.SetCapability("embedding", true); - model.SetCapability("text_embedding", true); - break; - case "chat": - model.SetCapability("chat", true); - break; - case "text-completion": - model.SetCapability("text_generation", true); - break; - } - } - } - - // 解析LMStudio扩展字段 - if (modelData.type != null) { - string modelType = modelData.type.ToString().ToLower(); - if (modelType == "vlm") { - model.SetCapability("vision", true); - model.SetCapability("multimodal", true); - } else if (modelType == "embeddings") { - model.SetCapability("embedding", true); - } - } - - if (modelData.max_context_length != null) { - model.SetCapability("input_token_limit", modelData.max_context_length.ToString()); - } - - if (modelData.arch != null) { - model.SetCapability("model_family", modelData.arch.ToString()); - } - - if (modelData.quantization != null) { - model.SetCapability("quantization", modelData.quantization.ToString()); - } - - model.SetCapability("provider", "LMStudio"); - - return model; - } catch (Exception ex) { - string modelDataStr = modelData?.ToString() ?? "null"; - _logger.LogError(ex, "解析LMStudio模型能力时出错: {ModelData}", modelDataStr); - return null; - } - } - - // --- 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 --- - 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; - } - - // Try native tool calling first; fall back to XML prompt-based if it fails - bool useNativeToolCalling = true; - var nativeTools = McpToolHelper.GetNativeToolDefinitions(); - - if (nativeTools == null || !nativeTools.Any()) { - useNativeToolCalling = false; - } - - 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); - - if (useNativeToolCalling) { - bool nativeFailed = false; - var nativeEnumerator = ExecWithNativeToolCallingAsync(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(message, ChatId, modelName, channel, binding, executionContext, 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 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( - 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(); - - // --- 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 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 - } - } - - /// - /// Execute LLM with XML prompt-based tool calling (fallback for models that don't support native tool calling). - /// - private async IAsyncEnumerable ExecWithXmlToolCallingAsync( - 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; - } - - try { - int maxToolCycles = Env.MaxToolCycles; - var currentMessageContentBuilder = new StringBuilder(); - - 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) { - currentMessageContentBuilder.Append(updatePart.Text); - llmResponseAccumulatorForToolParsing.Append(updatePart.Text); - if (currentMessageContentBuilder.ToString().Length > 10) { - yield return currentMessageContentBuilder.ToString(); - } - } - } - - 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 (Cycle {Cycle}): {Response}", ServiceName, cycle + 1, llmFullResponseText); - - 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); - } - - // --- 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; - - _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); - } - - 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()}."; - } - - currentMessageContentBuilder.Append(McpToolHelper.FormatToolCallDisplay(parsedToolName, toolArguments)); - yield return currentMessageContentBuilder.ToString(); - - 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); - } 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 = "OpenAI", - ChannelId = channel.Id, - LastAccumulatedContent = currentMessageContentBuilder.ToString(), - CyclesSoFar = maxToolCycles, - ProviderHistory = SerializeProviderHistory(providerHistory), - }; - } - } finally { - // No cleanup needed for ToolContext - } - } - - /// - /// 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; - } - - // 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 - } - } - - /// - /// 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. - /// - private 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 { - // Check for Reasoning property via reflection - var reasoningProp = assistantMsg.GetType().GetProperty("Reasoning"); - if (reasoningProp != null) { - var value = reasoningProp.GetValue(assistantMsg); - if (value is string reasoning && !string.IsNullOrEmpty(reasoning)) { - return reasoning; - } - } - - #pragma warning disable SCME0001 // Patch is for evaluation, may be changed in future - var patchProp = assistantMsg.GetType().GetProperty("Patch"); - if (patchProp?.GetValue(assistantMsg) is JsonPatch patch && - patch.TryGetValue("$.reasoning_content"u8, out string? reasoningFromPatch)) { - return reasoningFromPatch; - } - #pragma warning restore SCME0001 - } catch { - // Reflection failed, return null - } - return null; - } - - /// - /// 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) { - // 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)) { - if (reasoningFromPatch != null) { - return reasoningFromPatch; - } - } -#pragma warning restore SCME0001 - - // Fallback: reflection for SDK internal properties - try { - // Try ReasoningContentUpdate property (OpenAI SDK for thinking models) - var reasoningProp = update.GetType().GetProperty("ReasoningContentUpdate"); - if (reasoningProp != null) { - var value = reasoningProp.GetValue(update); - if (value is string reasoning && !string.IsNullOrEmpty(reasoning)) { - return reasoning; - } - } - // Fallback: try Reasoning property - var fallbackProp = update.GetType().GetProperty("Reasoning"); - if (fallbackProp != null) { - var value = fallbackProp.GetValue(update); - if (value is string fallback && !string.IsNullOrEmpty(fallback)) { - return fallback; - } - } - } catch { - // Reflection failed - } - return null; - } - - /// - /// Deserialize portable format back to OpenAI ChatMessage list. - /// - public static List DeserializeProviderHistory(List serialized, bool includeEmptyReasoningContent = false) { - var result = new List(); - if (serialized == null) return result; - - foreach (var msg in serialized) { - switch (msg.Role?.ToLowerInvariant()) { - case "system": - result.Add(new SystemChatMessage(msg.Content ?? "")); - break; - case "assistant": - var assistantMsg = new AssistantChatMessage(msg.Content ?? ""); - SetAssistantReasoningContent(assistantMsg, msg.ReasoningContent, includeEmptyReasoningContent); - result.Add(assistantMsg); - break; - case "user": - default: - result.Add(new UserChatMessage(msg.Content ?? "")); - break; - } - } - return result; - } - - /// - /// Set reasoning_content on AssistantChatMessage for thinking mode models. - /// Uses Patch.Set (OpenAI SDK v2.10.0+) with reflection fallback. - /// - internal static bool ShouldIncludeEmptyReasoningContent(LLMChannel channel, string modelName) { - var gateway = channel?.Gateway ?? string.Empty; - var model = modelName ?? string.Empty; - return gateway.Contains("deepseek", StringComparison.OrdinalIgnoreCase) || - model.Contains("deepseek", StringComparison.OrdinalIgnoreCase); - } - - private static void SetAssistantReasoningContent(AssistantChatMessage msg, string reasoningContent, bool includeEmptyReasoningContent = false) { - if (reasoningContent is null || (reasoningContent.Length == 0 && !includeEmptyReasoningContent)) { - return; - } - - // Try Patch.Set first (writes directly to JSON output) -#pragma warning disable SCME0001 // Patch API is experimental but functional - try { - var encodedReasoningContent = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(reasoningContent); - msg.Patch.Set("$.reasoning_content"u8, encodedReasoningContent.AsSpan()); - } catch { - // Patch.Set not available or failed, fall through to reflection - } -#pragma warning restore SCME0001 - // Reflection fallback for older SDK versions - try { - var prop = msg.GetType().GetProperty("Reasoning"); - if (prop != null && prop.CanWrite) { - prop.SetValue(msg, reasoningContent); - } - } catch { - // Reflection failed, ignore - } - } - - 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, channel, binding); - - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, endpoint)), - Transport = new HttpClientPipelineTransport(httpClient), - }; - - var apikey = new ApiKeyCredential(apiKey); - OpenAIClient client = new(apikey, 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(); - _logger.LogDebug("Embedding response type: {Type}", embedding.GetType().FullName); - _logger.LogDebug("Embedding response structure: {Response}", JsonConvert.SerializeObject(embedding, Formatting.Indented)); - - // Try reflection with all possible property names - 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 - try to 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; - } - } - - 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); - } - - public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) { - if (string.IsNullOrWhiteSpace(modelName)) { - modelName = "gpt-4-vision-preview"; - } - - prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; - - 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); - return $"Error: {ServiceName} channel/gateway/apikey is not configured."; - } - - using var httpClient = _httpClientFactory.CreateClient(); - OpencodeSessionHeaders.Apply(httpClient, channel, binding); - - var clientOptions = new OpenAIClientOptions { - Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding))), - Transport = new HttpClientPipelineTransport(httpClient), - }; - - var chatClient = new ChatClient(model: modelName, credential: new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); - - try { - // 读取图像并转换为Base64 - using var fileStream = File.OpenRead(photoPath); - var tg_img = SKBitmap.Decode(fileStream); - var tg_img_data = tg_img.Encode(SKEncodedImageFormat.Png, 99); - var tg_img_arr = tg_img_data.ToArray(); - var base64Image = Convert.ToBase64String(tg_img_arr); - - var messages = new List { - new UserChatMessage(new List() { - ChatMessageContentPart.CreateTextPart(prompt), - ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(tg_img_arr), "image/png"), - - }), - }; - - var responseBuilder = new StringBuilder(); - await foreach (var update in chatClient.CompleteChatStreamingAsync(messages)) { - foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { - if (updatePart?.Text != null) responseBuilder.Append(updatePart.Text); - } - } - return responseBuilder.ToString(); - } catch (Exception ex) { - _logger.LogError(ex, "Error analyzing image with OpenAI"); - return $"Error analyzing image: {ex.Message}"; - } - } - } -} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs index 2c18a32d..cec41e29 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/PromptCachingHelper.cs @@ -12,6 +12,7 @@ using OpenAI.Chat; using OpenAI.Responses; using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; namespace TelegramSearchBot.Service.AI.LLM { public static class PromptCachingOutcomes { @@ -86,6 +87,54 @@ public static void ApplyOpenAiPromptCaching(CreateResponseOptions options, strin } } + /// 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) { @@ -116,6 +165,91 @@ 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, + }); + } + + /// 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/ProviderApi/AnthropicModelApi.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs new file mode 100644 index 00000000..58392013 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs @@ -0,0 +1,521 @@ +using System; +using System.Collections.Generic; +using System.IO; +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.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using SkiaSharp; +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; +// Alias to resolve ambiguity between TelegramSearchBot.Model.Data.Message and Anthropic.Models.Messages.Message +using DataMessage = TelegramSearchBot.Model.Data.Message; + +namespace TelegramSearchBot.Service.AI.LLM { + [Injectable(ServiceLifetime.Transient)] + public class AnthropicModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + private const string ServiceName = "AnthropicModelApi"; + + private readonly ILogger _logger; + private readonly DataDbContext _dbContext; + private readonly IMessageExtensionService _messageExtensionService; + private readonly IHttpClientFactory _httpClientFactory; + + + private static readonly string[] _anthropicModels = { + "claude-sonnet-4-20250514", + "claude-opus-4-20250514", + "claude-3-5-sonnet-20241022", + "claude-3-5-haiku-20241022", + "claude-3-opus-20240229", + "claude-3-sonnet-20240229", + "claude-3-haiku-20240307" + }; + + public AnthropicModelApi( + DataDbContext context, + ILogger logger, + IMessageExtensionService messageExtensionService, + IHttpClientFactory httpClientFactory) { + _logger = logger; + _dbContext = context; + _messageExtensionService = messageExtensionService; + _httpClientFactory = httpClientFactory; + _logger.LogInformation("AnthropicModelApi instance created."); + } + + + private AnthropicClient CreateClient(LLMChannel channel, LLMApiBinding? binding = null) { + var options = new Anthropic.Core.ClientOptions { + ApiKey = LlmBindingSupport.ResolveApiKey(channel, binding), + }; + 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; + } + return new AnthropicClient(options); + } + + + internal static CacheControlEphemeral CreateCacheControl() { + return new CacheControlEphemeral(); + } + + internal static MessageCreateParamsSystem BuildSystemPrompt(string systemPrompt, bool enablePromptCaching) { + if (!enablePromptCaching) { + return systemPrompt; + } + + return new MessageCreateParamsSystem([ + new TextBlockParam(systemPrompt) { + CacheControl = CreateCacheControl(), + } + ], null); + } + + private static MessageParam CloneMessageForCachePrefix(MessageParam message, bool markCacheControl) { + if (message.Content.TryPickString(out var text)) { + return new MessageParam { + Role = message.Role, + Content = new List { + new(new TextBlockParam(text) { + CacheControl = markCacheControl ? CreateCacheControl() : null, + }, null) + } + }; + } + + if (!message.Content.TryPickContentBlockParams(out var blocks)) { + return new MessageParam { + Role = message.Role, + Content = message.Content, + }; + } + + var clonedBlocks = new List(); + for (int i = 0; i < blocks.Count; i++) { + var isLastBlock = markCacheControl && i == blocks.Count - 1; + var block = blocks[i]; + if (block.TryPickText(out var textBlock)) { + clonedBlocks.Add(new ContentBlockParam(new TextBlockParam(textBlock.Text) { + CacheControl = isLastBlock ? CreateCacheControl() : textBlock.CacheControl, + Citations = textBlock.Citations, + }, null)); + continue; + } + + if (block.TryPickImage(out var imageBlock)) { + clonedBlocks.Add(new ContentBlockParam(new ImageBlockParam { + Source = imageBlock.Source, + CacheControl = isLastBlock ? CreateCacheControl() : imageBlock.CacheControl, + }, null)); + continue; + } + + clonedBlocks.Add(block); + } + + return new MessageParam { + Role = message.Role, + Content = clonedBlocks, + }; + } + + internal static List PrepareMessagesForPromptCaching(List providerHistory, bool enablePromptCaching, bool excludeDynamicTail, out bool cacheBreakpointInserted) { + cacheBreakpointInserted = enablePromptCaching; + if (!enablePromptCaching || providerHistory.Count == 0) { + return providerHistory; + } + + var stableCount = excludeDynamicTail + ? Math.Max(providerHistory.Count - 1, 0) + : providerHistory.Count; + if (stableCount == 0) { + return providerHistory; + } + + var clonedMessages = providerHistory + .Select((message, index) => CloneMessageForCachePrefix(message, index == stableCount - 1)) + .ToList(); + cacheBreakpointInserted = true; + return clonedMessages; + } + + internal static (string toolDefinitionHash, string stablePrefixHash) BuildPromptCachingContext(string mode, string systemPrompt, List stableHistory) { + var toolDefinitionHash = PromptCachingHelper.ComputeToolDefinitionHash(); + var stablePrefixHash = PromptCachingHelper.ComputeStablePrefixHash(new { + Mode = mode, + SystemPrompt = systemPrompt, + StableHistory = stableHistory, + }); + return (toolDefinitionHash, stablePrefixHash); + } + + private void LogPromptCachingObservation( + 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 = PromptCachingHelper.DetermineAnthropicOutcome( + promptCachingEnabled, + cacheBreakpointInserted, + observationKey, + cacheCreationInputTokens, + cacheReadInputTokens, + 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, + CacheBreakpointInserted = cacheBreakpointInserted, + CacheCreationInputTokens = cacheCreationInputTokens, + CacheReadInputTokens = cacheReadInputTokens, + ProviderUsageJson = usageJson, + }); + } + + #region Models + + public virtual Task> GetAllModels(LLMChannel channel) { + return Task.FromResult>(_anthropicModels); + } + + public virtual Task> GetAllModelsWithCapabilities(LLMChannel channel) { + var results = new List(); + foreach (var modelName in _anthropicModels) { + results.Add(InferAnthropicModelCapabilities(modelName)); + } + return Task.FromResult>(results); + } + + private ModelWithCapabilities InferAnthropicModelCapabilities(string modelName) { + var model = new ModelWithCapabilities { ModelName = modelName }; + var lowerName = modelName.ToLower(); + + model.SetCapability("streaming", true); + model.SetCapability("function_calling", true); + model.SetCapability("tool_calls", true); + model.SetCapability("chat", true); + model.SetCapability("model_family", "Claude"); + + // All Claude 3+ models support vision + model.SetCapability("vision", true); + model.SetCapability("image_content", true); + + // Claude 3.5+ and Claude 4+ are multimodal + if (lowerName.Contains("claude-sonnet-4") || lowerName.Contains("claude-opus-4") || + lowerName.Contains("claude-3-5") || lowerName.Contains("claude-3.5")) { + model.SetCapability("multimodal", true); + model.SetCapability("advanced_reasoning", true); + } + + if (lowerName.Contains("opus")) { + model.SetCapability("advanced_reasoning", true); + model.SetCapability("complex_tasks", true); + } + + if (lowerName.Contains("haiku")) { + model.SetCapability("fast_response", true); + model.SetCapability("optimized", true); + } + + return model; + } + + #endregion + 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(); + } + + internal static List EnsureAlternatingRoles(List messages) { + if (!messages.Any()) return messages; + + var result = new List(); + + foreach (var msg in messages) { + if (result.Count > 0 && result.Last().Role == msg.Role) { + // Merge with previous message of same role + var prev = result.Last(); + var prevContent = ExtractTextFromContent(prev.Content); + var curContent = ExtractTextFromContent(msg.Content); + result[result.Count - 1] = new MessageParam { + Role = prev.Role, + Content = prevContent + "\n" + curContent + }; + } else { + result.Add(msg); + } + } + + // Ensure starts with user + if (result.Any() && result.First().Role == Role.Assistant) { + result.Insert(0, new MessageParam { + Role = Role.User, + Content = "(conversation start)" + }); + } + + return result; + } + + + #region Vision Support + + /// + /// 检查模型是否支持视觉能力 + /// + + /// + /// 尝试加载消息关联的图片文件,转换为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; + } + } + + #endregion + + #region Tool Conversion + + /// + /// Convert OpenAI ChatTool definitions to Anthropic Tool objects. + /// + private static List ConvertToAnthropicTools(List openAiTools, bool enablePromptCaching) { + var tools = new List(); + for (int index = 0; index < openAiTools.Count; index++) { + var chatTool = openAiTools[index]; + try { + var toolName = chatTool.FunctionName; + var toolDescription = chatTool.FunctionDescription; + var paramsJson = chatTool.FunctionParameters?.ToString() ?? "{}"; + var schemaDoc = System.Text.Json.JsonDocument.Parse(paramsJson); + 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 = toolName, + Description = toolDescription, + InputSchema = inputSchema, + CacheControl = enablePromptCaching && index == openAiTools.Count - 1 ? CreateCacheControl() : null, + }, null)); + } catch (Exception) { + } + } + + return tools; + } + + #endregion + + /// + /// Serialize Anthropic message list to portable format for snapshot persistence. + /// + public static List SerializeProviderHistory(string systemPrompt, List history) { + var result = new List(); + + if (!string.IsNullOrWhiteSpace(systemPrompt)) { + result.Add(new SerializedChatMessage { Role = "system", Content = systemPrompt }); + } + + foreach (var msg in history) { + string role = msg.Role == Role.Assistant ? "assistant" : "user"; + string content = ExtractTextFromContent(msg.Content); + result.Add(new SerializedChatMessage { Role = role, Content = content }); + } + + return result; + } + + /// + /// Deserialize portable format back to Anthropic message list. + /// Returns (systemPrompt, messages). + /// + public static (string systemPrompt, List messages) DeserializeProviderHistory(List serialized) { + string systemPrompt = null; + var messages = new List(); + if (serialized == null) return (systemPrompt, messages); + + foreach (var msg in serialized) { + switch (msg.Role?.ToLowerInvariant()) { + case "system": + systemPrompt = msg.Content ?? ""; + break; + case "assistant": + messages.Add(new MessageParam { + Role = Role.Assistant, + Content = msg.Content ?? "" + }); + break; + case "user": + default: + messages.Add(new MessageParam { + Role = Role.User, + Content = msg.Content ?? "" + }); + break; + } + } + + messages = EnsureAlternatingRoles(messages); + return (systemPrompt, messages); + } + + #region Embeddings + + public Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) { + throw new NotSupportedException($"{ServiceName}: Anthropic does not natively support embeddings. Use a different provider for embedding generation."); + } + + #endregion + + #region 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 AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) { + if (string.IsNullOrWhiteSpace(modelName)) { + modelName = "claude-sonnet-4-20250514"; + } + + prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; + + 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); + return $"Error: {ServiceName} channel/apikey is not configured."; + } + + using var client = CreateClient(channel, binding); + + try { + using var fileStream = File.OpenRead(photoPath); + var bitmap = SKBitmap.Decode(fileStream); + var imgData = bitmap.Encode(SKEncodedImageFormat.Png, 99); + var imgArray = imgData.ToArray(); + var base64Image = Convert.ToBase64String(imgArray); + + var imageSource = new Base64ImageSource { + Data = base64Image, + MediaType = MediaType.ImagePng, + }; + + var contentBlocks = new List { + new ImageBlockParam(imageSource), + new TextBlockParam(prompt), + }; + + var messages = new List { + new MessageParam { + Role = Role.User, + Content = contentBlocks + } + }; + + var parameters = new MessageCreateParams { + Model = modelName, + MaxTokens = 4096, + Messages = messages, + }; + + var responseBuilder = new StringBuilder(); + await foreach (var rawEvent in client.Messages.CreateStreaming(parameters)) { + if (rawEvent.TryPickContentBlockDelta(out var deltaEvent)) { + if (deltaEvent.Delta.TryPickText(out var textDelta)) { + responseBuilder.Append(textDelta.Text); + } + } + } + + return responseBuilder.ToString(); + } catch (Exception ex) { + _logger.LogError(ex, "Error analyzing image with Anthropic"); + return $"Error analyzing image: {ex.Message}"; + } + } + + #endregion + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/GeminiModelApi.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/GeminiModelApi.cs new file mode 100644 index 00000000..e0650b64 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/GeminiModelApi.cs @@ -0,0 +1,295 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using GenerativeAI; +using GenerativeAI.Types; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using SkiaSharp; +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM { + [Injectable(ServiceLifetime.Transient)] + 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; + + public GeminiModelApi( + DataDbContext context, + ILogger logger, + IHttpClientFactory httpClientFactory) { + _logger = logger; + _dbContext = context; + _httpClientFactory = httpClientFactory; + _logger.LogInformation("GeminiModelApi instance created"); + } + + + 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"); + } + + var role = fromUserId == Env.BotId ? Roles.Model : Roles.User; + + if (images != null && images.Count > 0 && role == Roles.User) { + var parts = new List(); + if (!string.IsNullOrWhiteSpace(content)) { + parts.Add(new Part { Text = content.Trim() }); + } + foreach (var imageBytes in images) { + parts.Add(new Part { + InlineData = new GenerativeAI.Types.Blob { + MimeType = "image/png", + Data = Convert.ToBase64String(imageBytes) + } + }); + } + chatHistory.Add(new Content { Parts = parts, Role = role }); + } else { + chatHistory.Add(new Content(content?.Trim() ?? "", role)); + } + } + + + /// + /// 尝试加载消息关联的图片文件,转换为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; + } + } + + public virtual async Task> GetAllModels(LLMChannel channel) { + if (channel.Provider.Equals(LLMProvider.Ollama)) { + return new List(); + } + + try { + var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); + var modelsResponse = await googleAI.ListModelsAsync(); + return modelsResponse.Models.Select(m => m.Name.Replace("models/", "")); + } catch (Exception ex) { + _logger.LogError(ex, "Failed to list Gemini models"); + return new List(); + } + } + + /// + /// 获取Gemini模型及其能力信息 + /// + public virtual async Task> GetAllModelsWithCapabilities(LLMChannel channel) { + if (channel.Provider.Equals(LLMProvider.Ollama)) { + return new List(); + } + + try { + var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); + var modelsResponse = await googleAI.ListModelsAsync(); + var results = new List(); + + foreach (var model in modelsResponse.Models) { + var modelName = model.Name.Replace("models/", ""); + var modelWithCaps = InferGeminiModelCapabilities(modelName, model); + results.Add(modelWithCaps); + } + + _logger.LogInformation("Retrieved {Count} Gemini models with capabilities", results.Count); + return results; + } catch (Exception ex) { + _logger.LogError(ex, "Failed to list Gemini models with capabilities"); + return new List(); + } + } + + /// + /// 根据Gemini模型名称和信息推断能力 + /// + private ModelWithCapabilities InferGeminiModelCapabilities(string modelName, GenerativeAI.Types.Model modelInfo) { + var model = new ModelWithCapabilities { ModelName = modelName }; + var lowerName = modelName.ToLower(); + + // 基本能力设置 + model.SetCapability("streaming", true); // Gemini API支持流式响应 + + // 从Gemini API模型信息中获取支持的方法 + if (modelInfo.SupportedGenerationMethods != null) { + foreach (var method in modelInfo.SupportedGenerationMethods) { + if (method.ToLower().Contains("generatecontent")) { + model.SetCapability("chat", true); + } else if (method.ToLower().Contains("embed")) { + model.SetCapability("embedding", true); + } + } + } + + // 基于模型名称推断能力 + if (lowerName.Contains("gemini")) { + // Gemini模型系列能力 + model.SetCapability("function_calling", true); + model.SetCapability("tool_calls", true); + model.SetCapability("response_json_object", true); + + // Gemini 2.0和Pro模型支持更多功能 + if (lowerName.Contains("2.0") || lowerName.Contains("pro")) { + model.SetCapability("vision", true); + model.SetCapability("multimodal", true); + model.SetCapability("image_content", true); + model.SetCapability("audio_content", true); + model.SetCapability("video_content", true); + model.SetCapability("file_upload", true); + } + // Gemini 1.5系列 + else if (lowerName.Contains("1.5")) { + model.SetCapability("vision", true); + model.SetCapability("multimodal", true); + model.SetCapability("image_content", true); + + if (lowerName.Contains("pro")) { + model.SetCapability("long_context", true); + model.SetCapability("file_upload", true); + model.SetCapability("audio_content", true); + } + } + // Flash模型 - 更快的响应 + if (lowerName.Contains("flash")) { + model.SetCapability("fast_response", true); + model.SetCapability("optimized", true); + } + + // Pro模型 - 更强的能力 + if (lowerName.Contains("pro")) { + model.SetCapability("advanced_reasoning", true); + model.SetCapability("complex_tasks", true); + } + } + + // 嵌入模型检测 + if (lowerName.Contains("embedding") || lowerName.Contains("embed")) { + model.SetCapability("embedding", true); + model.SetCapability("text_embedding", true); + model.SetCapability("function_calling", false); + model.SetCapability("vision", false); + model.SetCapability("chat", false); + } + + // 文本生成模型 + if (lowerName.Contains("text") && !lowerName.Contains("embedding")) { + model.SetCapability("text_generation", true); + model.SetCapability("chat", true); + } + + // 从模型信息中提取输入/输出token限制 + if (modelInfo.InputTokenLimit > 0) { + model.SetCapability("input_token_limit", modelInfo.InputTokenLimit.ToString()); + } + + if (modelInfo.OutputTokenLimit > 0) { + model.SetCapability("output_token_limit", modelInfo.OutputTokenLimit.ToString()); + } + + // 设置模型版本信息 + model.SetCapability("model_version", modelInfo.Version ?? "unknown"); + model.SetCapability("model_family", "Gemini"); + + // 基于模型名称的特殊能力 + if (lowerName.Contains("code")) { + model.SetCapability("code_generation", true); + model.SetCapability("code_completion", true); + } + + return model; + } + + + + + /// + 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); + throw new ArgumentException("Channel or ApiKey is not configured"); + } + + try { + var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); + var embeddings = googleAI.CreateEmbeddingModel("models/embedding-001"); + var response = await embeddings.EmbedContentAsync(text); +#pragma warning disable CS8602 // 解引用可能出现空引用。 + return response.Embedding.Values.Select(v => ( float ) v).ToArray(); +#pragma warning restore CS8602 // 解引用可能出现空引用。 + } catch (Exception ex) { + _logger.LogError(ex, "Failed to generate embeddings"); + throw; + } + } + + public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null) { + if (string.IsNullOrWhiteSpace(modelName)) { + modelName = "gpt-4-vision-preview"; + } + + prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; + + if (channel == null || string.IsNullOrWhiteSpace(channel.Gateway) || string.IsNullOrWhiteSpace(channel.ApiKey)) { + _logger.LogError("{ServiceName}: Channel, Gateway or ApiKey is not configured.", ServiceName); + return $"Error: {ServiceName} channel/gateway/apikey is not configured."; + } + + var googleAI = new GoogleAi(channel.ApiKey, client: _httpClientFactory.CreateClient()); + var model = googleAI.CreateGenerativeModel("models/" + modelName); + try { + var chat = model.StartChat(); + + + var request = new GenerateContentRequest(); + request.AddText(prompt); + // Attach a local file + request.AddInlineFile(photoPath); + // Generate the content with attached files + var response = await chat.GenerateContentAsync(request); + return response.Text; + } catch (Exception ex) { + _logger.LogError(ex, "Error analyzing image with OpenAI"); + return $"Error analyzing image: {ex.Message}"; + } + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OllamaModelApi.cs similarity index 56% rename from TelegramSearchBot.LLM/Service/AI/LLM/OllamaService.cs rename to TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OllamaModelApi.cs index 2e0f46fe..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 ILLMService +using TelegramSearchBot.Interface.AI.LLM; using TelegramSearchBot.Model; using TelegramSearchBot.Model.AI; using TelegramSearchBot.Model.Data; @@ -27,57 +27,26 @@ namespace TelegramSearchBot.Service.AI.LLM { // Standalone implementation, not using BaseLlmService [Injectable(ServiceLifetime.Transient)] - public class OllamaService : IService, ILLMService { - 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 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) { + IHttpClientFactory httpClientFactory) { _logger = logger; _dbContext = context; _serviceProvider = serviceProvider; _httpClientFactory = httpClientFactory; - _botIdentityProvider = botIdentityProvider; - _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 --- @@ -115,162 +84,10 @@ 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, - [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; - - // --- History and Prompt Setup --- - // NOTE: History context is limited as OllamaSharp.Chat manages it. - var botName = await GetBotNameAsync(); - var systemPrompt = McpToolHelper.FormatSystemPrompt(botName, ChatId); - - var chat = new OllamaSharp.Chat(ollama, systemPrompt); - - // Track history explicitly for snapshot serialization - 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 - - for (int cycle = 0; cycle < maxToolCycles; cycle++) { - if (cancellationToken.IsCancellationRequested) throw new TaskCanceledException(); - - bool receivedAnyToken = false; - - trackedHistory.Add(new SerializedChatMessage { Role = "user", Content = nextMessageToSend }); - - _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); - - if (receivedAnyToken) { - trackedHistory.Add(new SerializedChatMessage { Role = "assistant", Content = llmFullResponseText }); - } - - 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); - } - - // --- 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; - } - } - - _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 - } - } + - // 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/ProviderApi/OpenAiModelApi.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OpenAiModelApi.cs new file mode 100644 index 00000000..7f58333b --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/OpenAiModelApi.cs @@ -0,0 +1,1144 @@ +using System; +using System.ClientModel; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; // Added for IHttpClientFactory +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; // For CancellationToken +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using OpenAI; +using OpenAI.Chat; +using OpenAI.Embeddings; +using SkiaSharp; // Added for image processing +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; +using TelegramSearchBot.Model.Tools; // For BraveSearchResult +// Using alias for the common internal ChatMessage format +using CommonChat = OpenAI.Chat; + +namespace TelegramSearchBot.Service.AI.LLM { + // Standalone implementation, not inheriting from BaseLlmService + [Injectable(ServiceLifetime.Transient)] + public class OpenAiModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + /// + /// Mutable accumulator for streaming tool call updates. + /// + private class ToolCallAccumulator { + public string Id { get; set; } + public string Name { get; set; } + public StringBuilder Arguments { get; } = new StringBuilder(); + } + + internal static string NormalizeToolCallId(string toolCallId) { + return string.IsNullOrWhiteSpace(toolCallId) + ? $"call_{Guid.NewGuid():N}" + : toolCallId.Trim(); + } + + internal static string NormalizeToolCallName(string toolCallName) { + return string.IsNullOrWhiteSpace(toolCallName) + ? "unknown" + : toolCallName.Trim(); + } + + internal static string NormalizeToolCallArguments(string argumentsJson) { + return string.IsNullOrWhiteSpace(argumentsJson) ? "{}" : argumentsJson; + } + + internal static Dictionary DeserializeToolArgumentsForDisplay(string argumentsJson) { + try { + var normalized = NormalizeToolCallArguments(argumentsJson); + var values = JsonConvert.DeserializeObject>(normalized); + return values?.ToDictionary( + kvp => kvp.Key, + kvp => kvp.Value?.ToString() ?? string.Empty) + ?? new Dictionary(); + } catch { + return new Dictionary(); + } + } + + internal static bool IsMiniMaxCompatibleEndpoint(LLMChannel channel, string modelName) { + if (channel?.Provider == LLMProvider.MiniMax) { + return true; + } + + var gateway = channel?.Gateway ?? string.Empty; + var model = modelName ?? string.Empty; + return gateway.Contains("minimax", StringComparison.OrdinalIgnoreCase) || + model.Contains("minimax", StringComparison.OrdinalIgnoreCase); + } + + internal static string NormalizeOpenAIEndpoint(LLMChannel channel, string endpoint = null) { + // endpoint 为 binding 解析后的地址(无 binding 时回退 channel.Gateway,blueprint §六.1) + var gateway = endpoint ?? channel?.Gateway ?? string.Empty; + if (channel?.Provider != LLMProvider.MiniMax) { + return gateway; + } + + gateway = gateway.TrimEnd('/'); + return !string.IsNullOrEmpty(gateway) && + !gateway.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) + ? $"{gateway}/v1" + : gateway; + } + + private static string SanitizeAndTruncateArguments(string arguments, int maxChars = 2048) { + if (string.IsNullOrWhiteSpace(arguments)) { + return string.Empty; + } + + var sanitized = arguments; + var sensitiveKeys = new[] { "api_key", "apikey", "apiKey", "token", "password", "secret", "authorization" }; + foreach (var key in sensitiveKeys) { + sanitized = Regex.Replace( + sanitized, + $"(\"{Regex.Escape(key)}\"\\s*:\\s*\")[^\"]*(\")", + "$1***$2", + RegexOptions.IgnoreCase); + } + + sanitized = sanitized.Replace("\r", "\\r").Replace("\n", "\\n"); + return sanitized.Length <= maxChars + ? sanitized + : sanitized.Substring(0, maxChars) + $"..."; + } + + private readonly ILogger _logger; + private readonly DataDbContext _dbContext; + private readonly IHttpClientFactory _httpClientFactory; + private readonly IMessageExtensionService _messageExtensionService; + + private const string ServiceName = "OpenAiModelApi"; + + public OpenAiModelApi( + DataDbContext context, + ILogger logger, + IMessageExtensionService messageExtensionService, + IHttpClientFactory httpClientFactory) { + _logger = logger; + _dbContext = context; + _messageExtensionService = messageExtensionService; + _httpClientFactory = httpClientFactory; + _logger.LogInformation("OpenAiModelApi instance created. McpToolHelper should be initialized at application startup."); + } + + + + + private static List GetStablePrefixMessages(List providerHistory, bool excludeDynamicTail) { + if (!excludeDynamicTail || providerHistory.Count <= 1) { + return providerHistory.ToList(); + } + + return providerHistory.Take(providerHistory.Count - 1).ToList(); + } + + /// Compact, deterministic history serialization for prompt-cache key hashing. + internal 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; + } + + internal static (string toolDefinitionHash, string stablePrefixHash, string promptCacheKey) BuildPromptCachingContext( + string providerName, + string modelName, + string mode, + List providerHistory, + bool excludeDynamicTail) { + var toolDefinitionHash = PromptCachingHelper.ComputeToolDefinitionHash(); + var stablePrefixHash = PromptCachingHelper.ComputeStablePrefixHash(new { + Mode = mode, + StableHistory = SerializeProviderHistory(GetStablePrefixMessages(providerHistory, excludeDynamicTail)), + }); + var promptCacheKey = PromptCachingHelper.BuildOpenAiPromptCacheKey(providerName, modelName, toolDefinitionHash, stablePrefixHash); + return (toolDefinitionHash, stablePrefixHash, promptCacheKey); + } + + public virtual async Task> GetAllModels(LLMChannel channel) { + if (channel.Provider.Equals(LLMProvider.Ollama)) { + return new List(); + } + + if (channel.Provider == LLMProvider.MiniMax) { + var models = await GetGenericOpenAICompatibleModels(channel); + return models.Any() ? models : _miniMaxModels; + } + + // 检查是否为OpenRouter + if (IsOpenRouter(channel.Gateway)) { + return await GetOpenRouterModels(channel); + } + + // 首先尝试使用通用 HTTP 方式获取模型列表(兼容 MiniMax 等非标准 OpenAI 兼容 API) + var genericModels = await GetGenericOpenAICompatibleModels(channel); + if (genericModels.Any()) { + return genericModels; + } + + // 回退到 OpenAI SDK + try { + var handler = new HttpClientHandler { + Proxy = WebRequest.DefaultWebProxy, + UseProxy = true + }; + + using var httpClient = new HttpClient(handler); + OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); + + // --- Client Setup --- + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(NormalizeOpenAIEndpoint(channel)), + 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 from s in models.Value + select s.Id; + } catch (Exception ex) { + _logger.LogError(ex, "使用 OpenAI SDK 获取模型列表失败 (Gateway: {Gateway})", channel.Gateway); + return new List(); + } + } + + public virtual async Task> GetAllModels(LLMChannel channel, LLMApiBinding binding) { + if (channel == null) return new List(); + if (binding == null) return await GetAllModels(channel); + + // binding 路由:确定性 endpoint,不做品牌/URL 猜测(blueprint §六.7) + var genericModels = await GetGenericOpenAICompatibleModels(channel, binding); + if (genericModels.Any()) { + return genericModels; + } + + try { + var handler = new HttpClientHandler { + Proxy = WebRequest.DefaultWebProxy, + UseProxy = true + }; + + using var httpClient = new HttpClient(handler); + + // --- Client Setup --- + 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 from s in models.Value + select s.Id; + } catch (Exception ex) { + _logger.LogError(ex, "使用 OpenAI SDK 获取模型列表失败 (Gateway: {Gateway})", LlmBindingSupport.ResolveEndpoint(channel, binding)); + return new List(); + } + } + + public async Task IsHealthyAsync(LLMChannel channel, LLMApiBinding binding) { + var models = await GetAllModels(channel, binding); + return models.Any(); + } + + /// + /// 使用通用 HTTP GET 方式获取 OpenAI 兼容 API 的模型列表,兼容 MiniMax、DeepSeek 等提供商 + /// + private async Task> GetGenericOpenAICompatibleModels(LLMChannel channel, LLMApiBinding? binding = null) { + try { + var httpClient = _httpClientFactory.CreateClient(); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + if (!string.IsNullOrEmpty(apiKey)) { + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); + } + OpencodeSessionHeaders.Apply(httpClient, channel, binding); + + // 构建模型列表 URL,确保路径正确 + var gatewayBase = NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)).TrimEnd('/'); + var modelsUrl = gatewayBase.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) + ? $"{gatewayBase}/models" + : $"{gatewayBase}/v1/models"; + + var response = await httpClient.GetAsync(modelsUrl); + if (!response.IsSuccessStatusCode && channel.Provider != LLMProvider.MiniMax) { + // 尝试不带 /v1 的路径 + var altUrl = $"{gatewayBase}/models"; + if (altUrl != modelsUrl) { + response = await httpClient.GetAsync(altUrl); + } + } + + if (response.IsSuccessStatusCode) { + var content = await response.Content.ReadAsStringAsync(); + var modelsData = JsonConvert.DeserializeObject(content); + + var models = new List(); + if (modelsData?.data != null) { + foreach (var model in modelsData.data) { + string modelId = model.id?.ToString(); + if (!string.IsNullOrEmpty(modelId)) { + models.Add(modelId); + } + } + } + + if (models.Any()) { + _logger.LogInformation("通用 HTTP 方式获取到 {Count} 个模型 (Gateway: {Gateway})", models.Count, channel.Gateway); + } + return models; + } else { + _logger.LogDebug("通用 HTTP 方式获取模型列表失败: {StatusCode} (Gateway: {Gateway})", response.StatusCode, channel.Gateway); + return new List(); + } + } catch (Exception ex) { + _logger.LogDebug(ex, "通用 HTTP 方式获取模型列表出错 (Gateway: {Gateway})", channel.Gateway); + return new List(); + } + } + + /// + /// 检查是否为OpenRouter服务 + /// + private bool IsOpenRouter(string gateway) { + return !string.IsNullOrEmpty(gateway) && + ( gateway.Contains("openrouter.ai") || gateway.Contains("openrouter") ); + } + + /// + /// MiniMax OpenAI-compatible text model fallback snapshot. + /// + private static readonly string[] _miniMaxModels = { + "MiniMax-M3", + "MiniMax-M2.7", + "MiniMax-M2.7-highspeed", + "MiniMax-M2.5", + "MiniMax-M2.5-highspeed", + "MiniMax-M2.1", + "MiniMax-M2.1-highspeed", + "MiniMax-M2" + }; + + /// + /// 获取OpenRouter模型列表 + /// + private async Task> GetOpenRouterModels(LLMChannel channel) { + try { + var httpClient = _httpClientFactory.CreateClient(); + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); + + var response = await httpClient.GetAsync("https://openrouter.ai/api/v1/models"); + if (response.IsSuccessStatusCode) { + var content = await response.Content.ReadAsStringAsync(); + var modelsData = JsonConvert.DeserializeObject(content); + + var models = new List(); + if (modelsData?.data != null) { + foreach (var model in modelsData.data) { + string modelId = model.id?.ToString(); + if (!string.IsNullOrEmpty(modelId)) { + models.Add(modelId); + } + } + } + + _logger.LogInformation("获取到 {Count} 个OpenRouter模型", models.Count); + return models; + } else { + _logger.LogWarning("获取OpenRouter模型失败: {StatusCode}", response.StatusCode); + return new List(); + } + } catch (Exception ex) { + _logger.LogError(ex, "获取OpenRouter模型时出错"); + return new List(); + } + } + + /// + /// 获取OpenAI模型及其能力信息 + /// + public virtual async Task> GetAllModelsWithCapabilities(LLMChannel channel) { + if (channel.Provider.Equals(LLMProvider.Ollama)) { + return new List(); + } + + if (channel.Provider == LLMProvider.MiniMax) { + var models = await GetAllModels(channel); + return models.Select(InferOpenAIModelCapabilities); + } + + // 检查是否为OpenRouter + if (IsOpenRouter(channel.Gateway)) { + return await GetOpenRouterModelsWithCapabilities(channel); + } + + // 检查是否为LMStudio(通过capabilities字段检测) + if (channel.Provider == LLMProvider.LMStudio) { + return await GetLMStudioModelsWithCapabilities(channel); + } + + + using var httpClient = _httpClientFactory.CreateClient(); + + try { + // 尝试使用OpenAI内部API获取模型能力信息 + var internalApiUrl = channel.Gateway.TrimEnd('/') + "/dashboard/onboarding/models"; + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); + OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); + + var response = await httpClient.GetAsync(internalApiUrl); + if (response.IsSuccessStatusCode) { + var content = await response.Content.ReadAsStringAsync(); + var modelsWithCapabilities = ParseOpenAIModelsWithCapabilities(content); + if (modelsWithCapabilities.Any()) { + _logger.LogInformation("Successfully retrieved {Count} OpenAI models with capabilities from internal API", modelsWithCapabilities.Count()); + return modelsWithCapabilities; + } + } + + _logger.LogInformation("Internal API failed, falling back to standard models API with hardcoded capabilities"); + + // 如果内部API失败,使用标准API并根据模型名称推断能力 + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(NormalizeOpenAIEndpoint(channel)), + 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(m => InferOpenAIModelCapabilities(m.Id)); + } catch (Exception ex) { + _logger.LogError(ex, "Error getting OpenAI models with capabilities"); + return new List(); + } + } + + /// + /// 解析OpenAI内部API返回的模型能力信息 + /// + private IEnumerable ParseOpenAIModelsWithCapabilities(string jsonContent) { + try { + var modelsData = JsonConvert.DeserializeObject(jsonContent); + var results = new List(); + + if (modelsData?.data != null) { + foreach (var modelData in modelsData.data) { + var modelWithCaps = new ModelWithCapabilities { + ModelName = modelData.id?.ToString() ?? "" + }; + + // 解析features数组 + if (modelData.features != null) { + foreach (var feature in modelData.features) { + string featureName = feature?.ToString() ?? ""; + modelWithCaps.SetCapability(featureName, true); + } + } + + // 解析其他能力字段 + if (modelData.capabilities != null) { + foreach (var capability in modelData.capabilities) { + string capName = capability.Name?.ToString() ?? ""; + string capValue = capability.Value?.ToString() ?? ""; + modelWithCaps.SetCapability(capName, capValue); + } + } + + results.Add(modelWithCaps); + } + } + + return results; + } catch (Exception ex) { + _logger.LogError(ex, "Error parsing OpenAI models capabilities JSON"); + return new List(); + } + } + + /// + /// 根据OpenAI模型名称推断能力 + /// + private ModelWithCapabilities InferOpenAIModelCapabilities(string modelName) { + var model = new ModelWithCapabilities { ModelName = modelName }; + + // 基于模型名称的能力推断 + var lowerName = modelName.ToLower(); + + // 嵌入模型 + if (lowerName.Contains("embedding") || lowerName.Contains("ada")) { + model.SetCapability("embedding", true); + model.SetCapability("function_calling", false); + model.SetCapability("vision", false); + } + // GPT-4系列模型 + else if (lowerName.StartsWith("gpt-4")) { + model.SetCapability("function_calling", true); + model.SetCapability("streaming", true); + model.SetCapability("response_json_object", true); + + // GPT-4 Vision模型 + if (lowerName.Contains("vision") || lowerName.Contains("4o") || lowerName.Contains("4-turbo")) { + model.SetCapability("vision", true); + model.SetCapability("image_content", true); + model.SetCapability("multimodal", true); + } + + // 较新的模型支持并行工具调用 + if (lowerName.Contains("4o") || lowerName.Contains("4-turbo") || lowerName.Contains("1106") || lowerName.Contains("0125")) { + model.SetCapability("parallel_tool_calls", true); + model.SetCapability("response_json_schema", true); + } + } + // GPT-3.5系列模型 + else if (lowerName.StartsWith("gpt-3.5")) { + model.SetCapability("function_calling", true); + model.SetCapability("streaming", true); + + if (lowerName.Contains("1106") || lowerName.Contains("0125")) { + model.SetCapability("response_json_object", true); + } + } + // 图片生成模型 + else if (ModelWithCapabilities.IsKnownImageGenerationModelName(modelName)) { + model.SetCapability("image_generation", true); + model.SetCapability("text_to_image", true); + model.SetCapability("function_calling", false); + } + // Whisper模型 + else if (lowerName.Contains("whisper")) { + model.SetCapability("audio_transcription", true); + model.SetCapability("function_calling", false); + } + // TTS模型 + else if (lowerName.Contains("tts")) { + model.SetCapability("text_to_speech", true); + model.SetCapability("function_calling", false); + } + + return model; + } + + /// + /// 获取OpenRouter模型及其能力信息 + /// + private async Task> GetOpenRouterModelsWithCapabilities(LLMChannel channel) { + try { + var httpClient = _httpClientFactory.CreateClient(); + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); + + var response = await httpClient.GetAsync("https://openrouter.ai/api/v1/models"); + if (response.IsSuccessStatusCode) { + var content = await response.Content.ReadAsStringAsync(); + var modelsData = JsonConvert.DeserializeObject(content); + + var results = new List(); + if (modelsData?.data != null) { + foreach (var modelData in modelsData.data) { + var modelWithCaps = ParseOpenRouterModelCapabilities(modelData); + if (modelWithCaps != null) { + results.Add(modelWithCaps); + } + } + } + + _logger.LogInformation("获取到 {Count} 个OpenRouter模型及其能力信息", results.Count); + return results; + } else { + _logger.LogWarning("获取OpenRouter模型能力失败: {StatusCode}", response.StatusCode); + return new List(); + } + } catch (Exception ex) { + _logger.LogError(ex, "获取OpenRouter模型能力时出错"); + return new List(); + } + } + + /// + /// 解析OpenRouter模型能力信息 + /// + private ModelWithCapabilities ParseOpenRouterModelCapabilities(dynamic modelData) { + try { + string modelId = modelData.id?.ToString(); + if (string.IsNullOrEmpty(modelId)) { + return null; + } + + var model = new ModelWithCapabilities { ModelName = modelId }; + + // 基本信息 + if (modelData.name != null) { + model.SetCapability("display_name", modelData.name.ToString()); + } + + if (modelData.description != null) { + model.SetCapability("description", modelData.description.ToString()); + } + + if (modelData.context_length != null) { + model.SetCapability("context_length", modelData.context_length.ToString()); + } + + // 架构信息 + if (modelData.architecture != null) { + var architecture = modelData.architecture; + + // 输入模态 + if (architecture.input_modalities != null) { + bool supportsText = false; + bool supportsImage = false; + + foreach (var modality in architecture.input_modalities) { + string modalityStr = modality.ToString().ToLower(); + if (modalityStr == "text") { + supportsText = true; + } else if (modalityStr == "image") { + supportsImage = true; + } + } + + model.SetCapability("text_input", supportsText); + model.SetCapability("vision", supportsImage); + model.SetCapability("image_content", supportsImage); + model.SetCapability("multimodal", supportsImage); + } + + // 输出模态 + if (architecture.output_modalities != null) { + foreach (var modality in architecture.output_modalities) { + string modalityStr = modality.ToString().ToLower(); + if (modalityStr == "text") { + model.SetCapability("text_output", true); + } + } + } + + if (architecture.tokenizer != null) { + model.SetCapability("tokenizer", architecture.tokenizer.ToString()); + } + } + + // 定价信息 + if (modelData.pricing != null) { + var pricing = modelData.pricing; + if (pricing.prompt != null) { + model.SetCapability("prompt_price", pricing.prompt.ToString()); + } + if (pricing.completion != null) { + model.SetCapability("completion_price", pricing.completion.ToString()); + } + if (pricing.image != null) { + model.SetCapability("image_price", pricing.image.ToString()); + } + } + + // 支持的参数 + if (modelData.supported_parameters != null) { + var supportedParams = new List(); + foreach (var param in modelData.supported_parameters) { + string paramStr = param.ToString(); + supportedParams.Add(paramStr); + + // 检查工具调用支持 + if (paramStr.ToLower().Contains("tool") || paramStr.ToLower().Contains("function")) { + model.SetCapability("function_calling", true); + model.SetCapability("tool_calls", true); + } + + // 检查流式支持 + if (paramStr.ToLower().Contains("stream")) { + model.SetCapability("streaming", true); + } + + // 检查JSON格式支持 + if (paramStr.ToLower().Contains("response_format")) { + model.SetCapability("response_json_object", true); + } + } + + model.SetCapability("supported_parameters", string.Join(", ", supportedParams)); + } + + // 基于模型名称的额外推断 + var lowerModelId = modelId.ToLower(); + + // 推断提供商 + if (lowerModelId.Contains("openai/") || lowerModelId.Contains("gpt")) { + model.SetCapability("provider", "OpenAI"); + } else if (lowerModelId.Contains("anthropic/") || lowerModelId.Contains("claude")) { + model.SetCapability("provider", "Anthropic"); + } else if (lowerModelId.Contains("google/") || lowerModelId.Contains("gemini")) { + model.SetCapability("provider", "Google"); + } else if (lowerModelId.Contains("meta/") || lowerModelId.Contains("llama")) { + model.SetCapability("provider", "Meta"); + } else if (lowerModelId.Contains("mistral/")) { + model.SetCapability("provider", "Mistral"); + } + + // 默认能力设置 + model.SetCapability("chat", true); + + // 如果没有明确的工具调用信息,基于模型名称推断 + if (!model.GetCapabilityBool("function_calling")) { + if (lowerModelId.Contains("gpt-4") || lowerModelId.Contains("gpt-3.5") || + lowerModelId.Contains("claude") || lowerModelId.Contains("gemini")) { + model.SetCapability("function_calling", true); + model.SetCapability("tool_calls", true); + } + } + + return model; + } catch (Exception ex) { + string modelDataStr = modelData?.ToString() ?? "null"; + _logger.LogError(ex, "解析OpenRouter模型能力时出错: {ModelData}", modelDataStr); + return null; + } + } + + // --- LMStudio Capability Detection --- + + /// + /// 获取LMStudio模型及其能力信息 + /// LMStudio在/v1/models返回的模型对象中包含capabilities数组 + /// + private async Task> GetLMStudioModelsWithCapabilities(LLMChannel channel) { + try { + var httpClient = _httpClientFactory.CreateClient(); + if (!string.IsNullOrEmpty(channel.ApiKey)) { + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); + } + + var gatewayBase = channel.Gateway.TrimEnd('/'); + var modelsUrl = gatewayBase.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) + ? $"{gatewayBase}/models" + : $"{gatewayBase}/v1/models"; + + var response = await httpClient.GetAsync(modelsUrl); + if (response.IsSuccessStatusCode) { + var content = await response.Content.ReadAsStringAsync(); + var modelsData = JsonConvert.DeserializeObject(content); + + var results = new List(); + if (modelsData?.data != null) { + foreach (var modelData in modelsData.data) { + var modelWithCaps = ParseLMStudioModelCapabilities(modelData); + if (modelWithCaps != null) { + results.Add(modelWithCaps); + } + } + } + + _logger.LogInformation("获取到 {Count} 个LMStudio模型及其能力信息", results.Count); + return results; + } else { + _logger.LogWarning("获取LMStudio模型失败: {StatusCode}", response.StatusCode); + // 回退到通用方式 + var genericModels = await GetGenericOpenAICompatibleModels(channel); + return genericModels.Select(m => InferOpenAIModelCapabilities(m)); + } + } catch (Exception ex) { + _logger.LogError(ex, "获取LMStudio模型能力时出错"); + return new List(); + } + } + + /// + /// 解析LMStudio模型能力信息 + /// LMStudio模型包含capabilities数组,如["tool_use", "vision", "chat", "text-completion"] + /// + private ModelWithCapabilities ParseLMStudioModelCapabilities(dynamic modelData) { + try { + string modelId = modelData.id?.ToString(); + if (string.IsNullOrEmpty(modelId)) { + return null; + } + + var model = new ModelWithCapabilities { ModelName = modelId }; + + // 基本能力 + model.SetCapability("streaming", true); + model.SetCapability("chat", true); + + // 解析LMStudio的capabilities数组 + if (modelData.capabilities != null) { + foreach (var capability in modelData.capabilities) { + string capStr = capability.ToString().ToLower(); + switch (capStr) { + case "tool_use": + model.SetCapability("function_calling", true); + model.SetCapability("tool_calls", true); + break; + case "vision": + model.SetCapability("vision", true); + model.SetCapability("multimodal", true); + model.SetCapability("image_content", true); + break; + case "embeddings": + model.SetCapability("embedding", true); + model.SetCapability("text_embedding", true); + break; + case "chat": + model.SetCapability("chat", true); + break; + case "text-completion": + model.SetCapability("text_generation", true); + break; + } + } + } + + // 解析LMStudio扩展字段 + if (modelData.type != null) { + string modelType = modelData.type.ToString().ToLower(); + if (modelType == "vlm") { + model.SetCapability("vision", true); + model.SetCapability("multimodal", true); + } else if (modelType == "embeddings") { + model.SetCapability("embedding", true); + } + } + + if (modelData.max_context_length != null) { + model.SetCapability("input_token_limit", modelData.max_context_length.ToString()); + } + + if (modelData.arch != null) { + model.SetCapability("model_family", modelData.arch.ToString()); + } + + if (modelData.quantization != null) { + model.SetCapability("quantization", modelData.quantization.ToString()); + } + + model.SetCapability("provider", "LMStudio"); + + return model; + } catch (Exception ex) { + string modelDataStr = modelData?.ToString() ?? "null"; + _logger.LogError(ex, "解析LMStudio模型能力时出错: {ModelData}", modelDataStr); + return null; + } + } + + + + + + /// + + + + + + + /// + /// Resume LLM execution from a saved v2 (normalized) snapshot. + /// + + + /// + /// Stateless transport adapter for the OpenAI Chat Completions API: converts the + /// normalized history into SDK messages per turn and streams one assistant turn. + /// + 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 { + // Check for Reasoning property via reflection + var reasoningProp = assistantMsg.GetType().GetProperty("Reasoning"); + if (reasoningProp != null) { + var value = reasoningProp.GetValue(assistantMsg); + if (value is string reasoning && !string.IsNullOrEmpty(reasoning)) { + return reasoning; + } + } + + #pragma warning disable SCME0001 // Patch is for evaluation, may be changed in future + var patchProp = assistantMsg.GetType().GetProperty("Patch"); + if (patchProp?.GetValue(assistantMsg) is JsonPatch patch && + patch.TryGetValue("$.reasoning_content"u8, out string? reasoningFromPatch)) { + return reasoningFromPatch; + } + #pragma warning restore SCME0001 + } catch { + // Reflection failed, return null + } + return null; + } + + /// + /// Extract reasoning_content from streaming update for thinking mode models. + /// Uses Patch API first (OpenAI SDK), falls back to reflection for internal properties. + /// + 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)) { + if (reasoningFromPatch != null) { + return reasoningFromPatch; + } + } +#pragma warning restore SCME0001 + + // Fallback: reflection for SDK internal properties + try { + // Try ReasoningContentUpdate property (OpenAI SDK for thinking models) + var reasoningProp = update.GetType().GetProperty("ReasoningContentUpdate"); + if (reasoningProp != null) { + var value = reasoningProp.GetValue(update); + if (value is string reasoning && !string.IsNullOrEmpty(reasoning)) { + return reasoning; + } + } + // Fallback: try Reasoning property + var fallbackProp = update.GetType().GetProperty("Reasoning"); + if (fallbackProp != null) { + var value = fallbackProp.GetValue(update); + if (value is string fallback && !string.IsNullOrEmpty(fallback)) { + return fallback; + } + } + } catch { + // Reflection failed + } + return null; + } + + /// + /// Deserialize portable format back to OpenAI ChatMessage list. + /// + public static List DeserializeProviderHistory(List serialized, bool includeEmptyReasoningContent = false) { + var result = new List(); + if (serialized == null) return result; + + foreach (var msg in serialized) { + switch (msg.Role?.ToLowerInvariant()) { + case "system": + result.Add(new SystemChatMessage(msg.Content ?? "")); + break; + case "assistant": + var assistantMsg = new AssistantChatMessage(msg.Content ?? ""); + SetAssistantReasoningContent(assistantMsg, msg.ReasoningContent, includeEmptyReasoningContent); + result.Add(assistantMsg); + break; + case "user": + default: + result.Add(new UserChatMessage(msg.Content ?? "")); + break; + } + } + return result; + } + + /// + /// Set reasoning_content on AssistantChatMessage for thinking mode models. + /// Uses Patch.Set (OpenAI SDK v2.10.0+) with reflection fallback. + /// + internal static bool ShouldIncludeEmptyReasoningContent(LLMChannel channel, string modelName) { + var gateway = channel?.Gateway ?? string.Empty; + var model = modelName ?? string.Empty; + return gateway.Contains("deepseek", StringComparison.OrdinalIgnoreCase) || + model.Contains("deepseek", StringComparison.OrdinalIgnoreCase); + } + + internal static void SetAssistantReasoningContent(AssistantChatMessage msg, string reasoningContent, bool includeEmptyReasoningContent = false) { + if (reasoningContent is null || (reasoningContent.Length == 0 && !includeEmptyReasoningContent)) { + return; + } + + // Try Patch.Set first (writes directly to JSON output) +#pragma warning disable SCME0001 // Patch API is experimental but functional + try { + var encodedReasoningContent = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(reasoningContent); + msg.Patch.Set("$.reasoning_content"u8, encodedReasoningContent.AsSpan()); + } catch { + // Patch.Set not available or failed, fall through to reflection + } +#pragma warning restore SCME0001 + // Reflection fallback for older SDK versions + try { + var prop = msg.GetType().GetProperty("Reasoning"); + if (prop != null && prop.CanWrite) { + prop.SetValue(msg, reasoningContent); + } + } catch { + // Reflection failed, ignore + } + } + + 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, channel, binding); + + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, endpoint)), + Transport = new HttpClientPipelineTransport(httpClient), + }; + + var apikey = new ApiKeyCredential(apiKey); + OpenAIClient client = new(apikey, 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(); + _logger.LogDebug("Embedding response type: {Type}", embedding.GetType().FullName); + _logger.LogDebug("Embedding response structure: {Response}", JsonConvert.SerializeObject(embedding, Formatting.Indented)); + + // Try reflection with all possible property names + 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 - try to 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; + } + } + + + public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null) { + return await AnalyzeImageAsync(photoPath, modelName, channel, null, prompt); + } + + public async Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, LLMApiBinding binding, string prompt = null) { + if (string.IsNullOrWhiteSpace(modelName)) { + modelName = "gpt-4-vision-preview"; + } + + prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; + + 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); + return $"Error: {ServiceName} channel/gateway/apikey is not configured."; + } + + using var httpClient = _httpClientFactory.CreateClient(); + OpencodeSessionHeaders.Apply(httpClient, channel, binding); + + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding))), + Transport = new HttpClientPipelineTransport(httpClient), + }; + + var chatClient = new ChatClient(model: modelName, credential: new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); + + try { + // 读取图像并转换为Base64 + using var fileStream = File.OpenRead(photoPath); + var tg_img = SKBitmap.Decode(fileStream); + var tg_img_data = tg_img.Encode(SKEncodedImageFormat.Png, 99); + var tg_img_arr = tg_img_data.ToArray(); + var base64Image = Convert.ToBase64String(tg_img_arr); + + var messages = new List { + new UserChatMessage(new List() { + ChatMessageContentPart.CreateTextPart(prompt), + ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(tg_img_arr), "image/png"), + + }), + }; + + var responseBuilder = new StringBuilder(); + await foreach (var update in chatClient.CompleteChatStreamingAsync(messages)) { + foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { + if (updatePart?.Text != null) responseBuilder.Append(updatePart.Text); + } + } + return responseBuilder.ToString(); + } catch (Exception ex) { + _logger.LogError(ex, "Error analyzing image with OpenAI"); + return $"Error analyzing image: {ex.Message}"; + } + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/ResponsesModelApi.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/ResponsesModelApi.cs new file mode 100644 index 00000000..748d6b49 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/ResponsesModelApi.cs @@ -0,0 +1,680 @@ +#pragma warning disable OPENAI001 // OpenAI Responses API is experimental/evaluation only + +using System; +using System.ClientModel; +using System.ClientModel.Primitives; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using OpenAI; +using OpenAI.Chat; +using OpenAI.Responses; +using SkiaSharp; +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Common; +using TelegramSearchBot.Interface; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; +using TelegramSearchBot.Model.Tools; + +namespace TelegramSearchBot.Service.AI.LLM { + /// + /// OpenAI Responses API 服务实现。 + /// 使用 OpenAI 新的 /v1/responses 接口(Responses API), + /// 支持 built-in tools(web_search, file_search 等)和 Function calling。 + /// 与现有的 OpenAiModelApi(Chat Completions API)并存。 + /// + [Injectable(ServiceLifetime.Transient)] + public class ResponsesModelApi : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + private const string ServiceName = "ResponsesModelApi"; + + /// + /// Mutable accumulator for streaming tool call argument deltas. + /// + internal class ResponsesToolCallAccumulator { + public string CallId { get; set; } + public string Name { get; set; } + public StringBuilder Arguments { get; } = new StringBuilder(); + } + + private readonly ILogger _logger; + private readonly DataDbContext _dbContext; + private readonly IHttpClientFactory _httpClientFactory; + private readonly IMessageExtensionService _messageExtensionService; + + public ResponsesModelApi( + DataDbContext context, + ILogger logger, + IMessageExtensionService messageExtensionService, + IHttpClientFactory httpClientFactory) { + _logger = logger; + _dbContext = context; + _messageExtensionService = messageExtensionService; + _httpClientFactory = httpClientFactory; + _logger.LogInformation("ResponsesModelApi instance created."); + } + + + + private static List GetStablePrefixInputItems(List inputItems, bool excludeDynamicTail) { + if (!excludeDynamicTail || inputItems.Count == 0) { + return inputItems.ToList(); + } + + return inputItems.Take(inputItems.Count - 1).ToList(); + } + + internal static (string toolDefinitionHash, string stablePrefixHash, string promptCacheKey) BuildPromptCachingContext( + string providerName, + string modelName, + string mode, + string instructions, + List inputItems, + bool excludeDynamicTail) { + var toolDefinitionHash = PromptCachingHelper.ComputeToolDefinitionHash(); + var stablePrefixHash = PromptCachingHelper.ComputeStablePrefixHash(new { + Mode = mode, + Instructions = instructions, + StableHistory = SerializeInputItems(GetStablePrefixInputItems(inputItems, excludeDynamicTail)), + }); + var promptCacheKey = PromptCachingHelper.BuildOpenAiPromptCacheKey(providerName, modelName, toolDefinitionHash, stablePrefixHash); + return (toolDefinitionHash, stablePrefixHash, promptCacheKey); + } + + + // ======================================================================== + // Capability implementation + // ======================================================================== + + + + + /// + + + /// + /// Native transport for the OpenAI Responses API: converts the normalized history to + /// input items per turn and streams one assistant turn (text + function calls). + /// + + // ======================================================================== + // Helper: Vision support check + // ======================================================================== + + + 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 + // ======================================================================== + + + + // ======================================================================== + // Helper: IsSameSender + // ======================================================================== + + 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; + } + } + + // ======================================================================== + // Helper: Model capability parsing (from OpenAiModelApi) + // ======================================================================== + + + internal 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; + } + } + + + internal 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 = OpenAiModelApi.NormalizeToolCallId(rawCallId); + lastResolvedCallId = callId; + 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, + 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 + : OpenAiModelApi.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 { + var internalApiUrl = channel.Gateway.TrimEnd('/') + "/dashboard/onboarding/models"; + httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {channel.ApiKey}"); + + var response = await httpClient.GetAsync(internalApiUrl); + if (response.IsSuccessStatusCode) { + var content = await response.Content.ReadAsStringAsync(); + return ParseOpenAIModelsWithCapabilities(content); + } + + _logger.LogInformation("Internal API failed, falling back to standard models API with hardcoded capabilities"); + + 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(m => InferOpenAIModelCapabilities(m.Id)); + } catch (Exception ex) { + _logger.LogError(ex, "Error getting OpenAI models with capabilities"); + return new List(); + } + } + + // ======================================================================== + // 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"; + } + prompt = string.IsNullOrWhiteSpace(prompt) ? GeneralLLMService.DefaultAltPhotoPrompt : prompt; + + 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); + return $"Error: {ServiceName} channel/gateway/apikey is not configured."; + } + + using var httpClient = _httpClientFactory.CreateClient(); + OpencodeSessionHeaders.Apply(httpClient, channel, binding); + + // For image analysis, use Chat Completions API (vision support is more mature) + var clientOptions = new OpenAIClientOptions { + Endpoint = new Uri(LlmBindingSupport.ResolveEndpoint(channel, binding)), + Transport = new HttpClientPipelineTransport(httpClient), + }; + var chatClient = new ChatClient(model: modelName, credential: new ApiKeyCredential(LlmBindingSupport.ResolveApiKey(channel, binding)), clientOptions); + + try { + using var fileStream = File.OpenRead(photoPath); + var tg_img = SKBitmap.Decode(fileStream); + var tg_img_data = tg_img.Encode(SKEncodedImageFormat.Png, 99); + var tg_img_arr = tg_img_data.ToArray(); + + var messages = new List { + new UserChatMessage(new List { + ChatMessageContentPart.CreateTextPart(prompt), + ChatMessageContentPart.CreateImagePart(BinaryData.FromBytes(tg_img_arr), "image/png"), + }), + }; + + var responseBuilder = new StringBuilder(); + await foreach (var update in chatClient.CompleteChatStreamingAsync(messages)) { + foreach (ChatMessageContentPart updatePart in update.ContentUpdate ?? Enumerable.Empty()) { + if (updatePart?.Text != null) responseBuilder.Append(updatePart.Text); + } + } + return responseBuilder.ToString(); + } catch (Exception ex) { + _logger.LogError(ex, "Error analyzing image with OpenAI Responses Service"); + return $"Error analyzing image: {ex.Message}"; + } + } + + // ======================================================================== + + private IEnumerable ParseOpenAIModelsWithCapabilities(string jsonContent) { + try { + var modelsData = JsonConvert.DeserializeObject(jsonContent); + var results = new List(); + + if (modelsData?.data != null) { + foreach (var modelData in modelsData.data) { + var modelWithCaps = new ModelWithCapabilities { + ModelName = modelData.id?.ToString() ?? "" + }; + + if (modelData.features != null) { + foreach (var feature in modelData.features) { + string featureName = feature?.ToString() ?? ""; + modelWithCaps.SetCapability(featureName, true); + } + } + + if (modelData.capabilities != null) { + foreach (var capability in modelData.capabilities) { + string capName = capability.Name?.ToString() ?? ""; + string capValue = capability.Value?.ToString() ?? ""; + modelWithCaps.SetCapability(capName, capValue); + } + } + + results.Add(modelWithCaps); + } + } + + return results; + } catch (Exception ex) { + _logger.LogError(ex, "Error parsing OpenAI models capabilities JSON"); + return new List(); + } + } + + private ModelWithCapabilities InferOpenAIModelCapabilities(string modelName) { + var model = new ModelWithCapabilities { ModelName = modelName }; + var lowerName = modelName.ToLower(); + + if (lowerName.Contains("embedding") || lowerName.Contains("ada")) { + model.SetCapability("embedding", true); + model.SetCapability("function_calling", false); + model.SetCapability("vision", false); + } else if (lowerName.StartsWith("gpt-4")) { + model.SetCapability("function_calling", true); + model.SetCapability("streaming", true); + model.SetCapability("response_json_object", true); + + if (lowerName.Contains("vision") || lowerName.Contains("4o") || lowerName.Contains("4-turbo")) { + model.SetCapability("vision", true); + model.SetCapability("image_content", true); + model.SetCapability("multimodal", true); + } + + if (lowerName.Contains("4o") || lowerName.Contains("4-turbo") || lowerName.Contains("1106") || lowerName.Contains("0125")) { + model.SetCapability("parallel_tool_calls", true); + model.SetCapability("response_json_schema", true); + } + } else if (lowerName.StartsWith("gpt-3.5")) { + model.SetCapability("function_calling", true); + model.SetCapability("streaming", true); + if (lowerName.Contains("1106") || lowerName.Contains("0125")) { + model.SetCapability("response_json_object", true); + } + } else if (ModelWithCapabilities.IsKnownImageGenerationModelName(modelName)) { + model.SetCapability("image_generation", true); + model.SetCapability("text_to_image", true); + model.SetCapability("function_calling", false); + } else if (lowerName.Contains("whisper")) { + model.SetCapability("audio_transcription", true); + model.SetCapability("function_calling", false); + } else if (lowerName.Contains("tts")) { + model.SetCapability("text_to_speech", true); + model.SetCapability("function_calling", false); + } + + // Responses API capabilities - only for modern models that support them + if (lowerName.Contains("4o") || lowerName.Contains("o1") || lowerName.Contains("o3")) { + model.SetCapability("responses_api", true); + model.SetCapability("web_search", true); + model.SetCapability("file_search", true); + } + + return model; + } + + } +} 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..b08092fe --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AnthropicMessagesTransport.cs @@ -0,0 +1,303 @@ +#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 { + /// + /// Builds the transport from a channel/binding pair. Absorbs the client + /// construction glue formerly in AnthropicModelApi.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; + 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 = 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))); + } + _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) = AnthropicModelApi.BuildPromptCachingContext( + _nativeTools ? "anthropic-native" : "anthropic-xml", + _systemPrompt, + AnthropicModelApi.SerializeProviderHistory(_systemPrompt, rawHistory)); + var parameters = new MessageCreateParams { + Model = _modelName, + MaxTokens = 8192, + System = AnthropicModelApi.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 AnthropicModelApi.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 ? AnthropicModelApi.CreateCacheControl() : null, + }, null)); + } catch (Exception) { + } + } + return tools; + } + } +} 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..f6a392ab --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/GeminiTransport.cs @@ -0,0 +1,100 @@ +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 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; + + 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..57967210 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OllamaTransport.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +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; +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 { + /// + /// 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 OllamaModelApi. + 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; + 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 + }); + } + }} 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..de121881 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAIResponsesTransport.cs @@ -0,0 +1,236 @@ +#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 { + /// + /// 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; + 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) = ResponsesModelApi.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(); + + ResponsesModelApi.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 = OpenAiModelApi.NormalizeToolCallId(fcItem.CallId), + Name = OpenAiModelApi.NormalizeToolCallName(fcItem.FunctionName), + ArgumentsJson = OpenAiModelApi.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())); + } + } + } +} +} 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..dd3c9435 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/OpenAiChatTransport.cs @@ -0,0 +1,257 @@ +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 System.Net.Http; +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 { + /// + /// Builds the transport plus its turn config from a channel/binding pair. + /// 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 = OpenAiModelApi.NormalizeOpenAIEndpoint(channel, LlmBindingSupport.ResolveEndpoint(channel, binding)); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + 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(); + 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, + 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) = OpenAiModelApi.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 = OpenAiModelApi.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("OpenAiModelApi: Tool call at index {Index} has no ID, generating fallback.", index); + } + toolCalls.Add(new LlmToolCall { + Id = OpenAiModelApi.NormalizeToolCallId(acc.Id), + Name = OpenAiModelApi.NormalizeToolCallName(acc.Name), + ArgumentsJson = OpenAiModelApi.NormalizeToolCallArguments(acc.Arguments.ToString()) + }); + } + } else if (toolCallAccumulators.Any()) { + _logger.LogWarning( + "OpenAiModelApi: 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) } }; + } + OpenAiModelApi.SetAssistantReasoningContent(assistant, m.Thinking, _includeEmptyReasoningContent); + messages.Add(assistant); + } else { + var assistant = new AssistantChatMessage(m.Text ?? string.Empty); + OpenAiModelApi.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. + /// +} 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 eeeb21ee..343f65c6 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 LlmChatRunner _chatRunner; private readonly ILogger _logger; public LlmServiceProxy(IServiceProvider serviceProvider, ILogger logger) { _serviceProvider = serviceProvider; + _chatRunner = serviceProvider.GetRequiredService(); _logger = logger; } @@ -22,15 +24,14 @@ 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); 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; } @@ -48,7 +49,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 _chatRunner.RunAsync(history, message, task.ChatId, task.ModelName, channel, binding, executionContext, supportsVision, cancellationToken) .WithCancellation(cancellationToken)) { yield return chunk; } @@ -72,28 +77,6 @@ public async IAsyncEnumerable CallAsync( }; } - private ILLMService ResolveService(LLMProvider provider) { - return provider switch { - LLMProvider.Ollama => _serviceProvider.GetRequiredService(), - LLMProvider.Gemini => _serviceProvider.GetRequiredService(), - LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), - LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), - _ => _serviceProvider.GetRequiredService() - }; - } - - /// 按 binding 线协议解析 client(与 ILLMFactory.GetLLMService(LlmProtocol) 同构)。 - private ILLMService 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() - }; - } - private void ApplyBotIdentity(string botName, long botUserId) { var identityProvider = _serviceProvider.GetService(); if (identityProvider != null) { @@ -107,69 +90,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, diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs index d700b851..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(); - 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.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 7bfb2fc9..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(); - 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.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()); 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/Model/AI/LLMConfState.cs b/TelegramSearchBot/Model/AI/LLMConfState.cs index 5ec14ee1..1734008a 100644 --- a/TelegramSearchBot/Model/AI/LLMConfState.cs +++ b/TelegramSearchBot/Model/AI/LLMConfState.cs @@ -17,6 +17,15 @@ public enum LLMConfState { [Description("setting_music_generation_model")] SettingMusicGenerationModel, + [Description("awaiting_preset_selection")] + AwaitingPresetSelection, + + [Description("awaiting_preset_gateway")] + AwaitingPresetGateway, + + [Description("awaiting_preset_apikey")] + AwaitingPresetApiKey, + [Description("awaiting_name")] AwaitingName, diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs index cb1c7e5a..43cc2f90 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.GetCatalog(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.GetCatalog(channel.Provider); if (service == null) { _logger.LogWarning("未找到通道 {ChannelName} ({Provider}) 的LLM服务", channel.Name, channel.Provider); continue; diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs index 8566339e..16af20cb 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs @@ -50,6 +50,9 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService _stateHandlers = new Dictionary>> { { LLMConfState.AwaitingName.GetDescription(), HandleAwaitingNameAsync }, + { LLMConfState.AwaitingPresetSelection.GetDescription(), HandleAwaitingPresetSelectionAsync }, + { LLMConfState.AwaitingPresetGateway.GetDescription(), HandleAwaitingPresetGatewayAsync }, + { LLMConfState.AwaitingPresetApiKey.GetDescription(), HandleAwaitingPresetApiKeyAsync }, { LLMConfState.AwaitingGateway.GetDescription(), HandleAwaitingGatewayAsync }, { LLMConfState.AwaitingProvider.GetDescription(), HandleAwaitingProviderAsync }, { LLMConfState.AwaitingParallel.GetDescription(), HandleAwaitingParallelAsync }, @@ -91,6 +94,87 @@ 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.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, preset.DefaultGateway); + } + + await redis.SetDataAsync(preset.Id); + await redis.SetStateAsync(LLMConfState.AwaitingPresetApiKey.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 API Key(本地/免 Key 服务发送 - 跳过):"); + } + + 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(); + 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, string gateway) { + var channelId = await Helper.AddChannel( + preset.DisplayName, + gateway, + 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 +658,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); }