feat(*): 模型计费与渠道目录 - 支持视频 Token 报价结算和稳定模型展示
This commit is contained in:
1 parent
90add8c466
commit
1a79005d25
57 files changed
+2904
-341
No files matched your search
@@ -1035,8 +1035,12 @@ func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[str
|
||||
log.Error = errorMessage
|
||||
}
|
||||
}
|
||||
arkVideo := log.Capability == "video" && strings.Contains(log.Path, "/contents/generations/tasks")
|
||||
usage, _ := payload["usage"].(map[string]any)
|
||||
if usage != nil {
|
||||
if log.Capability == "video" {
|
||||
log.InputTokens, log.CachedTokens = 0, 0
|
||||
log.OutputTokens, log.UsageAvailable = videoCompletionTokens(payload, arkVideo)
|
||||
} else if usage != nil {
|
||||
inputTokens, inputAvailable := firstInt64Value(usage, "input_tokens", "prompt_tokens")
|
||||
outputTokens, outputAvailable := firstInt64Value(usage, "output_tokens", "completion_tokens")
|
||||
if inputAvailable {
|
||||
@@ -1048,14 +1052,6 @@ func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[str
|
||||
if inputAvailable || outputAvailable {
|
||||
log.UsageAvailable = true
|
||||
}
|
||||
// 火山方舟视频的输入 Token 恒为 0;查询任务以 completion_tokens 为实际用量,
|
||||
// 兼容只返回 total_tokens 的同协议中转实现。
|
||||
if log.Capability == "video" && strings.Contains(log.Path, "/contents/generations/tasks") {
|
||||
if log.OutputTokens == 0 {
|
||||
log.OutputTokens = firstInt64(usage, "total_tokens")
|
||||
}
|
||||
log.UsageAvailable = log.OutputTokens > 0
|
||||
}
|
||||
if details, ok := usage["input_tokens_details"].(map[string]any); ok {
|
||||
log.CachedTokens = firstInt64(details, "cached_tokens", "cache_read_input_tokens")
|
||||
}
|
||||
@@ -1066,7 +1062,7 @@ func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[str
|
||||
log.CachedTokens = firstInt64(usage, "cached_tokens", "cache_read_input_tokens", "prompt_cache_hit_tokens")
|
||||
}
|
||||
}
|
||||
if usageMetadata, ok := payload["usageMetadata"].(map[string]any); ok {
|
||||
if usageMetadata, ok := payload["usageMetadata"].(map[string]any); ok && log.Capability != "video" {
|
||||
inputTokens, inputAvailable := firstInt64Value(usageMetadata, "promptTokenCount")
|
||||
outputTokens, outputAvailable := firstInt64Value(usageMetadata, "candidatesTokenCount")
|
||||
if inputAvailable {
|
||||
@@ -1101,12 +1097,40 @@ func (s *Service) enrichAPICallLogPayload(log *model.ApiCallLog, payload map[str
|
||||
}
|
||||
}
|
||||
|
||||
func videoCompletionTokens(payload map[string]any, arkProtocol bool) (int64, bool) {
|
||||
if status, exists := payload["status"]; exists {
|
||||
text, ok := status.(string)
|
||||
status := strings.ToLower(strings.TrimSpace(text))
|
||||
if !ok || (status != "succeeded" && (arkProtocol || (status != "completed" && status != "success"))) {
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
usage, _ := payload["usage"].(map[string]any)
|
||||
value, exists := usage["completion_tokens"]
|
||||
if !exists && !arkProtocol {
|
||||
value, exists = usage["output_tokens"]
|
||||
}
|
||||
if !exists {
|
||||
value = usage["total_tokens"]
|
||||
}
|
||||
// completion_tokens 一旦返回就是唯一结算依据,非法值不能用 total_tokens 掩盖。
|
||||
// JSON Number 保留整数精度,避免浮点取整把小数或超出 int64 的值变成可计费用量。
|
||||
number, ok := value.(json.Number)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
tokens, err := number.Int64()
|
||||
if err != nil || tokens <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return tokens, true
|
||||
}
|
||||
|
||||
func providerResponsePayloads(responseBody []byte) []map[string]any {
|
||||
if len(responseBody) == 0 {
|
||||
return nil
|
||||
}
|
||||
var payload map[string]any
|
||||
if json.Unmarshal(responseBody, &payload) == nil {
|
||||
if payload, ok := decodeProviderResponsePayload(responseBody); ok {
|
||||
return []map[string]any{payload}
|
||||
}
|
||||
|
||||
@@ -1121,8 +1145,7 @@ func providerResponsePayloads(responseBody []byte) []map[string]any {
|
||||
if raw == "" || raw == "[DONE]" {
|
||||
return
|
||||
}
|
||||
var event map[string]any
|
||||
if json.Unmarshal([]byte(raw), &event) == nil {
|
||||
if event, ok := decodeProviderResponsePayload([]byte(raw)); ok {
|
||||
result = append(result, event)
|
||||
}
|
||||
}
|
||||
@@ -1140,6 +1163,17 @@ func providerResponsePayloads(responseBody []byte) []map[string]any {
|
||||
return result
|
||||
}
|
||||
|
||||
func decodeProviderResponsePayload(data []byte) (map[string]any, bool) {
|
||||
if !json.Valid(data) {
|
||||
return nil, false
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
var payload map[string]any
|
||||
err := decoder.Decode(&payload)
|
||||
return payload, err == nil && payload != nil
|
||||
}
|
||||
|
||||
func providerRequestIDFromPath(path string) string {
|
||||
parts := strings.Split(strings.Trim(strings.TrimSpace(path), "/"), "/")
|
||||
for index := len(parts) - 1; index >= 0; index-- {
|
||||
|
||||
Reference in new issue
Block a user