mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-09-22 09:58:00 +00:00
feat(opencode): 透传 completion_tokens_details.reasoning_tokens 与上游 cost
回答「opencodego 的用量与费用透传呢」时逐字段核对上游产出,发现 usage 漏了
一项、费用整项丢失。
## 上游实际发什么(实测 opencode.ai/zen/go/v1)
{
"choices": [...],
"usage": { "prompt_tokens": 37, "completion_tokens": 40, "total_tokens": 77,
"prompt_cache_hit_tokens": 0, "prompt_cache_miss_tokens": 37,
"prompt_tokens_details": {"cached_tokens": 0},
"completion_tokens_details": {"reasoning_tokens": 40} },
"cost": "0"
}
cost 在**顶层**且是**字符串**。流式时还会单独发一帧:
{"choices":[],"cost":"0"}
## 此前丢了两样
1. completion_tokens_details.reasoning_tokens —— 输出里有多少是思考 token。
没有它,客户端无法判断 completion_tokens 里多少是可见回答、多少是思考,
而两者都按输出计费。
2. cost —— 唯一的费用信号,网关整个丢弃。Go 订阅是包月制恒为 "0",
但 Zen 按量付费模型(以及未来的其它源)有信息量。
顺带修掉一处流式/非流式不一致:命中缓存时上游同时给
prompt_tokens_details.cached_tokens 和独立的 hit/miss,流式路径写成了 elseif,
只留 details,与非流式产出不同(只认独立字段的老客户端会看不到缓存)。
## 实现
- types.TokenUsage += CompletionTokensDetails;UnifiedResponse / UnifiedChunk += Cost
- opencodego/opencodezen 适配器映射两个字段;空 choices 帧改成 usage 与 cost
都可带(早退只带 usage 会把同帧的 cost 丢干净 —— 新测试先抓到的就是这个)
- Gateway ChatCompletion / ChatChunk += cost,随终帧发(对齐上游的
{"choices":[],"cost":"0"} 形态)
- Go 兜底 standardSSEChunk 同步支持(openai 系适配器不再漏 reasoning_tokens;
纯 cost 帧不再被整体丢弃),新增 rawCostString 兼容字符串/数字两种形态
费用只做**搬运**:不解析、不换算、不汇总 —— 它是上游事实,且只有部分上游提供。
## 验证
经网关实测 gozen:deepseek-v4.1-flash,流式与非流式产出逐字段一致:
prompt_tokens_details.cached_tokens=6784
prompt_cache_hit_tokens=6784 / miss=148
completion_tokens_details.reasoning_tokens=16
cost="0"
测试:TestOpenCodeCostAndReasoningPassthrough(含「无数据不得凭空造字段」反例)、
TestOpenCodeStreamCacheFieldsMatchNonStream、TestTokenUsageMarshalsCompletionTokensDetails。
This commit is contained in:
@ -1518,12 +1518,24 @@ func standardSSEChunk(data string) string {
|
||||
PromptTokensDetails *struct {
|
||||
CachedTokens int `json:"cached_tokens"`
|
||||
} `json:"prompt_tokens_details"`
|
||||
CompletionTokensDetails *struct {
|
||||
ReasoningTokens int `json:"reasoning_tokens"`
|
||||
} `json:"completion_tokens_details"`
|
||||
} `json:"usage"`
|
||||
// Cost is a top-level decimal string on OpenCode Zen/Go responses.
|
||||
Cost json.RawMessage `json:"cost"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(data), &raw); err != nil {
|
||||
return ""
|
||||
}
|
||||
if len(raw.Choices) == 0 && raw.UpstreamUsage.Total == 0 && raw.UpstreamUsage.TotalTokens == 0 {
|
||||
// Cost-only chunk: OpenCode sends the charge on its own frame
|
||||
// {"choices":[],"cost":"0"} with no usage at all. Dropping it here
|
||||
// would lose the only cost signal the client can see.
|
||||
if c := rawCostString(raw.Cost); c != "" {
|
||||
out, _ := json.Marshal(types.UnifiedChunk{Cost: c})
|
||||
return string(out)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
var usage *types.TokenUsage
|
||||
@ -1541,6 +1553,11 @@ func standardSSEChunk(data string) string {
|
||||
CachedTokens: pu.PromptTokensDetails.CachedTokens,
|
||||
}
|
||||
}
|
||||
if pu.CompletionTokensDetails != nil {
|
||||
usage.CompletionTokensDetails = &types.CompletionTokensDetails{
|
||||
ReasoningTokens: pu.CompletionTokensDetails.ReasoningTokens,
|
||||
}
|
||||
}
|
||||
}
|
||||
finish := ""
|
||||
done := false
|
||||
@ -1565,6 +1582,24 @@ func standardSSEChunk(data string) string {
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// rawCostString normalizes an upstream cost field into a plain string. The
|
||||
// value is a decimal **string** on OpenCode Zen/Go ("0", "0.0012"), but other
|
||||
// upstreams have been seen to send a bare number, so both are accepted.
|
||||
// Returns "" when absent, null, or empty.
|
||||
func rawCostString(raw json.RawMessage) string {
|
||||
s := strings.TrimSpace(string(raw))
|
||||
if s == "" || s == "null" {
|
||||
return ""
|
||||
}
|
||||
if s[0] == '"' {
|
||||
var unq string
|
||||
if json.Unmarshal(raw, &unq) == nil {
|
||||
return strings.TrimSpace(unq)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// pickFirst returns a if non-zero, else b (for usage keys that may appear in
|
||||
// either standard *_tokens or legacy short form).
|
||||
func pickFirst(a, b int) int {
|
||||
|
||||
Reference in New Issue
Block a user