fix(gateway): normalize model alias billing identity

This commit is contained in:
2026-05-28 01:19:28 +08:00
parent f5c69b9852
commit 2aeb47d6a5
4 changed files with 185 additions and 16 deletions
+72 -9
View File
@@ -203,14 +203,27 @@ SET frozen_balance = $2,
WHERE id = $1::uuid`, locked.ID, frozenAfter); err != nil {
return err
}
modelIdentity := taskBillingModelIdentity(GatewayTask{
Kind: task.Kind,
Model: task.Model,
RequestedModel: task.RequestedModel,
ResolvedModel: task.ResolvedModel,
Billings: billings,
Metrics: task.Metrics,
})
metadata, _ := json.Marshal(map[string]any{
"taskId": taskID,
"kind": task.Kind,
"model": task.Model,
"reserved": amount,
"balance": roundMoney(locked.Balance),
"frozenBefore": roundMoney(locked.FrozenBalance),
"frozenAfter": frozenAfter,
"taskId": taskID,
"kind": task.Kind,
"model": modelIdentity.Model,
"requestedModel": modelIdentity.RequestedModel,
"resolvedModel": modelIdentity.ResolvedModel,
"modelName": modelIdentity.ModelName,
"modelAlias": modelIdentity.ModelAlias,
"providerModel": modelIdentity.ProviderModelName,
"reserved": amount,
"balance": roundMoney(locked.Balance),
"frozenBefore": roundMoney(locked.FrozenBalance),
"frozenAfter": frozenAfter,
})
if _, err := tx.Exec(ctx, `
INSERT INTO gateway_wallet_transactions (
@@ -484,9 +497,11 @@ SELECT t.id::text, t.account_id::text, a.currency, COALESCE(t.gateway_tenant_id:
t.metadata || jsonb_strip_nulls(jsonb_build_object(
'taskId', task.id::text,
'kind', task.kind,
'model', task.model,
'model', COALESCE(NULLIF(platform_model.model_alias, ''), NULLIF(task.metrics->>'modelAlias', ''), NULLIF(task.resolved_model, ''), NULLIF(platform_model.model_name, ''), NULLIF(task.metrics->>'modelName', ''), task.model),
'requestedModel', task.requested_model,
'resolvedModel', task.resolved_model,
'resolvedModel', COALESCE(NULLIF(platform_model.model_alias, ''), NULLIF(task.metrics->>'modelAlias', ''), NULLIF(task.resolved_model, ''), NULLIF(platform_model.model_name, ''), NULLIF(task.metrics->>'modelName', '')),
'modelName', COALESCE(NULLIF(platform_model.model_name, ''), NULLIF(task.metrics->>'modelName', ''), NULLIF(task.resolved_model, '')),
'modelAlias', COALESCE(NULLIF(platform_model.model_alias, ''), NULLIF(task.metrics->>'modelAlias', '')),
'modelType', task.model_type,
'taskStatus', task.status,
'runMode', task.run_mode,
@@ -811,6 +826,54 @@ func walletFloat(value any) float64 {
}
}
type billingModelIdentity struct {
Model string
RequestedModel string
ResolvedModel string
ModelName string
ModelAlias string
ProviderModelName string
}
func taskBillingModelIdentity(task GatewayTask) billingModelIdentity {
modelAlias := firstNonEmpty(
taskBillingString(task.Metrics["modelAlias"]),
firstBillingLineString(task.Billings, "modelAlias"),
)
modelName := firstNonEmpty(
taskBillingString(task.Metrics["modelName"]),
taskBillingString(task.Metrics["resolvedModel"]),
task.ResolvedModel,
firstBillingLineString(task.Billings, "model"),
)
providerModelName := firstNonEmpty(
taskBillingString(task.Metrics["providerModel"]),
firstBillingLineString(task.Billings, "providerModel"),
)
systemModel := firstNonEmpty(modelAlias, modelName, task.ResolvedModel, task.Model)
return billingModelIdentity{
Model: systemModel,
RequestedModel: firstNonEmpty(task.RequestedModel, task.Model),
ResolvedModel: systemModel,
ModelName: modelName,
ModelAlias: modelAlias,
ProviderModelName: providerModelName,
}
}
func firstBillingLineString(billings []any, key string) string {
for _, raw := range billings {
line, _ := raw.(map[string]any)
if line == nil {
continue
}
if value := strings.TrimSpace(taskBillingString(line[key])); value != "" {
return value
}
}
return ""
}
func normalizeWalletCurrency(currency string) string {
currency = strings.TrimSpace(currency)
if currency == "" {