Skip to content

feat(relay): web search encoding per channel and vendor-reported search billing - #7690

Merged
Calcium-Ion merged 4 commits into
mainfrom
feat/web-search-hook-vendor-billing
Oct 7, 2026
Merged

Calcium-Ion merged 4 commits into
mainfrom
feat/web-search-hook-vendor-billing

Conversation

@Calcium-Ion

@Calcium-Ion Calcium-Ion commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

🔗 关联任务 / Related Issue

  • 无对应 Issue。按此前定好的 web search 编码注入方案实现。同一批里的图片计数修正、Advanced Custom 转换器、Kimi Responses 透传各占一个提交,共 4 个提交。
  • 基于 main 的 0a03088e0(转换修复与转换流失败计费),第 4 个提交依赖它。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

📝 变更描述 / 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)

  • relaykit 在 convmeta.Options 增加 WebSearch(WebSearchEncoder),由渠道 adaptor 在 Init 中设置。不设置时保持原有编码:Chat 用 web_search_options,Responses / Claude / Gemini 用原生托管工具。
  • 智谱:Chat 改发 GLM web_search 工具(search_std);Gemini 客户端接入智谱时先转成 Chat。
  • 阿里 compatible-mode Chat:发 enable_search 和 search_options,使用默认 turbo 策略,allowed_domains 映射为 assigned_site_list(最多 25 个)。Anthropic 兼容端点的请求带 web search 工具时,在 system 中补 cc_entrypoint 标记,否则阿里不执行搜索。
  • 智谱、阿里 Chat 的响应不报告搜索次数,因此强制搜索按 1 次计费,换渠道重试时撤销;智谱非流式响应返回了搜索结果时也计 1 次。
  • 厂商上报的次数替换按 web_search_call 计的条目:
    • Azure tool_usage.web_search.num_requests 计入 bing_web_search
    • xAI server_side_tool_usage_details 计入 web_search(按 grok 模型定价),x_search 按帖子数和用户数分别计费
    • 阿里 Responses 的 usage.x_tools,以及 Anthropic 端点的 server_tool_use,计入 search_strategy_agent
  • Claude server_tool_use 另外记录 web_fetch、code_execution、tool_search,运营者定价后才收费。带内置价格的工具名和这几个 Claude 服务端工具名都保留,客户端的同名函数不会按它们计费。
  • Claude、Gemini 的搜索次数从 gin context key 改为记入请求的工具计数;结算时每项工具次数最多按 10,000 计。
  • 按厂商标价币种内置价格(智谱、阿里为 CNY,Azure、xAI 为 USD),工具价格设置页同步。
  • Chat 目标出现多个托管 web search 定义时,丢弃多余的并给出诊断,不再报错。
  • 阿里 Responses 地址改为 /compatible-mode/v1/responses,因为旧的 /api/v2/apps/protocols 端点官方已停止维护。

本 PR 不包含 Gemini grounding 按查询计费,Gemini 仍然是每个有 grounding 的响应计 1 次 google_search,次数和现在相同。

📸 运行证明 / Proof of Work

每个提交都单独导出完整代码树(基础提交加上到该提交为止的改动)后验证:

gofmt -l <改动的 .go 文件>                       # 无输出
go build ./... && go vet ./...                    # 4 个提交均通过
cd relaykit && GOWORK=off go build ./... && GOWORK=off go vet ./... && GOWORK=off go test -count=1 ./...
# 4 个提交均为 9 个包 ok
go test ./relay/... ./service/... ./setting/... ./controller/... ./plugins/...
# 提交 1、2:31 个包 ok;提交 3(relay/service/setting/controller):30 个包 ok
# 最终提交:根模块全部包(与 make test 范围相同)43 个包 ok,tokenkit 构建通过
cd web && bun run typecheck                       # 提交 1 和最终提交均通过
oxlint、oxfmt --check(改动的 3 个 TS 文件和 7 个语言文件)   # 通过
vitest:tool-price-validation、channel-quick-options、responses-websocket-setting
# 3 个文件、42 个用例通过

未提交的本地回归测试(relaykit 的转换测试按约定只保留在本地):web search 编码钩子、Advanced Custom 路径校验均通过;B1 差分回放把已捕获的请求重放一遍,所有路由的计费与基线逐字节一致。

实网测试(2026-10-05,在包含本批改动的完整工作树上,之后的精简只有单元测试和本地测试覆盖):

  • 阿里 compatible-mode Chat 强制搜索:上游收到 enable_search: true、search_options: {forced_search: true},没有 web_search_options
  • 阿里 Responses:usage.x_tools.web_search.count 1 计为 search_strategy_agent × 1
  • 阿里 Anthropic 端点:system 中标记块只出现一次,server_tool_use.web_search_requests 1 计为 search_strategy_agent × 1
  • Kimi Responses 透传(kimi-k3):请求地址为 /v1/responses,请求体不变,web_search × 1
  • Anthropic web search:web_search × 1
  • 智谱、Azure、xAI 没有可用的渠道或 key,只做了 mock 测试;图片计数修正只有单元测试

✅ 提交前检查项 / Checklist

  • 人工确认: 无论描述是否由 AI 生成,我已审阅并精炼全文,仅保留可复核的要点,并声明对其准确性与完整性负责。PR 正文及后续评论均未直接粘贴未经人工过滤的大段 AI 文本。
  • 非重复提交: 我已搜索现有的 Issues 与 PRs,确认不是重复提交。
  • 新功能关联 Issue: 若此 PR 标记为 New feature,我已关联对应 Issue;若尚无 Issue,我已先自行创建。
  • 事前沟通: 若改动较大或涉及方向性变更,已在关联 Issue 中与维护者沟通并达成一致。
  • 功能范围: 本 PR 不是 Coding Plan、逆向渠道、第三方封装接口,也不是对 Codex 渠道类型的改动。
  • 非透传: 本 PR 不是针对开启透传后的转发行为;透传会直接发送内容,不会经过 new-api 的逻辑处理。
  • 范围聚焦: 本 PR 为一项聚焦改动,未包含无关代码。
  • 本地验证: 已按变更路径实际验证,并写明命令与观察结果。仅声明 go build 通过或测试通过,不视为有效证明。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

🤖 Generated with Claude Code

…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.
@coderabbitai

coderabbitai Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: add328f3-8bab-443c-b063-1cc9ebbfd37a
📥 Commits

Reviewing files that changed from the base of the PR and between 0a03088 and d8cd754.

📒 Files selected for processing (52)
  • relay/alpha_search_handler.go
  • relay/channel/advancedcustom/adaptor.go
  • relay/channel/advancedcustom/adaptor_test.go
  • relay/channel/ali/adaptor.go
  • relay/channel/ali/adaptor_test.go
  • relay/channel/ali/web_search.go
  • relay/channel/claude/relay-claude.go
  • relay/channel/claude/relay_responses.go
  • relay/channel/claude/tool_billing_test.go
  • relay/channel/gemini/relay-gemini.go
  • relay/channel/gemini/relay_responses.go
  • relay/channel/moonshot/adaptor.go
  • relay/channel/moonshot/adaptor_test.go
  • relay/channel/openai/adaptor.go
  • relay/channel/openai/relay-openai.go
  • relay/channel/openai/relay_responses.go
  • relay/channel/openai/relay_responses_billing_test.go
  • relay/channel/openai/web_search.go
  • relay/channel/xai/adaptor.go
  • relay/channel/xai/web_search.go
  • relay/channel/zhipu_4v/adaptor.go
  • relay/channel/zhipu_4v/web_search.go
  • relay/common/relay_info.go
  • relay/common/tool_usage.go
  • relay/common/tool_usage_test.go
  • relay/response_model_test.go
  • relay/responses_websocket.go
  • relaykit/dto/channel_settings.go
  • relaykit/dto/openai_request.go
  • relaykit/relayconvert/convmeta/options.go
  • relaykit/relayconvert/convmeta/websearch.go
  • relaykit/relayconvert/internal/toolconv/encode.go
  • relaykit/relayconvert/internal/toolconv/model.go
  • relaykit/relayconvert/request_registry.go
  • relaykit/relayconvert/request_registry_test.go
  • relaykit/relayconvert/text_converter_registry.go
  • service/responses_usage.go
  • service/responses_usage_test.go
  • service/text_quota.go
  • service/text_quota_test.go
  • setting/operation_setting/tools.go
  • setting/operation_setting/tools_price_test.go
  • web/src/features/channels/lib/advanced-custom.ts
  • web/src/features/channels/types.ts
  • web/src/features/system-settings/models/tool-price-settings.tsx
  • web/src/i18n/locales/en.json
  • web/src/i18n/locales/fr.json
  • web/src/i18n/locales/ja.json
  • web/src/i18n/locales/ru.json
  • web/src/i18n/locales/vi.json
  • web/src/i18n/locales/zh-TW.json
  • web/src/i18n/locales/zh.json
 _______________________________________________________
< I find bugs faster than you can say 'merge conflict.' >
 -------------------------------------------------------
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Calcium-Ion
Calcium-Ion merged commit acb347a into main Oct 7, 2026
2 of 3 checks passed
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.
@Calcium-Ion
Calcium-Ion deleted the feat/web-search-hook-vendor-billing branch October 22, 2026 04:57
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant