Skip to content

fix(billing): settle Imagen and Jimeng from returned image payloads - #8015

Closed
yxkong wants to merge 3 commits into
QuantumNous:mainfrom
yxkong:codex/image-payload-count
Closed

yxkong wants to merge 3 commits into
QuantumNous:mainfrom
yxkong:codex/image-payload-count

Conversation

@yxkong

@yxkong yxkong commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Agent

本 PR 由 Codex 辅助编写。

Links

Kind

  • Bug fix

Issue facts

  • Actual behavior: Imagen 请求 4 张、有效产物 2 张时 n 仍为 4;即梦请求 4 张、URL 1 张时 n 仍为 4;表达式实际数量均未写入。
  • Impact: 按张结算可多计或少计。
  • Frequency: 合成固定输入稳定复现,没有线上频率结论。
  • Evidence that the problem is in new-api rather than the client or upstream: 真实 handler 正常转换图片,但计费数量断言在原 main 失败。
  • Applicable types and their fields (relay / billing / frontend / deployment; write "not applicable" otherwise): relay + billing,详见两个 issue;前端/部署不适用。

Change

Imagen 计数未过滤且非空的 base64;即梦取有效 URL 数与 base64 数的较大值,避免同图两种表示重复计数。两者调用现有 UpdateImageCount,更新固定价 n 与表达式数量;零或超限结果保留预留值。

即梦 JSON 调用改用项目统一入口 common.Marshal/Unmarshal;当前 codec 仍调用标准库 encoding/json,没有切换 JSON 引擎。

Research

两条响应路径补齐同一计费事实缺口。

Duplicate / prior art

Docs and code

Alternatives considered

  • Option A: 沿用请求 n,会保留已复现的错误。
  • Option B: 按 JSON 条目或两种表示相加,会计入空载荷/过滤项或重复计数。
  • Why this approach: 复用数量入口及其范围检查,不另建结算流程。

Files

Path Why
relay/channel/gemini/relay-gemini.go Imagen 有效载荷计数
relay/channel/gemini/relay_gemini_usage_test.go 扩展现有回归
relay/channel/jimeng/image.go 即梦计数;修改处使用 common JSON 包装
relay/channel/jimeng/image_test.go 唯一新增测试文件,验证真实 handler

Behavior

  • Before: 响应数量不同仍按请求 n,表达式没有实际数量。
  • After: 正数且上限内的有效产物数进入既有结算,返回图片内容保持原样。
  • Explicit non-goals / leftover work: 不修改单价、零张退款、请求校验、数据库或其他协议问题。

Verification

  • Commands and results:
    • go test ./relay/channel/gemini ./relay/channel/jimeng -count=1 -run 'Test(Gemini|Jimeng)ImageHandlerBillsPayloadCount':仅加测试时两包失败,观测 expected 2/actual 4、expected 1/actual 4 和实际数量 nil;补齐调用后两包通过。
    • go test ./relay/channel/gemini ./relay/channel/jimeng -count=1:两包通过。
    • go test -race -parallel=1 ./relay/channel/gemini ./relay/channel/jimeng -count=1:两包通过,含 18 个新子用例。
    • go vet ./relay/channel/gemini ./relay/channel/jimeng && go build ./...:退出码 0。
  • Manual steps and observed result: 无线上操作。
  • UI: screenshot or recording (or why none): 无 UI 改动。
  • Tests added or updated, or why none: 过滤/空载荷、张数增减、双表示去重、零/超限保留预留、表达式数量和上游错误。
  • Databases / providers / platforms exercised: Go 1.26.10,macOS arm64,真实 handler + 合成 HTTP body,不访问数据库或厂商服务。
  • Not verified: 默认并行整包 -race 因既有 gin.SetMode 竞争失败,原 main 生产代码与测试经 overlay 同样失败;串行 race 通过。真实厂商可用性、钱包余额和日志未实跑,issue 金额为合成单价公式推导。

