Repository navigation
feat(relay): hosted-tool conversion fidelity, reasoning normalization, and billing usage integrity - #7137
Conversation
- add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls
- centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent
- carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges
- add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens
- connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (149)
WalkthroughThe change centralizes reasoning conversion, adds request-phase tool-loss policies and diagnostics, introduces direct Claude and OpenAI Responses routes, adds stateful provider stream conversion, normalizes billing usage, preserves annotations and hosted-tool data, and exposes Claude token counting. ChangesRelay conversion and reasoning
Estimated code review effort: 5 (Critical) | ~120 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Resolve conflicts with upstream QuantumNous#7137: - controller/relay.go: keep the shared service.ProcessChannelError wrapper and thread relayInfo through it so conversion diagnostics land in error logs for HTTP, task, and Responses WebSocket relays. - relay/channel/openai/relay_responses.go: adopt upstream's relayconvert.NormalizeResponsesUsage + dto.MergeUsageNonZero. - Drop service.ApplyResponsesUsage; the WebSocket session now uses the same relaykit normalizer, which is extended to honour Responses output_tokens_details so reasoning/image token details are billed. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
… and port fork video features Sync QuantumNous/new-api main through 0ed497f via the resolved PR QuantumNous#6914 branch. Upstream replaced every Go video task adaptor with the sandboxed JS task plugin system and reserved channel type 61 for "Task Plugin". Fork features ported onto the plugin system: - New API Video channel: renumbered to ChannelTypeNewAPIVideo = 62 with a one-time, idempotent migration of channel rows and task platforms from the legacy 61 (guarded by an options marker so real Task Plugin channels are never touched). Reimplemented as factory plugin plugins/tasks/newapi-video: POST /v1/video/generations submit, JSON-only bodies, per-call billing (no multipliers), tolerant result-URL extraction, and channel-backed content proxying for upstreams that return their own /content endpoint. - xAI Grok Imagine video: reimplemented as factory plugin plugins/tasks/xai (channelTypes [48]) with the native generation body, OpenAI video size -> aspect_ratio/resolution translation, multipart input_reference via file placeholders, and the per-model resolution ratios. The native POST /v1/videos/generations entry collides with the host's /v1/videos/:task_id shape, so it is bound host-side through the new middleware.PinHostOwnedPluginRoute instead of meta.routes. - Removed relay/channel/task/{sora,xai}, the ClientProtocol task field, and the fork's video-proxy channel branches (now plugin artifacts). Conflict resolutions: - controller/relay.go: keep service.ProcessChannelError wrapper plus the fork's per-channel concurrency slot; adopt upstream stage/refund flow. - relay/claude_handler.go: adopt upstream ApplyReasoningModelSuffix and keep the fork's post-processing (history-aware thinking strip, clear_ thinking edit removal, sampling normalisation, developer role, tool normalisation). - relay/common/relay_info.go: upstream fields plus fork UserRatio. - router/web-router.go: upstream plugin dispatcher chain plus the fork's index injector on "/" and "/index.html". - model/*, middleware/distributor.go, service/channel_select.go, relay/channel/openai/*: upstream. - i18n locale files: union of both sides. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
merge: sync QuantumNous/main (hosted-tool relay rework QuantumNous#7135 QuantumNous#7137)
…, and billing usage integrity (QuantumNous#7137) * feat(relaykit): preserve hosted tools across conversions - add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls * refactor(relaykit): normalize reasoning and thinking intent - centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent * fix(billing): preserve authoritative usage across relay hops - carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges * feat(relay): bridge Responses with Claude and Gemini protocols - add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens * fix(relay): wire relaykit conversions into host channels - connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage
…, and billing usage integrity (QuantumNous#7137) * feat(relaykit): preserve hosted tools across conversions - add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls * refactor(relaykit): normalize reasoning and thinking intent - centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent * fix(billing): preserve authoritative usage across relay hops - carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges * feat(relay): bridge Responses with Claude and Gemini protocols - add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens * fix(relay): wire relaykit conversions into host channels - connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage (cherry picked from commit 0ed497f)
…, and billing usage integrity (QuantumNous#7137) * feat(relaykit): preserve hosted tools across conversions - add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls * refactor(relaykit): normalize reasoning and thinking intent - centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent * fix(billing): preserve authoritative usage across relay hops - carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges * feat(relay): bridge Responses with Claude and Gemini protocols - add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens * fix(relay): wire relaykit conversions into host channels - connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage
…, and billing usage integrity (QuantumNous#7137) * feat(relaykit): preserve hosted tools across conversions - add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls * refactor(relaykit): normalize reasoning and thinking intent - centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent * fix(billing): preserve authoritative usage across relay hops - carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges * feat(relay): bridge Responses with Claude and Gemini protocols - add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens * fix(relay): wire relaykit conversions into host channels - connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage
移植上游 QuantumNous#7211(49ec469668)。gpt-6-astra 不匹配原先的 "gpt-5" 前缀, max_tokens / temperature / top_p / logprobs 原样透传,上游返回 400 "Unsupported parameter: 'max_tokens' ... Use 'max_completion_tokens' instead", 渠道测试也因此失败。 - dto 新增 OpenAIChatCapabilities / GetOpenAIChatCapabilities,按映射后模型与 推理力度统一决定:是否改用 max_completion_tokens、是否把 system 改为 developer、是否支持 temperature / top_p / logprobs(含 top_logprobs)。 - IsOpenAIGPT5Model 精确匹配 gpt-5 / gpt-5-* / gpt-5.*,不再误中 gpt-50 等。 - gpt-6-astra 及其日期快照纳入规则;gpt-5.1/5.2/5.4 未开推理时保留采样参数; 未识别的模型(含未来代际、gpt-6-astra-pro)不改写。 - adaptor 冲突手动解决:本分支没有 QuantumNous#7137 的推理意图函数,保留 ParseOpenAIReasoningEffortFromModelSuffix,但改为先剥离推理力度后缀再判定能力, 否则 gpt-6-astra-high 会被当作未知模型放行。 上游 controller/channel_test_request_test.go 依赖本分支缺少的 EffortTailModelIDs / ApplyReasoningModelSuffix,未移植;改为在 relay/channel/openai/chat_capabilities_test.go 覆盖转换行为。
Backport-of: QuantumNous/new-api 0ed497f (QuantumNous#7137)
Sync 266 upstream commits since f116414 (2026-08-18): Highlights merged: - Stability: OOM fix (QuantumNous#6949), SQLite WAL/busy-timeout (QuantumNous#7030), TLS transport isolation (QuantumNous#7627), DB migration hardening, bounded upstream header waits - Billing integrity: cross-protocol conversion loss fixes, stream-cut usage estimation (QuantumNous#7427), cached-token breakdowns, Gemini stream token details, tokenkit calibration, image billing via ResponsesUsageAccumulator - Auth/security overhaul: scoped access tokens, step-up verification, audit logs, password transport encryption (opt-in), unified OAuth, passkey RPID - Architecture: hosted-tool DTOs + ResponsesToolState (QuantumNous#7137), moejs JS task plugins, pricing expression system, model/vendor management rework, Responses WebSocket relay, channel editor redesign Custom features preserved and re-integrated: - bridge_tool_types / strip_tool_types / repair_text_tool_call_models: request-side chat bridge now coexists with upstream ResponsesToolState; response restoration prefers the bridge (keeps hashed long-name round-trips) and falls back to ToolState - emulated hosted tools (web_search/image/vision) + tool_hosting MCP module: wiring moved into PrepareResponsesRequest (new 5th return value carries the emulation base body) - price alias: GetCompletionRatio/GetBillingMode/GetBillingExpr/ratio lookups keep alias resolution ahead of upstream builtin-expression precedence; UI alias mode re-integrated into the expression pricing editors and PRICING_KEYS pipeline - non-native Responses upstream history guard: rewrites now also run on the adaptor's fast path - Aliyun ACR release workflow, custom Dockerfile tweaks Merge notes: - relaykit converter signatures synthesized: (ctx, req, bridge) - responses stream handler delegates usage/tool/image accounting to service.ResponsesUsageAccumulator (upstream semantics for images on incomplete responses) - go.mod: upstream dependency set + modelcontextprotocol/go-sdk v1.7.0 - i18n locales: key union, normalized via i18n:sync Verification on Windows: - go build ./... OK; relaykit tests all green; relay tests green except pre-existing TestWriteMCPVisionImageSecureTemporaryFile (Windows file perms, fails on pre-merge branch too) - controller: 101 failures identical to upstream/main run on the same machine (DB matrix tests need AUDIT_MYSQL_DSN etc; security tests hit Windows temp-dir cleanup locks) - not merge regressions - web: typecheck clean, vitest 2221/2221, lint clean for touched files, production build OK
Rebase the fork onto upstream v1.0.0-rc.44 (718a4e7), which already carries the rc.31 reasoning refactor (PR QuantumNous#7137): a protocol-neutral reasoning.Intent IR plus the geminiCapabilitiesFor capability table. The five local patches that previously implemented effort forwarding and the Gemini 3 thinking_level split are fully subsumed by that refactor, so the fork now tracks upstream directly and only keeps its own additions. Gemini 4 Argon rejects thinkingBudget with HTTP 400 and cannot disable thinking, so it needs the thinking_level dialect: register gemini-4 in the capability table, map minimal (dropped by this family) to low, and give the family its documented medium default. - relaykit/relayconvert/reasoning/gemini.go: gemini-4 capability entry, minimal to low coercion, medium default effort - relaykit/relayconvert/reasoning/gemini_test.go: render and normalize cases covering level passthrough, budget conversion, disable fallback, and the minimal downgrade - relay/channel/gemini/constant.go: list gemini-4-argon as a known model - .github/workflows/docker-publish.yml: restore the GHCR multi-arch build and derive the version tag from the nearest release tag instead of a hardcoded rc number
…, and billing usage integrity (QuantumNous#7137) * feat(relaykit): preserve hosted tools across conversions - add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls * refactor(relaykit): normalize reasoning and thinking intent - centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent * fix(billing): preserve authoritative usage across relay hops - carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges * feat(relay): bridge Responses with Claude and Gemini protocols - add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens * fix(relay): wire relaykit conversions into host channels - connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage
🔗 关联任务 / Related Issue
🚀 变更类型 / Type of change
📝 变更描述 / Description
本 PR 包含 5 个提交,围绕 relaykit 跨协议转换与计费完整性三条主线:
1. 内置(hosted)工具跨协议保真转换(
feat(relaykit): preserve hosted tools across conversions)toolconv:跨协议转换时将工具定义、tool_choice、hosted 工具调用历史从载荷中摘出、绕过中间 pivot、在目标协议贴回。web_search 在 OpenAI Chat / Claude / Gemini / Responses 四协议间全向保真,MCP 在 Claude ↔ Responses 间保真。url_citationannotations,Gemini groundingSupports → annotations /web_search_call。allow(静默降级 + 结构化诊断落管理日志);拒绝为渠道级 opt-in(ChannelOtherSettings.ToolLossPolicy),拒绝返回 400;响应/流阶段无论何种策略均不拒绝。other.admin_info.conversion_diagnostics(仅管理员可见)。2. reasoning/thinking 归一化(
refactor(relaykit): normalize reasoning and thinking intent)ReasoningIntent(unset/enabled/adaptive/disabled + 五档 effort + budget),按模型家族能力表渲染为目标协议字段;支持 Claude adaptive thinking,未知上游降级 manual。-thinking/-nothinking/effort 尾缀)上提到 relay 入口层(ApplyReasoningModelSuffix):入口一次解析、产出ReasoningConversionState、同步到所有出站请求副本;转换器只消费显式意图,不再各自解析模型名。deepseek-v4-前缀与火山引擎自解析后缀保持交由渠道 adaptor 处理,generic-thinking裁剪仅限 OpenRouter 方言。qwen-max等真实模型名保护)从 relaykit 硬编码移至宿主配置(GlobalSettings.EffortTailModelIDs)。max_tokens: 0恢复视为未设置(填默认值),管理员配置的默认值 0 语义不变。ModelPriceHelper使用带后缀的 origin 模型名 + 通配倍率条目。3. 计费 usage 完整性(
fix(billing): preserve authoritative usage across relay hops)BillingUsagesidecar)为结算真源:跨协议桥接克隆搬运而非重建,Source/Semantic 保留真实上游方言,级联部署可依赖该契约。CacheCreation整体替换、candidates+thoughts 成对采信);Claude 流中断时快照过期少计 output;倒数第二个 SSE 事件 usage 兜底收窄并对齐cached_tokens帧;非流式不可信 usage 整份替换防明细放大。willContinue终结分片只计一次),与 OpenAI/Claude 渠道对齐。-nothinking计费别名路径(其差异定价只服务于已退役的 2.5 flash preview 模型,且存在 self-use 模式下落 37.5 兜底倍率的多收风险);保留-nothinking请求适配(关闭 thinking)与BillingModelName基础设施。4. 协议桥与端点(
feat(relay): bridge Responses with Claude and Gemini protocols)sequence_number。POST /v1/messages/count_tokens端点(Anthropic token 计数,不走上游、不计费、不受估算开关影响)。5. 宿主接线(
fix(relay): wire relaykit conversions into host channels)ReasoningConversion状态清理、转换失败错误呈现(客户端错误 400 + SkipRetry)。📸 运行证明 / Proof of Work
两模块构建与测试通过:根模块
go build ./... && go test ./...;relaykit 独立模块cd relaykit && GOWORK=off go build ./... && GOWORK=off go test ./...。计费不变量、转换语义与后缀适配均有对应单元/回归测试锁定(testify)。✅ 提交前检查项 / Checklist
Summary by CodeRabbit