Repository navigation
feat(relay): web search encoding per channel and vendor-reported search billing - #7690
Merged
Merged
Conversation
…s on Advanced Custom channels Advanced Custom channels could convert Claude Messages and Gemini generateContent requests only to OpenAI Chat. Two converters, claude_messages_to_openai_responses and gemini_generate_content_to_openai_responses, now send them to an OpenAI Responses upstream and convert the response back to the client's format. The channel form offers them with the /v1/responses upstream path and bearer auth by default, and the backend validates their incoming paths like the existing converters. The IDs keep the values the relay already uses for these conversions.
Moonshot channels answered /v1/responses with "not implemented". They
now forward the request body unchanged to {base}/v1/responses. Kimi
serves only kimi-k3 on that endpoint today; other models are rejected
by the upstream, not by the gateway.
…quest ends failed or incomplete An image_generation call that completed before a response.failed, response.incomplete, or cancelled terminal had already reached the client, but the stream accumulator reset the image count to zero on those terminals, and the non-stream handler skipped image outputs when the response status was failed, cancelled, or incomplete. Completed images now bill on every terminal, as they already did when the client disconnected; image items that never completed still bill nothing.
…ported search counts Converted requests always encoded hosted web search the OpenAI way: web_search_options on Chat and the native hosted tool on Responses, Claude, and Gemini. Zhipu and DashScope compatible-mode Chat take their own search fields, and the search counts that Azure, xAI, and DashScope report in usage were ignored; only web_search_call items were billed. relaykit gains a WebSearchEncoder option that a channel adaptor sets in Init; without one, the default encodings stay. Zhipu sends the GLM web_search tool with search_std, and Gemini clients on Zhipu now convert through Chat. DashScope compatible-mode Chat sends enable_search and search_options with the default turbo strategy and maps allowed domains to assigned_site_list (up to 25 sites). DashScope Anthropic-compatible requests that carry a web search tool get the cc_entrypoint system marker DashScope requires before it searches. Zhipu and DashScope Chat responses report no search usage, so a forced search bills one call, which is dropped again if the request retries on another channel; a Zhipu non-stream response that returns search results also bills one. Several hosted web search definitions on a Chat target now drop the extras with a diagnostic instead of failing the request. Vendor-reported counts replace the counted web_search_call items under the vendor's price key: Azure tool_usage.web_search.num_requests as bing_web_search, xAI server_side_tool_usage_details (web_search_calls as web_search, priced for grok models, plus x_search posts and profiles), and DashScope usage.x_tools on Responses and server_tool_use on its Anthropic endpoint as search_strategy_agent. Claude server_tool_use also counts web_fetch, code_execution, and tool_search, which bill only once an operator prices them. Tool names with a built-in price and these Claude server tools are reserved, so a client function of the same name is never billed under them. The Claude and Gemini search counts move from gin context keys into the per-request tool counts, and every tool count is capped at 10,000 at settlement. Built-in prices are seeded for these keys in each vendor's list currency (Zhipu and DashScope in CNY, Azure and xAI in USD) and listed on the tool price settings page. DashScope Responses requests move to /compatible-mode/v1/responses, because the old /api/v2/apps/protocols endpoint is no longer maintained.
Contributor
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing (52)
✨ 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 |
zengwenliang416
pushed a commit
to zengwenliang416/new-api
that referenced
this pull request
Oct 8, 2026
…ch billing (QuantumNous#7690) * feat(channels): convert Claude and Gemini requests to OpenAI Responses on Advanced Custom channels Advanced Custom channels could convert Claude Messages and Gemini generateContent requests only to OpenAI Chat. Two converters, claude_messages_to_openai_responses and gemini_generate_content_to_openai_responses, now send them to an OpenAI Responses upstream and convert the response back to the client's format. The channel form offers them with the /v1/responses upstream path and bearer auth by default, and the backend validates their incoming paths like the existing converters. The IDs keep the values the relay already uses for these conversions. * feat(moonshot): forward Responses requests to Kimi unchanged Moonshot channels answered /v1/responses with "not implemented". They now forward the request body unchanged to {base}/v1/responses. Kimi serves only kimi-k3 on that endpoint today; other models are rejected by the upstream, not by the gateway. * fix(relay): bill completed image_generation calls when a Responses request ends failed or incomplete An image_generation call that completed before a response.failed, response.incomplete, or cancelled terminal had already reached the client, but the stream accumulator reset the image count to zero on those terminals, and the non-stream handler skipped image outputs when the response status was failed, cancelled, or incomplete. Completed images now bill on every terminal, as they already did when the client disconnected; image items that never completed still bill nothing. * feat(relay): let channels encode hosted web search and bill vendor-reported search counts Converted requests always encoded hosted web search the OpenAI way: web_search_options on Chat and the native hosted tool on Responses, Claude, and Gemini. Zhipu and DashScope compatible-mode Chat take their own search fields, and the search counts that Azure, xAI, and DashScope report in usage were ignored; only web_search_call items were billed. relaykit gains a WebSearchEncoder option that a channel adaptor sets in Init; without one, the default encodings stay. Zhipu sends the GLM web_search tool with search_std, and Gemini clients on Zhipu now convert through Chat. DashScope compatible-mode Chat sends enable_search and search_options with the default turbo strategy and maps allowed domains to assigned_site_list (up to 25 sites). DashScope Anthropic-compatible requests that carry a web search tool get the cc_entrypoint system marker DashScope requires before it searches. Zhipu and DashScope Chat responses report no search usage, so a forced search bills one call, which is dropped again if the request retries on another channel; a Zhipu non-stream response that returns search results also bills one. Several hosted web search definitions on a Chat target now drop the extras with a diagnostic instead of failing the request. Vendor-reported counts replace the counted web_search_call items under the vendor's price key: Azure tool_usage.web_search.num_requests as bing_web_search, xAI server_side_tool_usage_details (web_search_calls as web_search, priced for grok models, plus x_search posts and profiles), and DashScope usage.x_tools on Responses and server_tool_use on its Anthropic endpoint as search_strategy_agent. Claude server_tool_use also counts web_fetch, code_execution, and tool_search, which bill only once an operator prices them. Tool names with a built-in price and these Claude server tools are reserved, so a client function of the same name is never billed under them. The Claude and Gemini search counts move from gin context keys into the per-request tool counts, and every tool count is capped at 10,000 at settlement. Built-in prices are seeded for these keys in each vendor's list currency (Zhipu and DashScope in CNY, Azure and xAI in USD) and listed on the tool price settings page. DashScope Responses requests move to /compatible-mode/v1/responses, because the old /api/v2/apps/protocols endpoint is no longer maintained.
Javabcd
pushed a commit
to Javabcd/new-api
that referenced
this pull request
Oct 9, 2026
…ch billing (QuantumNous#7690) * feat(channels): convert Claude and Gemini requests to OpenAI Responses on Advanced Custom channels Advanced Custom channels could convert Claude Messages and Gemini generateContent requests only to OpenAI Chat. Two converters, claude_messages_to_openai_responses and gemini_generate_content_to_openai_responses, now send them to an OpenAI Responses upstream and convert the response back to the client's format. The channel form offers them with the /v1/responses upstream path and bearer auth by default, and the backend validates their incoming paths like the existing converters. The IDs keep the values the relay already uses for these conversions. * feat(moonshot): forward Responses requests to Kimi unchanged Moonshot channels answered /v1/responses with "not implemented". They now forward the request body unchanged to {base}/v1/responses. Kimi serves only kimi-k3 on that endpoint today; other models are rejected by the upstream, not by the gateway. * fix(relay): bill completed image_generation calls when a Responses request ends failed or incomplete An image_generation call that completed before a response.failed, response.incomplete, or cancelled terminal had already reached the client, but the stream accumulator reset the image count to zero on those terminals, and the non-stream handler skipped image outputs when the response status was failed, cancelled, or incomplete. Completed images now bill on every terminal, as they already did when the client disconnected; image items that never completed still bill nothing. * feat(relay): let channels encode hosted web search and bill vendor-reported search counts Converted requests always encoded hosted web search the OpenAI way: web_search_options on Chat and the native hosted tool on Responses, Claude, and Gemini. Zhipu and DashScope compatible-mode Chat take their own search fields, and the search counts that Azure, xAI, and DashScope report in usage were ignored; only web_search_call items were billed. relaykit gains a WebSearchEncoder option that a channel adaptor sets in Init; without one, the default encodings stay. Zhipu sends the GLM web_search tool with search_std, and Gemini clients on Zhipu now convert through Chat. DashScope compatible-mode Chat sends enable_search and search_options with the default turbo strategy and maps allowed domains to assigned_site_list (up to 25 sites). DashScope Anthropic-compatible requests that carry a web search tool get the cc_entrypoint system marker DashScope requires before it searches. Zhipu and DashScope Chat responses report no search usage, so a forced search bills one call, which is dropped again if the request retries on another channel; a Zhipu non-stream response that returns search results also bills one. Several hosted web search definitions on a Chat target now drop the extras with a diagnostic instead of failing the request. Vendor-reported counts replace the counted web_search_call items under the vendor's price key: Azure tool_usage.web_search.num_requests as bing_web_search, xAI server_side_tool_usage_details (web_search_calls as web_search, priced for grok models, plus x_search posts and profiles), and DashScope usage.x_tools on Responses and server_tool_use on its Anthropic endpoint as search_strategy_agent. Claude server_tool_use also counts web_fetch, code_execution, and tool_search, which bill only once an operator prices them. Tool names with a built-in price and these Claude server tools are reserved, so a client function of the same name is never billed under them. The Claude and Gemini search counts move from gin context keys into the per-request tool counts, and every tool count is capped at 10,000 at settlement. Built-in prices are seeded for these keys in each vendor's list currency (Zhipu and DashScope in CNY, Azure and xAI in USD) and listed on the tool price settings page. DashScope Responses requests move to /compatible-mode/v1/responses, because the old /api/v2/apps/protocols endpoint is no longer maintained.
12 tasks
timwenx
pushed a commit
to timwenx/new-api
that referenced
this pull request
Nov 18, 2026
…ch billing (QuantumNous#7690) * feat(channels): convert Claude and Gemini requests to OpenAI Responses on Advanced Custom channels Advanced Custom channels could convert Claude Messages and Gemini generateContent requests only to OpenAI Chat. Two converters, claude_messages_to_openai_responses and gemini_generate_content_to_openai_responses, now send them to an OpenAI Responses upstream and convert the response back to the client's format. The channel form offers them with the /v1/responses upstream path and bearer auth by default, and the backend validates their incoming paths like the existing converters. The IDs keep the values the relay already uses for these conversions. * feat(moonshot): forward Responses requests to Kimi unchanged Moonshot channels answered /v1/responses with "not implemented". They now forward the request body unchanged to {base}/v1/responses. Kimi serves only kimi-k3 on that endpoint today; other models are rejected by the upstream, not by the gateway. * fix(relay): bill completed image_generation calls when a Responses request ends failed or incomplete An image_generation call that completed before a response.failed, response.incomplete, or cancelled terminal had already reached the client, but the stream accumulator reset the image count to zero on those terminals, and the non-stream handler skipped image outputs when the response status was failed, cancelled, or incomplete. Completed images now bill on every terminal, as they already did when the client disconnected; image items that never completed still bill nothing. * feat(relay): let channels encode hosted web search and bill vendor-reported search counts Converted requests always encoded hosted web search the OpenAI way: web_search_options on Chat and the native hosted tool on Responses, Claude, and Gemini. Zhipu and DashScope compatible-mode Chat take their own search fields, and the search counts that Azure, xAI, and DashScope report in usage were ignored; only web_search_call items were billed. relaykit gains a WebSearchEncoder option that a channel adaptor sets in Init; without one, the default encodings stay. Zhipu sends the GLM web_search tool with search_std, and Gemini clients on Zhipu now convert through Chat. DashScope compatible-mode Chat sends enable_search and search_options with the default turbo strategy and maps allowed domains to assigned_site_list (up to 25 sites). DashScope Anthropic-compatible requests that carry a web search tool get the cc_entrypoint system marker DashScope requires before it searches. Zhipu and DashScope Chat responses report no search usage, so a forced search bills one call, which is dropped again if the request retries on another channel; a Zhipu non-stream response that returns search results also bills one. Several hosted web search definitions on a Chat target now drop the extras with a diagnostic instead of failing the request. Vendor-reported counts replace the counted web_search_call items under the vendor's price key: Azure tool_usage.web_search.num_requests as bing_web_search, xAI server_side_tool_usage_details (web_search_calls as web_search, priced for grok models, plus x_search posts and profiles), and DashScope usage.x_tools on Responses and server_tool_use on its Anthropic endpoint as search_strategy_agent. Claude server_tool_use also counts web_fetch, code_execution, and tool_search, which bill only once an operator prices them. Tool names with a built-in price and these Claude server tools are reserved, so a client function of the same name is never billed under them. The Claude and Gemini search counts move from gin context keys into the per-request tool counts, and every tool count is capped at 10,000 at settlement. Built-in prices are seeded for these keys in each vendor's list currency (Zhipu and DashScope in CNY, Azure and xAI in USD) and listed on the tool price settings page. DashScope Responses requests move to /compatible-mode/v1/responses, because the old /api/v2/apps/protocols endpoint is no longer maintained.
xiaolanawaqaq
pushed a commit
to xiaolanawaqaq/new-apiaaa
that referenced
this pull request
Nov 26, 2026
…ch billing (QuantumNous#7690) * feat(channels): convert Claude and Gemini requests to OpenAI Responses on Advanced Custom channels Advanced Custom channels could convert Claude Messages and Gemini generateContent requests only to OpenAI Chat. Two converters, claude_messages_to_openai_responses and gemini_generate_content_to_openai_responses, now send them to an OpenAI Responses upstream and convert the response back to the client's format. The channel form offers them with the /v1/responses upstream path and bearer auth by default, and the backend validates their incoming paths like the existing converters. The IDs keep the values the relay already uses for these conversions. * feat(moonshot): forward Responses requests to Kimi unchanged Moonshot channels answered /v1/responses with "not implemented". They now forward the request body unchanged to {base}/v1/responses. Kimi serves only kimi-k3 on that endpoint today; other models are rejected by the upstream, not by the gateway. * fix(relay): bill completed image_generation calls when a Responses request ends failed or incomplete An image_generation call that completed before a response.failed, response.incomplete, or cancelled terminal had already reached the client, but the stream accumulator reset the image count to zero on those terminals, and the non-stream handler skipped image outputs when the response status was failed, cancelled, or incomplete. Completed images now bill on every terminal, as they already did when the client disconnected; image items that never completed still bill nothing. * feat(relay): let channels encode hosted web search and bill vendor-reported search counts Converted requests always encoded hosted web search the OpenAI way: web_search_options on Chat and the native hosted tool on Responses, Claude, and Gemini. Zhipu and DashScope compatible-mode Chat take their own search fields, and the search counts that Azure, xAI, and DashScope report in usage were ignored; only web_search_call items were billed. relaykit gains a WebSearchEncoder option that a channel adaptor sets in Init; without one, the default encodings stay. Zhipu sends the GLM web_search tool with search_std, and Gemini clients on Zhipu now convert through Chat. DashScope compatible-mode Chat sends enable_search and search_options with the default turbo strategy and maps allowed domains to assigned_site_list (up to 25 sites). DashScope Anthropic-compatible requests that carry a web search tool get the cc_entrypoint system marker DashScope requires before it searches. Zhipu and DashScope Chat responses report no search usage, so a forced search bills one call, which is dropped again if the request retries on another channel; a Zhipu non-stream response that returns search results also bills one. Several hosted web search definitions on a Chat target now drop the extras with a diagnostic instead of failing the request. Vendor-reported counts replace the counted web_search_call items under the vendor's price key: Azure tool_usage.web_search.num_requests as bing_web_search, xAI server_side_tool_usage_details (web_search_calls as web_search, priced for grok models, plus x_search posts and profiles), and DashScope usage.x_tools on Responses and server_tool_use on its Anthropic endpoint as search_strategy_agent. Claude server_tool_use also counts web_fetch, code_execution, and tool_search, which bill only once an operator prices them. Tool names with a built-in price and these Claude server tools are reserved, so a client function of the same name is never billed under them. The Claude and Gemini search counts move from gin context keys into the per-request tool counts, and every tool count is capped at 10,000 at settlement. Built-in prices are seeded for these keys in each vendor's list currency (Zhipu and DashScope in CNY, Azure and xAI in USD) and listed on the tool price settings page. DashScope Responses requests move to /compatible-mode/v1/responses, because the old /api/v2/apps/protocols endpoint is no longer maintained.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 关联任务 / Related Issue
0a03088e0(转换修复与转换流失败计费),第 4 个提交依赖它。🚀 变更类型 / Type of change
📝 变更描述 / Description
1. Advanced Custom 渠道新增 Claude / Gemini → OpenAI Responses 转换器(e2d4612b5)
原来 Claude Messages、Gemini generateContent 请求只能转成 OpenAI Chat。新增
claude_messages_to_openai_responses、gemini_generate_content_to_openai_responses两个转换器:请求转成 Responses 发往上游,响应再转回客户端格式。渠道表单默认上游路径/v1/responses、Bearer 鉴权;后端和现有转换器一样校验入口路径。转换器 ID 沿用 relay 里已在用的值。2. Moonshot 渠道原生转发 Responses(308eafd2b)
原来
/v1/responses直接返回 not implemented。现在请求体原样转发到{base}/v1/responses。Kimi 该端点目前只支持 kimi-k3,其他模型由上游拒绝。3. Responses 以 failed / incomplete 结束时,已完成的图片照常计费(3a8a40904)
image_generation 在
response.failed、incomplete、cancelled之前就已完成并送达客户端,但流式累加器在这些终止事件上把图片数清零,非流式在这些状态下跳过图片输出,导致这些图片没有计费。现在已完成的图片在任何终止状态下都计费,和客户端断开时的规则一致;未完成的图片项仍不计费。4. 渠道自定义 web search 编码,按厂商上报的次数计费(d8cd75490)
convmeta.Options增加WebSearch(WebSearchEncoder),由渠道 adaptor 在Init中设置。不设置时保持原有编码:Chat 用web_search_options,Responses / Claude / Gemini 用原生托管工具。web_search工具(search_std);Gemini 客户端接入智谱时先转成 Chat。enable_search和search_options,使用默认 turbo 策略,allowed_domains映射为assigned_site_list(最多 25 个)。Anthropic 兼容端点的请求带 web search 工具时,在 system 中补cc_entrypoint标记,否则阿里不执行搜索。web_search_call计的条目:tool_usage.web_search.num_requests计入bing_web_searchserver_side_tool_usage_details计入web_search(按 grok 模型定价),x_search按帖子数和用户数分别计费usage.x_tools,以及 Anthropic 端点的server_tool_use,计入search_strategy_agentserver_tool_use另外记录web_fetch、code_execution、tool_search,运营者定价后才收费。带内置价格的工具名和这几个 Claude 服务端工具名都保留,客户端的同名函数不会按它们计费。/compatible-mode/v1/responses,因为旧的/api/v2/apps/protocols端点官方已停止维护。本 PR 不包含 Gemini grounding 按查询计费,Gemini 仍然是每个有 grounding 的响应计 1 次
google_search,次数和现在相同。📸 运行证明 / Proof of Work
每个提交都单独导出完整代码树(基础提交加上到该提交为止的改动)后验证:
未提交的本地回归测试(relaykit 的转换测试按约定只保留在本地):web search 编码钩子、Advanced Custom 路径校验均通过;B1 差分回放把已捕获的请求重放一遍,所有路由的计费与基线逐字节一致。
实网测试(2026-10-05,在包含本批改动的完整工作树上,之后的精简只有单元测试和本地测试覆盖):
enable_search: true、search_options: {forced_search: true},没有web_search_optionsusage.x_tools.web_search.count 1计为search_strategy_agent× 1server_tool_use.web_search_requests 1计为search_strategy_agent× 1/v1/responses,请求体不变,web_search× 1web_search× 1✅ 提交前检查项 / Checklist
New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。go build通过或测试通过,不视为有效证明。🤖 Generated with Claude Code