既有竞争涉及 TestGeminiChatHandlerCompletionTokensExcludeToolUsePromptTokens、TestGeminiTextGenerationHandlerUsesEstimatedPromptTokensWhenUsagePromptMissing、TestGeminiChatHandlerPromptOnlyUsageMetadataEstimatesCompletionTokens、TestGeminiChatHandlerMissingUsageMetadataBuildsEstimatedBillingUsage、TestGeminiTextGenerationHandlerPromptTokensIncludeToolUsePromptTokens、TestGeminiChatHandlerUsesEstimatedPromptTokensWhenUsagePromptMissing;baseline 运行还报告 TestStreamResponseGeminiChat2OpenAIAttachesUsageMetadata。

本次复核(fa90a4f8f,仅函数注释):go test -race -parallel=1 ./relay/channel/gemini ./relay/channel/jimeng -count=1 → 两包通过(2.463s / 2.735s);gofmt、git diff --check 通过。

最新补充(ecc66ddee):完善相邻转换函数与既有 usage 元数据回归的说明,仅修改注释;gofmt、git diff --check 通过。

CI:最新运行记录,后端 vet/build/test、前端 typecheck/test 均通过(提交 ecc66ddee)。

Risks

  • Failure modes: 零或超限计数保留预留值,不触发零张退款。
  • Billing / quota / auth impact: 修改按张计费数量,不改 token usage、价格、账本实现或认证。
  • Follow-ups: 既有 Gemini 并行测试全局状态竞争另行处理。

Summary by CodeRabbit

  • Bug Fixes
    • Image billing excludes safety-filtered predictions and predictions with blank payloads, while keeping non-filtered entries in the response.
    • Images provided as both a URL and base64 data are counted once. Billing also reflects the larger of the nonblank URL and base64 image counts.
    • Image usage calculations handle empty results and differing requested and returned image counts more accurately.

Use valid unfiltered Imagen payloads and the larger Jimeng URL/base64
count to update the existing fixed-price and expression quantities.
Keep the reserved quantity when the response count is zero or invalid.
@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1a2cfe0a-99df-401f-a24f-185a7d4d1e19
Commits

Reviewing files that changed from the base of the PR and between f1569d1 and ecc66dd.

Files selected for processing: 4
  • relay/channel/gemini/relay-gemini.go
  • relay/channel/gemini/relay_gemini_usage_test.go
  • relay/channel/jimeng/image.go
  • relay/channel/jimeng/image_test.go

Walkthrough

Gemini and Jimeng image handlers now update billing counts from returned image payloads. The changes add tests for filtered or blank payloads, differing request and response counts, expression billing, and error cases.

Changes

Image payload billing

Layer / File(s) Summary
Gemini prediction billing count
relay/channel/gemini/relay-gemini.go, relay/channel/gemini/relay_gemini_usage_test.go
Gemini counts unfiltered predictions with nonblank base64 data for billing while retaining all unfiltered predictions in the response. Tests check billing counts, response entries, and error cases.
Jimeng payload billing count
relay/channel/jimeng/image.go, relay/channel/jimeng/image_test.go
Jimeng counts nonblank URLs and base64 payloads separately, then records the larger count. JSON handling uses common.Unmarshal and common.Marshal. Tests cover payload counts and billing outcomes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Priority: Normal

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • #8013: The PR counts valid returned Imagen base64 payloads and passes that count to UpdateImageCount, addressing the reported billing mismatch.
  • #8014: The PR counts valid Jimeng URL and base64 payloads, avoids double-counting, and passes the result to UpdateImageCount.

Suggested reviewers: feitianbubu

Merge Risk: Minimal · up to ecc66

The image billing changes are ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Out of Scope Changes check Warning relay/channel/jimeng/image.go changes JSON parsing and serialization from encoding/json to common.Unmarshal and common.Marshal. Issues #8013 and #8014 require payload-based billing count updat… Remove the common.Unmarshal and common.Marshal replacement. Keep the existing encoding/json calls and retain the payload counting, UpdateImageCount call, and regression tests.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: billing Imagen and Jimeng based on returned image payloads.
Linked Issues check Passed PR satisfies the coding requirements for #8013 and #8014. GeminiImageHandler counts nonblank, non-filtered base64 payloads and calls UpdateImageCount. jimengImageHandler counts nonblank URL and …
Docstring Coverage Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 4 files.
Full details: Linked Issues check

Explanation

PR satisfies the coding requirements for #8013 and #8014. GeminiImageHandler counts nonblank, non-filtered base64 payloads and calls UpdateImageCount. jimengImageHandler counts nonblank URL and base64 entries and uses the larger count. UpdateImageCount preserves the reserved quantity for zero or out-of-range counts and updates fixed-price and expression billing. The regression tests cover filtering, blank payloads, differing counts, duplicate representations, expression billing, boundary counts, and errors. The current-head changes add comments only and do not alter this implementation.

Full details: Out of Scope Changes check

Explanation

relay/channel/jimeng/image.go changes JSON parsing and serialization from encoding/json to common.Unmarshal and common.Marshal. Issues #8013 and #8014 require payload-based billing count updates. They do not require a JSON wrapper change, and the current wrapper still uses the standard-library codec. The change therefore adds no demonstrated behavior for either issue and is outside the linked scope.

Resolution

Remove the common.Unmarshal and common.Marshal replacement. Keep the existing encoding/json calls and retain the payload counting, UpdateImageCount call, and regression tests.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · 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

A rabbit counts each image bright,
Base64 and URLs in sight.
Blank payloads leave the bill,
Filtered predictions count not still.
Tests check each number right,
Then hop away beneath moonlight.

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

@seefs001 seefs001 closed this Oct 25, 2026
@seefs001 seefs001 reopened this Oct 25, 2026
@yxkong

yxkong commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai 已在 fa90a4f 补充两个图片 handler、转换函数和回归用例的说明,本次仅修改注释。

关于 JSON 入口:AGENTS.md 的 JSON package 规则要求根模块业务代码使用 common.Marshal/Unmarshal。当前 common/json.go 的 hostJSONCodec 仍调用标准库 encoding/json,此次没有切换编码引擎。保留统一入口符合项目规范;原有响应条目转换保持不变。

复核:go test -race -parallel=1 ./relay/channel/gemini ./relay/channel/jimeng -count=1 → 两包通过(2.463s / 2.735s)。gofmt、git diff --check 通过。默认并行整包 race 的既有 gin.SetMode 竞争仍在 PR 未验证范围中说明。

@coderabbitai

coderabbitai Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
git show --format=fuller --stat fa90a4f8f
git show --format= --no-ext-diff fa90a4f8f
fd -H '^AGENTS\.md$'
rg -n -C 5 'JSON|encoding/json|common\.' AGENTS.md
fd '^json\.go$' common
ast-grep outline common/json.go --items all
rg -n -C 5 'hostJSONCodec|json\.(Marshal|Unmarshal|NewEncoder|NewDecoder)' common/json.go

Length of output: 14909


@yxkong 已核对 fa90a4f8f,4 个文件的改动均为注释,未修改执行逻辑。

JSON 入口的说明正确,也与已有项目规范 Learning 一致:

  • AGENTS.md 要求根模块业务代码使用 common.* JSON 包装函数。
  • common/json.go 的 hostJSONCodec 当前仍调用标准库 encoding/json。统一入口不等于切换编码引擎,无需恢复直接调用。

你报告的串行 race 测试、gofmt 和 git diff --check 结果已说明本次验证范围。我未重新运行这些检查。串行 race 通过不代表默认并行 race 通过;保留该限制说明是准确的。


🧠 Learnings used
Learnt from: CR
Repo: QuantumNous/new-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-20T14:34:27.931Z
Learning: Source excerpt:
# AGENTS.md — Project Conventions for new-api

## Rules

### Backend Rules

