修复模型引用与账单展示

This commit is contained in:
2026-06-15 00:17:20 +08:00
parent 10ec25d87b
commit 02ba5d3cdd
6 changed files with 146 additions and 47 deletions
+6 -6
View File
@@ -9,7 +9,7 @@ func TestNormalizeModelMatchKeyRemovesWhitespace(t *testing.T) {
}
}
func TestTaskBillingModelIdentityPrefersSystemAlias(t *testing.T) {
func TestTaskBillingModelIdentityKeepsRequestedModelPrimary(t *testing.T) {
identity := taskBillingModelIdentity(GatewayTask{
Model: "doubao-5.0 图像编辑",
RequestedModel: "doubao-5.0 图像编辑",
@@ -21,11 +21,11 @@ func TestTaskBillingModelIdentityPrefersSystemAlias(t *testing.T) {
},
})
if identity.Model != "doubao-5.0图像编辑" || identity.ResolvedModel != "doubao-5.0图像编辑" {
t.Fatalf("expected persisted model to use system alias, got %+v", identity)
if identity.Model != "doubao-5.0 图像编辑" || identity.RequestedModel != "doubao-5.0 图像编辑" {
t.Fatalf("expected persisted model to keep requested model, got %+v", identity)
}
if identity.RequestedModel != "doubao-5.0 图像编辑" {
t.Fatalf("expected requested model to preserve original request, got %+v", identity)
if identity.ResolvedModel != "doubao-5.0图像编辑" {
t.Fatalf("expected resolved model to keep system alias, got %+v", identity)
}
if identity.ModelName != "doubao-image-real" || identity.ProviderModelName != "doubao-provider-image" {
t.Fatalf("expected model name/provider model to stay available, got %+v", identity)
@@ -43,7 +43,7 @@ func TestTaskBillingModelIdentityFallsBackToBillingLines(t *testing.T) {
},
})
if identity.Model != "System Model Alias" || identity.ModelName != "system-model-name" {
if identity.Model != "front end alias" || identity.ModelName != "system-model-name" || identity.ResolvedModel != "System Model Alias" {
t.Fatalf("expected billing lines to provide system model identity, got %+v", identity)
}
}
+33 -32
View File
@@ -506,35 +506,35 @@ 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', 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', 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,
'requestId', COALESCE(task.request_id, attempt.request_id),
'apiKeyId', task.api_key_id,
'apiKeyName', task.api_key_name,
'apiKeyPrefix', task.api_key_prefix,
'provider', COALESCE(platform.provider, task.metrics->>'provider'),
'platformId', COALESCE(platform.id::text, task.metrics->>'platformId'),
'platformName', COALESCE(platform.name, task.metrics->>'platformName'),
'platformKey', platform.platform_key,
'platformModelId', COALESCE(platform_model.id::text, task.metrics->>'platformModelId'),
'platformModelName', platform_model.model_name,
'platformModelAlias', platform_model.model_alias,
'providerModel', COALESCE(platform_model.provider_model_name, task.metrics->>'providerModel'),
'clientId', attempt.client_id,
'usage', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.usage, attempt.usage, '{}'::jsonb) END,
'billings', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.billings, '[]'::jsonb) END,
'billingSummary', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.billing_summary, '{}'::jsonb) END,
'finalChargeAmount', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.final_charge_amount, 0)::float8 END,
'responseStartedAt', COALESCE(task.response_started_at::text, attempt.response_started_at::text),
'responseFinishedAt', COALESCE(task.response_finished_at::text, attempt.response_finished_at::text),
'responseDurationMs', COALESCE(task.response_duration_ms, attempt.response_duration_ms)
)), t.created_at
'model', COALESCE(NULLIF(task.requested_model, ''), NULLIF(task.model, ''), NULLIF(platform_model.model_alias, ''), NULLIF(task.metrics->>'modelAlias', ''), NULLIF(task.resolved_model, ''), NULLIF(platform_model.model_name, ''), NULLIF(task.metrics->>'modelName', '')),
'requestedModel', COALESCE(NULLIF(task.requested_model, ''), NULLIF(task.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,
'requestId', COALESCE(task.request_id, attempt.request_id),
'apiKeyId', task.api_key_id,
'apiKeyName', task.api_key_name,
'apiKeyPrefix', task.api_key_prefix,
'provider', COALESCE(platform.provider, task.metrics->>'provider'),
'platformId', COALESCE(platform.id::text, task.metrics->>'platformId'),
'platformName', COALESCE(platform.name, task.metrics->>'platformName'),
'platformKey', platform.platform_key,
'platformModelId', COALESCE(platform_model.id::text, task.metrics->>'platformModelId'),
'platformModelName', platform_model.model_name,
'platformModelAlias', platform_model.model_alias,
'providerModel', COALESCE(platform_model.provider_model_name, task.metrics->>'providerModel'),
'clientId', attempt.client_id,
'usage', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.usage, attempt.usage, '{}'::jsonb) END,
'billings', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.billings, '[]'::jsonb) END,
'billingSummary', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.billing_summary, '{}'::jsonb) END,
'finalChargeAmount', CASE WHEN task.id IS NULL THEN NULL ELSE COALESCE(task.final_charge_amount, 0)::float8 END,
'responseStartedAt', COALESCE(task.response_started_at::text, attempt.response_started_at::text),
'responseFinishedAt', COALESCE(task.response_finished_at::text, attempt.response_finished_at::text),
'responseDurationMs', COALESCE(task.response_duration_ms, attempt.response_duration_ms)
)), t.created_at
FROM gateway_wallet_transactions t
JOIN gateway_wallet_accounts a ON a.id = t.account_id
LEFT JOIN gateway_tasks task ON t.reference_type = 'gateway_task' AND t.reference_id = task.id::text
@@ -953,10 +953,11 @@ func taskBillingModelIdentity(task GatewayTask) billingModelIdentity {
taskBillingString(task.Metrics["providerModel"]),
firstBillingLineString(task.Billings, "providerModel"),
)
systemModel := firstNonEmpty(modelAlias, modelName, task.ResolvedModel, task.Model)
requestedModel := firstNonEmpty(task.RequestedModel, task.Model)
systemModel := firstNonEmpty(modelAlias, modelName, task.ResolvedModel, requestedModel)
return billingModelIdentity{
Model: systemModel,
RequestedModel: firstNonEmpty(task.RequestedModel, task.Model),
Model: firstNonEmpty(requestedModel, systemModel),
RequestedModel: requestedModel,
ResolvedModel: systemModel,
ModelName: modelName,
ModelAlias: modelAlias,