mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
fix(accounts): resolve plan display names in the backend
Map OpenAI plan identifiers to official desktop labels in the provider. Expose planTypeDisplay in account and dashboard responses while preserving raw planType values, and render backend labels directly in the frontend. Show missing plans as unknown and retain raw identifiers in badge tooltips. Validation: Admin/API tests (368 passed), OpenAI admin tests (29 passed), strict Clippy for affected backend crates with all targets and features, frontend production build, targeted ESLint, and diff whitespace checks. Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
@@ -471,6 +471,8 @@ pub struct DashboardAccountUsage {
|
||||
pub name: String,
|
||||
pub email: Option<String>,
|
||||
pub plan_type: Option<String>,
|
||||
/// Admin 按 Provider 补齐的套餐展示名称。
|
||||
pub plan_type_display: Option<String>,
|
||||
pub request_count: u64,
|
||||
pub success_count: u64,
|
||||
pub input_tokens: Option<u64>,
|
||||
|
||||
@@ -1019,6 +1019,8 @@ impl fmt::Debug for ProviderExport {
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct AccountDirectoryItem {
|
||||
pub account: AccountRecord,
|
||||
/// Provider 提供的套餐展示名称;未识别到套餐时为空。
|
||||
pub plan_type_display: Option<String>,
|
||||
pub projection: gateway_core::account::AccountStatusProjection,
|
||||
pub usage: Option<super::accounts::AccountUsage>,
|
||||
pub quota: ProviderQuota,
|
||||
|
||||
@@ -91,6 +91,11 @@ impl ProviderAdminError {
|
||||
pub trait ProviderAdmin: Send + Sync {
|
||||
fn provider_kind(&self) -> &ProviderKind;
|
||||
|
||||
/// 将原始套餐值投影为展示名称;默认保留未知 Provider 的原始名称。
|
||||
fn plan_type_display(&self, plan_type: &str) -> String {
|
||||
plan_type.to_owned()
|
||||
}
|
||||
|
||||
/// 账号已经由控制面提交为不可调度状态,释放 Provider 持有的账号级运行时资源。
|
||||
///
|
||||
/// 无账号级运行时资源的 Provider 不需要执行额外操作。该通知发生在 Store 事务
|
||||
@@ -230,6 +235,24 @@ impl ProviderAdminRegistry {
|
||||
.ok_or_else(|| ProviderAdminError::new(ProviderAdminErrorKind::Unsupported))
|
||||
}
|
||||
|
||||
/// 账号页和 Dashboard 共用的套餐展示投影,不修改持久化的原始套餐值。
|
||||
pub(crate) fn plan_type_display(
|
||||
&self,
|
||||
provider_kind: &str,
|
||||
plan_type: Option<&str>,
|
||||
) -> Option<String> {
|
||||
let plan_type = plan_type
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty() && !value.eq_ignore_ascii_case("unknown"))?;
|
||||
let provider = ProviderKind::new(provider_kind.to_owned())
|
||||
.ok()
|
||||
.and_then(|kind| self.providers.get(&kind));
|
||||
Some(provider.map_or_else(
|
||||
|| plan_type.to_owned(),
|
||||
|provider| provider.plan_type_display(plan_type),
|
||||
))
|
||||
}
|
||||
|
||||
/// 返回所有已注册 Provider 的 Dashboard 上游身份画像。
|
||||
pub fn dashboard_wire_profiles(&self) -> Vec<DashboardWireProfile> {
|
||||
self.providers
|
||||
|
||||
@@ -281,6 +281,10 @@ impl DefaultAccountsService {
|
||||
.await?;
|
||||
let usage = quota.representative_window_usage().cloned();
|
||||
Ok(AccountDirectoryItem {
|
||||
plan_type_display: self.providers.plan_type_display(
|
||||
stored.account.provider_kind.as_str(),
|
||||
stored.account.plan_type.as_deref(),
|
||||
),
|
||||
projection: stored.projection,
|
||||
usage,
|
||||
account: stored.account,
|
||||
@@ -369,6 +373,10 @@ impl AccountsService for DefaultAccountsService {
|
||||
.map(|(item, quota)| {
|
||||
let usage = quota.representative_window_usage().cloned();
|
||||
AccountDirectoryItem {
|
||||
plan_type_display: self.providers.plan_type_display(
|
||||
item.account.provider_kind.as_str(),
|
||||
item.account.plan_type.as_deref(),
|
||||
),
|
||||
usage,
|
||||
account: item.account,
|
||||
projection: item.projection,
|
||||
|
||||
@@ -162,6 +162,11 @@ impl DefaultObservabilityService {
|
||||
)
|
||||
.map_err(|error| map_store_error(error, "dashboard"))?;
|
||||
self.enrich_list_billing(&mut observation.recent_requests);
|
||||
for account in &mut observation.account_usage {
|
||||
account.plan_type_display = self
|
||||
.providers
|
||||
.plan_type_display(&account.provider_kind, account.plan_type.as_deref());
|
||||
}
|
||||
self.enrich_dashboard_quotas(&mut observation.account_usage)
|
||||
.await;
|
||||
let today_start = china_day_start(observation.range.end);
|
||||
|
||||
@@ -201,6 +201,10 @@ impl ProviderAdmin for FakeProviderAdmin {
|
||||
&self.kind
|
||||
}
|
||||
|
||||
fn plan_type_display(&self, plan_type: &str) -> String {
|
||||
format!("{} display: {plan_type}", self.kind)
|
||||
}
|
||||
|
||||
async fn account_unavailable(&self, _: &ProviderAccountId) {
|
||||
self.record("provider.account_unavailable");
|
||||
}
|
||||
@@ -1320,9 +1324,11 @@ async fn accounts_batch_update_should_commit_once_and_notify_each_provider() {
|
||||
#[tokio::test]
|
||||
async fn accounts_list_should_return_complete_directory_semantics() {
|
||||
let provider = FakeProviderAdmin::new("openai", events());
|
||||
let store = FakeAccountStore::new("openai", events());
|
||||
let page = accounts_service(provider, store)
|
||||
.await
|
||||
let mut stored = account_record("openai");
|
||||
stored.plan_type = Some(" self_serve_business_prolite ".to_owned());
|
||||
let store = FakeAccountStore::with_account(stored, events());
|
||||
let services = accounts_service(provider, store).await;
|
||||
let page = services
|
||||
.accounts()
|
||||
.list(AccountListQuery {
|
||||
page: 1,
|
||||
@@ -1344,6 +1350,27 @@ async fn accounts_list_should_return_complete_directory_semantics() {
|
||||
account.projection.status,
|
||||
gateway_admin::model::accounts::AccountStatus::Normal
|
||||
);
|
||||
assert_eq!(
|
||||
(
|
||||
account.account.plan_type.as_deref(),
|
||||
account.plan_type_display.as_deref(),
|
||||
),
|
||||
(
|
||||
Some(" self_serve_business_prolite "),
|
||||
Some("openai display: self_serve_business_prolite"),
|
||||
)
|
||||
);
|
||||
|
||||
let detail = services
|
||||
.accounts()
|
||||
.quota(
|
||||
&ProviderAccountId::new("acct_test").expect("account ID"),
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.expect("account detail");
|
||||
assert_eq!(detail.plan_type_display, account.plan_type_display);
|
||||
assert_eq!(detail.account.plan_type, account.account.plan_type);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
@@ -59,6 +59,7 @@ pub(super) fn account_models_data(result: ProviderModels) -> AccountModelsData {
|
||||
pub(super) fn account_view(item: AccountDirectoryItem, now: DateTime<Utc>) -> AccountView {
|
||||
let AccountDirectoryItem {
|
||||
account,
|
||||
plan_type_display,
|
||||
projection,
|
||||
usage,
|
||||
quota,
|
||||
@@ -89,6 +90,7 @@ pub(super) fn account_view(item: AccountDirectoryItem, now: DateTime<Utc>) -> Ac
|
||||
user_id: account.upstream_user_id,
|
||||
label: None,
|
||||
plan_type: account.plan_type,
|
||||
plan_type_display: plan_type_display.unwrap_or_else(|| "未知套餐".to_owned()),
|
||||
authentication_kind: account.authentication_kind,
|
||||
has_refresh_token: account.has_refresh_token,
|
||||
status,
|
||||
|
||||
@@ -202,6 +202,8 @@ pub struct AccountView {
|
||||
pub user_id: Option<String>,
|
||||
pub label: Option<String>,
|
||||
pub plan_type: Option<String>,
|
||||
/// 后端生成的套餐展示名称;缺失套餐时为“未知套餐”。
|
||||
pub plan_type_display: String,
|
||||
pub authentication_kind: String,
|
||||
pub has_refresh_token: bool,
|
||||
pub status: String,
|
||||
|
||||
@@ -900,6 +900,10 @@ pub(crate) fn dashboard_view(
|
||||
.clone()
|
||||
.unwrap_or_else(|| credential.name.clone()),
|
||||
plan_type: credential.plan_type.clone(),
|
||||
plan_type_display: credential
|
||||
.plan_type_display
|
||||
.clone()
|
||||
.unwrap_or_else(|| "未知套餐".to_owned()),
|
||||
tokens: credential
|
||||
.total_tokens
|
||||
.map_or_else(|| "—".to_owned(), format_compact_number),
|
||||
|
||||
@@ -373,6 +373,7 @@ pub struct DashboardAccountUsageView {
|
||||
pub authentication_kind: String,
|
||||
pub email: String,
|
||||
pub plan_type: Option<String>,
|
||||
pub plan_type_display: String,
|
||||
pub tokens: String,
|
||||
pub request_count: u64,
|
||||
pub request_buckets: Vec<DashboardAccountRequestBucketView>,
|
||||
|
||||
@@ -64,6 +64,7 @@ fn dashboard_account_usage_should_keep_daily_request_timeline() {
|
||||
authentication_kind: "oauth".to_owned(),
|
||||
email: "account@example.com".to_owned(),
|
||||
plan_type: Some("free".to_owned()),
|
||||
plan_type_display: "Free".to_owned(),
|
||||
tokens: "—".to_owned(),
|
||||
request_count: 3,
|
||||
request_buckets: vec![DashboardAccountRequestBucketView {
|
||||
|
||||
@@ -293,6 +293,7 @@ pub(crate) fn admin_dashboard_account_usage(
|
||||
name: usage.name,
|
||||
email: usage.email,
|
||||
plan_type: usage.plan_type,
|
||||
plan_type_display: None,
|
||||
request_count: usage.request_count,
|
||||
success_count: usage.success_count,
|
||||
input_tokens: usage.input_tokens,
|
||||
|
||||
@@ -166,6 +166,30 @@ impl ProviderAdmin for OpenAiAdminProvider {
|
||||
&self.provider_kind
|
||||
}
|
||||
|
||||
fn plan_type_display(&self, plan_type: &str) -> String {
|
||||
// 与官方 Desktop 的套餐名称映射一致;原始子类型继续由 plan_type 保留。
|
||||
match plan_type.to_ascii_lowercase().as_str() {
|
||||
"free" | "free_workspace" | "guest" => "Free",
|
||||
"go" => "Go",
|
||||
"plus" => "Plus",
|
||||
"pro" | "prolite" => "Pro",
|
||||
"team" | "self_serve_business_prolite" | "self_serve_business_usage_based" => {
|
||||
"Business"
|
||||
}
|
||||
"business"
|
||||
| "ent26"
|
||||
| "enterprise_cbp_automation"
|
||||
| "enterprise_cbp_usage_based"
|
||||
| "enterprise"
|
||||
| "hc" => "Enterprise",
|
||||
"edu" | "education" => "Edu",
|
||||
"edu_plus" => "Edu Plus",
|
||||
"edu_pro" => "Edu Pro",
|
||||
_ => plan_type,
|
||||
}
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
async fn account_unavailable(&self, account_id: &ProviderAccountId) {
|
||||
self.websocket_pool.evict_account(account_id.as_str()).await;
|
||||
}
|
||||
|
||||
@@ -197,6 +197,10 @@ Responses wire 之间的协议转换层,转换只在 xAI Provider 内完成。
|
||||
- `sortBy`: `email`、`status`、`planType`、`usage`、`lastUsedAt`、`expiresAt`;
|
||||
- `sortDirection`: `asc`、`desc`。
|
||||
|
||||
账号视图和 Dashboard 账号概览中的 `planType` 保留原始套餐值;`planTypeDisplay` 是后端按 Provider 生成的展示名称,
|
||||
前端直接展示该字段。OpenAI 的 `self_serve_business_prolite` 等 Team 套餐按官方客户端显示为
|
||||
`Business`;新套餐保留原始名称,空套餐或 `unknown` 显示为“未知套餐”。
|
||||
|
||||
### 账号连接测试 SSE
|
||||
|
||||
`GET /api/admin/accounts/connection-test` 固定探测请求指定的账号,不参与普通账号轮换。成功流沿用
|
||||
|
||||
@@ -119,6 +119,7 @@ export interface Account {
|
||||
userId: string | null
|
||||
label: string | null
|
||||
planType: string | null
|
||||
planTypeDisplay: string
|
||||
authenticationKind: string
|
||||
hasRefreshToken: boolean
|
||||
status: AccountStatus
|
||||
|
||||
@@ -142,6 +142,7 @@ export interface DashboardAccountUsage {
|
||||
authenticationKind: string
|
||||
email: string
|
||||
planType: string | null
|
||||
planTypeDisplay: string
|
||||
tokens: string
|
||||
requestCount: number
|
||||
requestBuckets: DashboardAccountRequestBucket[]
|
||||
|
||||
@@ -48,7 +48,7 @@ const selectedGroupIds = defineModel<string[]>('selectedGroupIds', { required: t
|
||||
size="lg"
|
||||
/>
|
||||
<div class="flex shrink-0 items-center gap-3">
|
||||
<AccountPlanBadge :plan-type="account.planType" size="sm" />
|
||||
<AccountPlanBadge :plan-type="account.planType" :plan-type-display="account.planTypeDisplay" size="sm" />
|
||||
<ProviderIconGroup
|
||||
:provider="account.provider"
|
||||
:authentication-kind="account.authenticationKind"
|
||||
|
||||
@@ -6,7 +6,7 @@ import { stablePresetVisualToneClass } from '../utils/visualTone'
|
||||
import AccountPlanBadge from './AccountPlanBadge.vue'
|
||||
|
||||
type AccountRow = Awaited<ReturnType<typeof getAccounts>>['items'][number]
|
||||
type AccountIdentity = Pick<AccountRow, 'id' | 'email' | 'planType'>
|
||||
type AccountIdentity = Pick<AccountRow, 'id' | 'email' | 'planType' | 'planTypeDisplay'>
|
||||
& Partial<Pick<AccountRow, 'accountId'>>
|
||||
|
||||
const props = withDefaults(
|
||||
@@ -83,7 +83,7 @@ const avatarToneClass = computed(() => {
|
||||
:class="metaGapClass"
|
||||
>
|
||||
<slot name="meta" />
|
||||
<AccountPlanBadge v-if="showPlan" :plan-type="account.planType" :size="metaSize" />
|
||||
<AccountPlanBadge v-if="showPlan" :plan-type="account.planType" :plan-type-display="account.planTypeDisplay" :size="metaSize" />
|
||||
</span>
|
||||
</div>
|
||||
<div
|
||||
@@ -92,7 +92,7 @@ const avatarToneClass = computed(() => {
|
||||
:class="metaGapClass"
|
||||
>
|
||||
<slot name="meta" />
|
||||
<AccountPlanBadge v-if="showPlan" :plan-type="account.planType" :size="metaSize" />
|
||||
<AccountPlanBadge v-if="showPlan" :plan-type="account.planType" :plan-type-display="account.planTypeDisplay" :size="metaSize" />
|
||||
</div>
|
||||
<div v-else-if="secondaryText" class="truncate font-emphasis" :class="secondaryClass">
|
||||
{{ secondaryText }}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { stableVisualIndex } from '../utils/visualTone'
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
planType?: string | null
|
||||
planTypeDisplay: string
|
||||
size?: 'xs' | 'sm' | 'md'
|
||||
}>(),
|
||||
{
|
||||
@@ -27,7 +28,7 @@ const fallbackPalettes = [
|
||||
'bg-cp-orange-container text-cp-orange-on-container',
|
||||
] as const
|
||||
|
||||
const label = computed(() => props.planType?.trim() || 'Free')
|
||||
const rawPlanType = computed(() => props.planType?.trim() || '')
|
||||
|
||||
const sizeClass = computed(() => {
|
||||
if (props.size === 'xs')
|
||||
@@ -38,7 +39,7 @@ const sizeClass = computed(() => {
|
||||
})
|
||||
|
||||
const paletteClass = computed(() => {
|
||||
const key = label.value.toLowerCase()
|
||||
const key = rawPlanType.value.toLowerCase()
|
||||
const planPalette = planPalettes[key]
|
||||
if (planPalette)
|
||||
return planPalette
|
||||
@@ -49,9 +50,10 @@ const paletteClass = computed(() => {
|
||||
|
||||
<template>
|
||||
<span
|
||||
class="inline-flex shrink-0 items-center justify-center whitespace-nowrap leading-none capitalize shadow-cp-tertiary"
|
||||
class="inline-flex shrink-0 items-center justify-center whitespace-nowrap leading-none shadow-cp-tertiary"
|
||||
:class="[sizeClass, paletteClass]"
|
||||
:title="rawPlanType || undefined"
|
||||
>
|
||||
<span>{{ label }}</span>
|
||||
<span>{{ planTypeDisplay }}</span>
|
||||
</span>
|
||||
</template>
|
||||
|
||||
@@ -116,7 +116,7 @@ function formatDuration(value: number | null) {
|
||||
<h3 id="profile-display-name" class="m-0 min-w-0 truncate text-cp-lg leading-tight font-heavy text-cp-text">
|
||||
{{ displayName }}
|
||||
</h3>
|
||||
<AccountPlanBadge v-if="account.planType" :plan-type="account.planType" size="sm" />
|
||||
<AccountPlanBadge v-if="account.planType" :plan-type="account.planType" :plan-type-display="account.planTypeDisplay" size="sm" />
|
||||
</div>
|
||||
<div
|
||||
v-if="username || accountIdentity"
|
||||
|
||||
@@ -39,7 +39,7 @@ const profileOpen = shallowRef(false)
|
||||
<span>{{ account.provider === 'xai' ? 'xAI 用量窗口' : 'Codex 额度' }}</span>
|
||||
<template v-if="account.provider === 'openai'">
|
||||
<span>·</span>
|
||||
<AccountPlanBadge :plan-type="account.planType" size="sm" />
|
||||
<AccountPlanBadge :plan-type="account.planType" :plan-type-display="account.planTypeDisplay" size="sm" />
|
||||
</template>
|
||||
<span>·</span>
|
||||
<span>最近刷新: {{ account.quota.refreshedAtDisplay }}</span>
|
||||
|
||||
@@ -253,7 +253,7 @@ const {
|
||||
</template>
|
||||
|
||||
<template #planType="{ row }">
|
||||
<AccountPlanBadge :plan-type="row.planType" />
|
||||
<AccountPlanBadge :plan-type="row.planType" :plan-type-display="row.planTypeDisplay" />
|
||||
</template>
|
||||
|
||||
<template #usage="{ row }">
|
||||
|
||||
@@ -65,7 +65,7 @@ const avatarToneClass = computed(() =>
|
||||
<strong class="min-w-0 truncate text-cp leading-[1.15] font-heavy text-cp-text">
|
||||
{{ displayTitle }}
|
||||
</strong>
|
||||
<AccountPlanBadge :plan-type="account.planType" size="xs" />
|
||||
<AccountPlanBadge :plan-type="account.planType" :plan-type-display="account.planTypeDisplay" size="xs" />
|
||||
</span>
|
||||
<span
|
||||
class="mt-0.5 block min-w-0 truncate font-mono text-cp-xs font-emphasis text-cp-text-quaternary"
|
||||
|
||||
@@ -297,6 +297,7 @@ export const themeDashboardSummary: DashboardSummaryResponse = {
|
||||
authenticationKind: 'oauth',
|
||||
email: 'relay@example.com',
|
||||
planType: 'team',
|
||||
planTypeDisplay: 'Business',
|
||||
tokens: '926K',
|
||||
requestCount: 1_284,
|
||||
requestBuckets: [],
|
||||
@@ -325,6 +326,7 @@ export const themeDashboardSummary: DashboardSummaryResponse = {
|
||||
authenticationKind: 'oauth',
|
||||
email: 'gateway@example.com',
|
||||
planType: 'free',
|
||||
planTypeDisplay: 'Free',
|
||||
tokens: '684K',
|
||||
requestCount: 894,
|
||||
requestBuckets: requestSeries.slice(-12).map((requestCount, index) => ({
|
||||
@@ -364,6 +366,7 @@ export const themeDashboardSummary: DashboardSummaryResponse = {
|
||||
authenticationKind: 'oauth',
|
||||
email: 'control@example.com',
|
||||
planType: 'pro',
|
||||
planTypeDisplay: 'Pro',
|
||||
tokens: '512K',
|
||||
requestCount: 742,
|
||||
requestBuckets: [],
|
||||
|
||||
Reference in New Issue
Block a user