Do NOT directly import or call `encoding/json` in business code. `json.RawMessage`, `json.Number`, and other type definitions from `encoding/json` may still be referenced as types, but actual marshal/unmarshal calls must go through `common.*`.
Learnt from: CR
Repo: QuantumNous/new-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-04-24T12:40:04.953Z
Learning: Applies to **/*.go : All JSON marshal/unmarshal operations MUST use wrapper functions from `common/json.go` (`common.Marshal()`, `common.Unmarshal()`, `common.UnmarshalJsonStr()`, `common.DecodeJson()`, `common.GetJsonType()`) instead of directly importing or calling `encoding/json` in business code. Exceptions: `json.RawMessage` and `json.Number` may still be referenced as types.

You are interacting with an AI system.

@yxkong

yxkong commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@coderabbitai 此前的复核回复已确认统一 JSON 入口符合 AGENTS.md,当前仍使用标准库编码,无需恢复直接调用。

摘要中的 Out of Scope Changes check 仍建议改回 encoding/json,与上述结论及仓库 JSON package 规则冲突。请据此复核并同步该项检查结论。此次保留 common.Marshal/Unmarshal,没有切换编码引擎,原有图片响应转换保持不变。

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor
🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
fd -H '^AGENTS\.md$'
rg -n -C 4 'encoding/json|JSON|common\.' AGENTS.md
fd '^json\.go$' common
ast-grep outline common/json.go --items all
rg -n -C 4 'hostJSONCodec|json\.(Marshal|Unmarshal)|func (Marshal|Unmarshal)' common/json.go
fd '^image\.go$' relay/channel/jimeng
ast-grep outline relay/channel/jimeng/image.go --items all
rg -n -C 5 'common\.(Marshal|Unmarshal)|encoding/json|ImageResponse|B64|Url|URL' relay/channel/jimeng/image.go
gh pr view 8015 --repo QuantumNous/new-api --json body --jq '.body'

Length of output: 18703


@yxkong 已复核。建议改回 encoding/json 的结论不成立,我撤回该建议。

  • AGENTS.md 明确要求根模块业务代码使用 common.* JSON 包装函数。
  • common/json.go 的 hostJSONCodec 仍调用标准库 encoding/json。此次统一入口不构成编码引擎切换。
  • relay/channel/jimeng/image.go 使用 common.Marshal/Unmarshal 符合该规则。图片响应转换仍分别保留 URL 和 base64 条目。

本次核对也与已有 JSON 规范 Learning 一致,无需新增规则。

我将触发摘要重新生成,并要求将 Out of Scope Changes check 中针对 JSON 入口的结论更正为:符合仓库规范,不应据此判定为范围外改动,无需恢复直接调用。 此操作不代表摘要已更新完成。


🧠 Learnings used
Learnt from: CR
Repo: QuantumNous/new-api PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-10-20T14:34:27.931Z
Learning: Source excerpt:
# AGENTS.md — Project Conventions for new-api

## Rules

### Backend Rules

Do NOT directly import or call `encoding/json` in business code. `json.RawMessage`, `json.Number`, and other type definitions from `encoding/json` may still be referenced as types, but actual marshal/unmarshal calls must go through `common.*`.
Learnt from: nerimoe
Repo: QuantumNous/new-api PR: 4936
File: middleware/gzip.go:69-74
Timestamp: 2026-05-18T07:33:49.556Z
Learning: In the QuantumNous/new-api repository (Go/Gin), the coding guideline requiring use of `common/json.go` wrapper functions for JSON marshal/unmarshal applies only to application-level serialization. It does NOT apply to Gin framework response helpers such as `c.AbortWithStatusJSON(...)`, `c.JSON(...)`, etc. Flagging those calls as violations of the guideline is a false positive.
✅ Action performed

Summary regenerated.

@yxkong

yxkong commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@seefs001 本 PR 合并修复 Imagen 和即梦的实际图片数量结算,关联 #8013、#8014。回归覆盖数量增减、过滤/空载荷、URL/base64 双表示及表达式计费数量。

最新提交 ecc66ddee,前后端 CI 均通过,当前无冲突。麻烦有空帮忙人工 review,确认后合并,谢谢。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants