Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): Gateway 折扣落到报价口径并修正免费判定与支付契约注释

Lizi9b0f1f2ac95f
diff --git a/apps/desktop/src/main/maker-host/__tests__/activeCatalogXdAvailability.test.ts b/apps/desktop/src/main/maker-host/__tests__/activeCatalogXdAvailability.test.ts
index 4a359d5ca0..ed25425259 100644
--- a/apps/desktop/src/main/maker-host/__tests__/activeCatalogXdAvailability.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/activeCatalogXdAvailability.test.ts
@@ -176,6 +176,20 @@ describe('XD 网关权威模型清单重建', () => {
id: 'missing-output',
inputCostPerToken: 0.000012,
},
+ {
+ id: 'cached-half-price',
+ costDiscount: 0.5,
+ inputCostPerToken: 0.000012,
+ outputCostPerToken: 0.000036,
+ cacheReadInputTokenCost: 0.000012,
+ cacheCreationInputTokenCost: 0.000036,
+ },
+ {
+ id: 'cache-only-priced',
+ inputCostPerToken: 0,
+ outputCostPerToken: 0,
+ cacheReadInputTokenCost: 0.000012,
+ },
]);
const cc = xdModels('claude-code');
@@ -200,6 +214,19 @@ describe('XD 网关权威模型清单重建', () => {
output: 36,
});
expect(cc.find((m) => m.id === 'missing-output')?.cost).toBeUndefined();
+ // 缓存维度随折扣一起投影;只免标准价、缓存带价的模型 cost 会暴露缓存价,
+ // 供展示层拒绝「免费」误标。
+ expect(cc.find((m) => m.id === 'cached-half-price')?.cost).toEqual({
+ input: 6,
+ output: 18,
+ cacheRead: 6,
+ cacheWrite: 18,
+ });
+ expect(cc.find((m) => m.id === 'cache-only-priced')?.cost).toEqual({
+ input: 0,
+ output: 0,
+ cacheRead: 12,
+ });
});
it('非法 effort 档位被白名单过滤;defaultEffort 不在档位集内时回落 high 规则', () => {
diff --git a/apps/desktop/src/main/maker-host/active-catalog.ts b/apps/desktop/src/main/maker-host/active-catalog.ts
index a5d3f7f2f5..007c6df673 100644
--- a/apps/desktop/src/main/maker-host/active-catalog.ts
+++ b/apps/desktop/src/main/maker-host/active-catalog.ts
@@ -66,6 +66,9 @@ export interface XdGatewayModelInfo {
/** AIGateway 标准 token 单价(per token)。 */
inputCostPerToken?: number;
outputCostPerToken?: number;
+ /** AIGateway 缓存 token 单价(per token);参与「免费」判定,防止只免标准价的模型误标免费。 */
+ cacheReadInputTokenCost?: number;
+ cacheCreationInputTokenCost?: number;
/** 进哪些 runtime tab;缺省 = 仅 claude-code 兜底。 */
agents?: AgentKind[];
name?: string;
@@ -119,6 +122,10 @@ const VALID_EFFORTS: ReadonlySet<string> = new Set([
type Effort = CatalogModel['efforts'][number];
+function nonNegativeFiniteOrUndefined(value: number | undefined): number | undefined {
+ return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : undefined;
+}
+
function effectiveGatewayModelCost(model: XdGatewayModelInfo): CatalogModel['cost'] | undefined {
const input = model.inputCostPerToken;
const output = model.outputCostPerToken;
@@ -140,9 +147,13 @@ function effectiveGatewayModelCost(model: XdGatewayModelInfo): CatalogModel['cos
? model.costDiscount
: 0;
const multiplier = 1 - discount;
+ const cacheRead = nonNegativeFiniteOrUndefined(model.cacheReadInputTokenCost);
+ const cacheWrite = nonNegativeFiniteOrUndefined(model.cacheCreationInputTokenCost);
return {
input: input * 1_000_000 * multiplier,
output: output * 1_000_000 * multiplier,
+ ...(cacheRead !== undefined ? { cacheRead: cacheRead * 1_000_000 * multiplier } : {}),
+ ...(cacheWrite !== undefined ? { cacheWrite: cacheWrite * 1_000_000 * multiplier } : {}),
};
}
diff --git a/apps/desktop/src/main/usage/__tests__/modelPricing.test.ts b/apps/desktop/src/main/usage/__tests__/modelPricing.test.ts
index e75814400b..8cc35b7fa1 100644
--- a/apps/desktop/src/main/usage/__tests__/modelPricing.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/modelPricing.test.ts
@@ -156,6 +156,76 @@ describe('gateway model pricing projection', () => {
expect(mocks.send).toHaveBeenCalledWith(MODEL_PRICING_CHANGED_CHANNEL, pricing);
});
+ it('applies gateway costDiscount to quotes and keeps originals for display', async () => {
+ const pricing = replaceGatewayModelPricing([
+ {
+ id: 'half-price',
+ costDiscount: 0.5,
+ inputCostPerToken: 0.000012,
+ outputCostPerToken: 0.000036,
+ cacheReadInputTokenCost: 0.000012,
+ },
+ {
+ id: 'invalid-discount',
+ costDiscount: 1.2,
+ inputCostPerToken: 0.000012,
+ outputCostPerToken: 0.000036,
+ },
+ {
+ // codex/ 折扣路由的 0.15 乘数是客户端权威,不与服务端 costDiscount 叠加。
+ id: 'codex/gpt-5.5',
+ costDiscount: 0.5,
+ inputCostPerToken: 0.00002,
+ outputCostPerToken: 0.00004,
+ },
+ ]);
+
+ expect(pricing).toEqual({
+ xd: {
+ 'half-price': {
+ providerId: 'xd',
+ modelId: 'half-price',
+ currency: gatewayCurrencyForRegion(CURRENT_CINDY_REGION),
+ source: 'gateway',
+ approximate: false,
+ inputPerMtok: 6,
+ outputPerMtok: 18,
+ cacheReadPerMtok: 6,
+ discount: 0.5,
+ originalInputPerMtok: 12,
+ originalOutputPerMtok: 36,
+ originalCacheReadPerMtok: 12,
+ },
+ 'invalid-discount': {
+ providerId: 'xd',
+ modelId: 'invalid-discount',
+ currency: gatewayCurrencyForRegion(CURRENT_CINDY_REGION),
+ source: 'gateway',
+ approximate: false,
+ inputPerMtok: 12,
+ outputPerMtok: 36,
+ },
+ 'codex/gpt-5.5': {
+ providerId: 'xd',
+ modelId: 'codex/gpt-5.5',
+ currency: gatewayCurrencyForRegion(CURRENT_CINDY_REGION),
+ source: 'gateway',
+ approximate: false,
+ inputPerMtok: 3,
+ outputPerMtok: 6,
+ },
+ },
+ });
+
+ // 折扣元数据经磁盘缓存整组存活,冷启动记账与展示口径不回退到原价。
+ await vi.waitFor(async () => {
+ const raw = JSON.parse(await readFile(userDataPath('cache', 'model-pricing.json'), 'utf8'));
+ expect(raw.pricing).toEqual(pricing);
+ });
+ __resetModelPricingCacheForTesting();
+ await expect(getModelPricing()).resolves.toEqual(pricing);
+ });
+
it('keeps legal zero tiers but drops missing, invalid and 0/0 standard prices', () => {
const pricing = replaceGatewayModelPricing([
{
diff --git a/apps/desktop/src/main/usage/modelPricing.ts b/apps/desktop/src/main/usage/modelPricing.ts
index 652c35587d..031cfd3362 100644
--- a/apps/desktop/src/main/usage/modelPricing.ts
+++ b/apps/desktop/src/main/usage/modelPricing.ts
@@ -114,6 +114,26 @@ function validateQuote(
if (isNonNegativeFinite(quote.cacheCreatePerMtok)) {
next.cacheCreatePerMtok = quote.cacheCreatePerMtok;
}
+ // 折扣元数据整组透传:discount 合法且带完整 input/output 原价才保留,避免残缺
+ // 缓存数据让展示层重建出错误的划线原价。
+ if (
+ typeof quote.discount === 'number' &&
+ Number.isFinite(quote.discount) &&
+ quote.discount > 0 &&
+ quote.discount <= 1 &&
+ isNonNegativeFinite(quote.originalInputPerMtok) &&
+ isNonNegativeFinite(quote.originalOutputPerMtok)
+ ) {
+ next.discount = quote.discount;
+ next.originalInputPerMtok = quote.originalInputPerMtok;
+ next.originalOutputPerMtok = quote.originalOutputPerMtok;
+ if (isNonNegativeFinite(quote.originalCacheReadPerMtok)) {
+ next.originalCacheReadPerMtok = quote.originalCacheReadPerMtok;
+ }
+ if (isNonNegativeFinite(quote.originalCacheCreatePerMtok)) {
+ next.originalCacheCreatePerMtok = quote.originalCacheCreatePerMtok;
+ }
+ }
return next;
}
diff --git a/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts b/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts
index f00e148f66..94c5fcb8b8 100644
--- a/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts
+++ b/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts
@@ -503,8 +503,11 @@ describe('ModelSelector trigger variants', () => {
currency: 'CNY',
source: 'gateway',
approximate: false,
- inputPerMtok: 12,
- outputPerMtok: 36,
+ inputPerMtok: 6,
+ outputPerMtok: 18,
+ discount: 0.5,
+ originalInputPerMtok: 12,
+ originalOutputPerMtok: 36,
},
},
};
@@ -579,8 +582,11 @@ describe('ModelSelector trigger variants', () => {
currency: 'CNY',
source: 'gateway',
approximate: false,
- inputPerMtok: 12,
- outputPerMtok: 36,
+ inputPerMtok: 6,
+ outputPerMtok: 18,
+ discount: 0.5,
+ originalInputPerMtok: 12,
+ originalOutputPerMtok: 36,
},
},
};
@@ -828,8 +834,11 @@ describe('ModelSelector trigger variants', () => {
currency: 'CNY',
source: 'gateway',
approximate: false,
- inputPerMtok: 12,
- outputPerMtok: 36,
+ inputPerMtok: 6,
+ outputPerMtok: 18,
+ discount: 0.5,
+ originalInputPerMtok: 12,
+ originalOutputPerMtok: 36,
},
},
};
diff --git a/apps/desktop/src/renderer/components/new-chat/ModelSelector.tsx b/apps/desktop/src/renderer/components/new-chat/ModelSelector.tsx
index d9ab791db4..377bfed079 100644
--- a/apps/desktop/src/renderer/components/new-chat/ModelSelector.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ModelSelector.tsx
@@ -648,8 +648,9 @@ function ModelSelectorContentView({
// ── 模型单价 ─────────────────────────────────────────────────────────────
// providerId 是价格索引的一部分。同模型经 XD / OpenAI / Anthropic 等来源出现时,
- // 必须按实际行来源查价,不能退化为 pricing[modelId]。只有 XD Gateway 目录价会
- // 叠加 CatalogModel.cost 作为折后展示价;其它来源保持价格源自带的币种。
+ // 必须按实际行来源查价,不能退化为 pricing[modelId]。XD Gateway quote 本身即
+ // 折后实付口径(原价在 quote.original*),CatalogModel.cost 只作「明确全零 →
+ // 免费」的证据;其它来源保持价格源自带的币种。
const pricePresentationOf = (providerId: string | null, id: string) => {
// device-link 只同步被控端 provider 目录,不同步价格快照;不能把控制端价格与
// 被控端 CatalogModel.cost 拼成一个展示结果。在协议补齐前远程选择器不展示价格。
diff --git a/apps/desktop/src/renderer/hooks/useModelPricing.ts b/apps/desktop/src/renderer/hooks/useModelPricing.ts
index 1e4bc98f1f..86dd86eb51 100644
--- a/apps/desktop/src/renderer/hooks/useModelPricing.ts
+++ b/apps/desktop/src/renderer/hooks/useModelPricing.ts
@@ -2,10 +2,7 @@
import { useEffect, useState } from 'react';
-import type {
- ModelPriceQuote,
- ModelPricingCatalog,
-} from '../../shared/regionalMoney';
+import type { ModelPriceQuote, ModelPricingCatalog } from '../../shared/regionalMoney';
let cache: ModelPricingCatalog | null = null;
let cacheLoaded = false;
@@ -30,10 +27,23 @@ function isValidQuote(
quote.approximate === false &&
isNonNegativeFinite(quote.inputPerMtok) &&
isNonNegativeFinite(quote.outputPerMtok) &&
- (quote.cacheReadPerMtok === undefined ||
- isNonNegativeFinite(quote.cacheReadPerMtok)) &&
- (quote.cacheCreatePerMtok === undefined ||
- isNonNegativeFinite(quote.cacheCreatePerMtok))
+ (quote.cacheReadPerMtok === undefined || isNonNegativeFinite(quote.cacheReadPerMtok)) &&
+ (quote.cacheCreatePerMtok === undefined || isNonNegativeFinite(quote.cacheCreatePerMtok)) &&
+ // 折扣元数据可选;discount 存在时必须合法且带完整 input/output 原价。
+ (quote.discount === undefined ||
+ (typeof quote.discount === 'number' &&
+ Number.isFinite(quote.discount) &&
+ quote.discount > 0 &&
+ quote.discount <= 1 &&
+ isNonNegativeFinite(quote.originalInputPerMtok) &&
+ isNonNegativeFinite(quote.originalOutputPerMtok))) &&
+ (quote.originalInputPerMtok === undefined || isNonNegativeFinite(quote.originalInputPerMtok)) &&
+ (quote.originalOutputPerMtok === undefined ||
+ isNonNegativeFinite(quote.originalOutputPerMtok)) &&
+ (quote.originalCacheReadPerMtok === undefined ||
+ isNonNegativeFinite(quote.originalCacheReadPerMtok)) &&
+ (quote.originalCacheCreatePerMtok === undefined ||
+ isNonNegativeFinite(quote.originalCacheCreatePerMtok))
);
}
@@ -45,8 +55,8 @@ function isValidCatalog(v: unknown): v is ModelPricingCatalog {
!!rawModels &&
typeof rawModels === 'object' &&
!Array.isArray(rawModels) &&
- Object.entries(rawModels as Record<string, unknown>).every(
- ([modelId, quote]) => isValidQuote(quote, providerId, modelId),
+ Object.entries(rawModels as Record<string, unknown>).every(([modelId, quote]) =>
+ isValidQuote(quote, providerId, modelId),
),
);
}
@@ -78,12 +88,11 @@ export function useModelPricing(): ModelPricingCatalog | null {
useEffect(() => {
let active = true;
- const unsubscribe =
- window.electronAPI.maker.usage.onModelPricingChanged((next) => {
- cacheLoaded = true;
- cache = isValidCatalog(next) ? next : null;
- if (active) setPricing(cache);
- });
+ const unsubscribe = window.electronAPI.maker.usage.onModelPricingChanged((next) => {
+ cacheLoaded = true;
+ cache = isValidCatalog(next) ? next : null;
+ if (active) setPricing(cache);
+ });
void load().then((res) => {
if (active) setPricing(res);
});
diff --git a/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts b/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts
index 4053f9016e..edebb303af 100644
--- a/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts
@@ -50,12 +50,20 @@ describe('modelPriceFormat', () => {
]);
});
- it('uses the effective price while retaining the original for a 50% discount', () => {
- const original = quote({ inputPerMtok: 12, outputPerMtok: 36 });
- expect(modelPricePresentation(original, { input: 6, output: 18 })).toEqual({
+ it('presents a discounted quote with its carried originals and badge values', () => {
+ const discounted = quote({
+ inputPerMtok: 6,
+ outputPerMtok: 18,
+ cacheReadPerMtok: 0.15,
+ discount: 0.5,
+ originalInputPerMtok: 12,
+ originalOutputPerMtok: 36,
+ originalCacheReadPerMtok: 0.3,
+ });
+ expect(modelPricePresentation(discounted, { input: 6, output: 18 })).toEqual({
kind: 'priced',
- current: quote({ inputPerMtok: 6, outputPerMtok: 18 }),
- original,
+ current: quote({ inputPerMtok: 6, outputPerMtok: 18, cacheReadPerMtok: 0.15 }),
+ original: quote({ inputPerMtok: 12, outputPerMtok: 36, cacheReadPerMtok: 0.3 }),
discount: 0.5,
});
expect(modelPriceDiscountLabelValues(0.5)).toEqual({
@@ -68,34 +76,46 @@ describe('modelPriceFormat', () => {
});
expect(
modelPriceDetailRows(
- quote({ inputPerMtok: 6, outputPerMtok: 18, cacheReadPerMtok: 0.3 }),
- original,
+ quote({ inputPerMtok: 6, outputPerMtok: 18, cacheReadPerMtok: 0.15 }),
+ quote({ inputPerMtok: 12, outputPerMtok: 36, cacheReadPerMtok: 0.3 }),
),
).toEqual([
{ kind: 'input', value: '¥6', originalValue: '¥12' },
{ kind: 'output', value: '¥18', originalValue: '¥36' },
- { kind: 'cacheRead', value: '¥0.3' },
+ { kind: 'cacheRead', value: '¥0.15', originalValue: '¥0.3' },
]);
});
- it('marks only an explicit double-zero model price with a confirmed missing quote as free', () => {
+ it('marks only an all-zero cost with a confirmed missing quote as free', () => {
expect(modelPricePresentation(null, { input: 0, output: 0 })).toEqual({
kind: 'free',
});
+ expect(
+ modelPricePresentation(null, { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }),
+ ).toEqual({ kind: 'free' });
expect(modelPricePresentation(undefined, { input: 0, output: 0 })).toBeNull();
+ // 缓存维度带价 ⇒ 不是免费模型;标准报价被 0/0 规则丢弃后宁可不展示价格。
+ expect(modelPricePresentation(null, { input: 0, output: 0, cacheRead: 8.4 })).toBeNull();
+ expect(modelPricePresentation(null, { input: 0, output: 0, cacheWrite: 15 })).toBeNull();
});
- it('keeps double-zero effective prices as a 100% discount when the quote is nonzero', () => {
- const original = quote({ inputPerMtok: 12, outputPerMtok: 36 });
- expect(modelPricePresentation(original, { input: 0, output: 0 })).toEqual({
+ it('keeps a 100% discounted quote as a discount, never as free', () => {
+ const fullDiscount = quote({
+ inputPerMtok: 0,
+ outputPerMtok: 0,
+ discount: 1,
+ originalInputPerMtok: 12,
+ originalOutputPerMtok: 36,
+ });
+ expect(modelPricePresentation(fullDiscount, { input: 0, output: 0 })).toEqual({
kind: 'priced',
current: quote({ inputPerMtok: 0, outputPerMtok: 0 }),
- original,
+ original: quote({ inputPerMtok: 12, outputPerMtok: 36 }),
discount: 1,
});
});
- it('preserves the standard price when there is no discount', () => {
+ it('preserves the standard price when the quote carries no discount', () => {
const standard = quote({ inputPerMtok: 12, outputPerMtok: 36 });
expect(modelPricePresentation(standard, { input: 12, output: 36 })).toEqual({
kind: 'priced',
@@ -103,29 +123,30 @@ describe('modelPriceFormat', () => {
});
});
- it('ignores sub-threshold floating-point discount noise', () => {
- const standard = quote({ inputPerMtok: 12, outputPerMtok: 36 });
- expect(
- modelPricePresentation(standard, {
- input: 12 * (1 - 1e-12),
- output: 36 * (1 - 1e-12),
- }),
- ).toEqual({
+ it('ignores sub-threshold discounts on the quote', () => {
+ const noisy = quote({
+ inputPerMtok: 12,
+ outputPerMtok: 36,
+ discount: 1e-12,
+ originalInputPerMtok: 12,
+ originalOutputPerMtok: 36,
+ });
+ expect(modelPricePresentation(noisy, { input: 12, output: 36 })).toEqual({
kind: 'priced',
- current: standard,
+ current: noisy,
});
});
- it('keeps missing or inconsistent display costs on the existing fallback path', () => {
+ it('never derives a discount from catalog costs alone', () => {
const standard = quote({ inputPerMtok: 12, outputPerMtok: 36 });
- expect(modelPricePresentation(standard, { input: 6 })).toEqual({
+ expect(modelPricePresentation(standard, { input: 6, output: 18 })).toEqual({
kind: 'priced',
current: standard,
});
- expect(modelPricePresentation(undefined, { input: 0 })).toBeNull();
- expect(modelPricePresentation(standard, { input: 6, output: 30 })).toEqual({
+ expect(modelPricePresentation(standard, { input: 6 })).toEqual({
kind: 'priced',
current: standard,
});
+ expect(modelPricePresentation(undefined, { input: 0 })).toBeNull();
});
});
diff --git a/apps/desktop/src/renderer/lib/modelPriceFormat.ts b/apps/desktop/src/renderer/lib/modelPriceFormat.ts
index 98a9213bc0..3857bef7e4 100644
--- a/apps/desktop/src/renderer/lib/modelPriceFormat.ts
+++ b/apps/desktop/src/renderer/lib/modelPriceFormat.ts
@@ -3,6 +3,8 @@ import type { ModelPriceQuote, MoneyCurrency } from '../../shared/regionalMoney'
interface EffectiveModelCost {
input?: number;
output?: number;
+ cacheRead?: number;
+ cacheWrite?: number;
}
const MIN_DISPLAY_DISCOUNT = 0.0005;
@@ -47,33 +49,11 @@ function isNonNegativeFinite(value: unknown): value is number {
return typeof value === 'number' && Number.isFinite(value) && value >= 0;
}
-function inferDiscount(
- quote: ModelPriceQuote,
- cost: Required<EffectiveModelCost>,
-): number | undefined {
- const candidates: number[] = [];
- for (const [original, current] of [
- [quote.inputPerMtok, cost.input],
- [quote.outputPerMtok, cost.output],
- ] as const) {
- if (original === 0) {
- if (current !== 0) return undefined;
- continue;
- }
- const candidate = 1 - current / original;
- if (candidate < MIN_DISPLAY_DISCOUNT || candidate > 1) return undefined;
- candidates.push(candidate);
- }
- if (candidates.length === 0) return undefined;
- const discount = candidates[0];
- return candidates.every((candidate) => Math.abs(candidate - discount) < 1e-9)
- ? discount
- : undefined;
-}
-
/**
- * 构建模型选择器的展示价格。quote 继续保留用量估算所需的标准价;
- * CatalogModel.cost 只承载 XD 模型的折后展示价。
+ * 构建模型选择器的展示价格。quote 即实付口径——Gateway costDiscount 已在构建
+ * quote 时套用(shared/modelPriceQuote.ts),用量记账与展示同价;折扣前原价由
+ * quote.original* 承载。CatalogModel.cost 只作为「明确全零 → 免费」的证据
+ * (含缓存维度),不再参与折扣推断。
*/
export function modelPricePresentation(
quote: ModelPriceQuote | null | undefined,
@@ -81,33 +61,49 @@ export function modelPricePresentation(
): ModelPricePresentation | null {
if (quote === undefined) return null;
- const input = effectiveCost?.input;
- const output = effectiveCost?.output;
- const hasEffectiveCost = isNonNegativeFinite(input) && isNonNegativeFinite(output);
+ if (quote === null) {
+ // 免费证据链:价格快照已成功加载但确认无该模型报价(quote === null),且目录
+ // cost 的所有计费维度(含缓存)都明确为 0。任何维度带价都不许出免费标签。
+ const input = effectiveCost?.input;
+ const output = effectiveCost?.output;
+ const allDimensionsZero =
+ isNonNegativeFinite(input) &&
+ input === 0 &&
+ isNonNegativeFinite(output) &&
+ output === 0 &&
+ (effectiveCost?.cacheRead === undefined || effectiveCost.cacheRead === 0) &&
+ (effectiveCost?.cacheWrite === undefined || effectiveCost.cacheWrite === 0);
+ return allDimensionsZero ? { kind: 'free' } : null;
+ }
if (
- hasEffectiveCost &&
- input === 0 &&
- output === 0 &&
- (quote === null || (quote.inputPerMtok === 0 && quote.outputPerMtok === 0))
+ quote.discount !== undefined &&
+ quote.discount >= MIN_DISPLAY_DISCOUNT &&
+ quote.originalInputPerMtok !== undefined &&
+ quote.originalOutputPerMtok !== undefined
) {
- return { kind: 'free' };
+ const {
+ discount,
+ originalInputPerMtok,
+ originalOutputPerMtok,
+ originalCacheReadPerMtok,
+ originalCacheCreatePerMtok,
+ ...current
+ } = quote;
+ const original: ModelPriceQuote = {
+ ...current,
+ inputPerMtok: originalInputPerMtok,
+ outputPerMtok: originalOutputPerMtok,
+ ...(originalCacheReadPerMtok !== undefined
+ ? { cacheReadPerMtok: originalCacheReadPerMtok }
+ : {}),
+ ...(originalCacheCreatePerMtok !== undefined
+ ? { cacheCreatePerMtok: originalCacheCreatePerMtok }
+ : {}),
+ };
+ return { kind: 'priced', current, original, discount };
}
- if (quote === null) return null;
- if (!hasEffectiveCost) return { kind: 'priced', current: quote };
-
- const discount = inferDiscount(quote, { input, output });
- if (discount === undefined) return { kind: 'priced', current: quote };
- return {
- kind: 'priced',
- current: {
- ...quote,
- inputPerMtok: input,
- outputPerMtok: output,
- },
- original: quote,
- discount,
- };
+ return { kind: 'priced', current: quote };
}
export function modelPriceDetailRows(
@@ -149,6 +145,14 @@ export function modelPriceDetailRows(
{
kind: 'cacheRead' as const,
value: formatModelPriceAmount(quote.cacheReadPerMtok, quote.currency),
+ ...(originalQuote?.cacheReadPerMtok !== undefined
+ ? {
+ originalValue: formatModelPriceAmount(
+ originalQuote.cacheReadPerMtok,
+ originalQuote.currency,
+ ),
+ }
+ : {}),
},
]),
...(quote.cacheCreatePerMtok === undefined
@@ -157,6 +161,14 @@ export function modelPriceDetailRows(
{
kind: 'cacheCreate' as const,
value: formatModelPriceAmount(quote.cacheCreatePerMtok, quote.currency),
+ ...(originalQuote?.cacheCreatePerMtok !== undefined
+ ? {
+ originalValue: formatModelPriceAmount(
+ originalQuote.cacheCreatePerMtok,
+ originalQuote.currency,
+ ),
+ }
+ : {}),
},
]),
];
diff --git a/apps/desktop/src/shared/billing.ts b/apps/desktop/src/shared/billing.ts
index 9fa4667141..22367250ae 100644
--- a/apps/desktop/src/shared/billing.ts
+++ b/apps/desktop/src/shared/billing.ts
@@ -88,8 +88,10 @@ export type BillingPaymentOrder = {
status: BillingPaymentOrderStatus;
paymentAction: BillingPaymentAction | null;
/**
- * Added by the fulfillment projection. Older servers may omit it; the client
- * must then stay in "crediting" and never infer delivery from payment alone.
+ * Added by the fulfillment projection; informational only. Since PR #401 the
+ * checkout UX treats payment status SUCCEEDED as terminal (COMPLETED) and no
+ * longer keys any phase, recovery or polling behavior off this field —
+ * crediting is reconciled server-side for every SUCCEEDED order.
*/
fulfillmentStatus?: BillingFulfillmentStatus;
createdAt: string;
diff --git a/apps/desktop/src/shared/modelPriceQuote.ts b/apps/desktop/src/shared/modelPriceQuote.ts
index 1dee64c9ab..32b667d29d 100644
--- a/apps/desktop/src/shared/modelPriceQuote.ts
+++ b/apps/desktop/src/shared/modelPriceQuote.ts
@@ -51,6 +51,45 @@ function applyCodexBudgetDiscount(quote: ModelPriceQuote): ModelPriceQuote {
};
}
+function normalizedCostDiscount(value: unknown): number | undefined {
+ return typeof value === 'number' && Number.isFinite(value) && value > 0 && value <= 1
+ ? value
+ : undefined;
+}
+
+/**
+ * 把 Gateway costDiscount 落到报价上:各单价 ×(1-discount) 变为实付口径,原价挪进
+ * original* 字段供划线展示。codex/ 折扣路由跳过——其 0.15 乘数由
+ * applyCodexBudgetDiscount 作为客户端权威,不与服务端 costDiscount 叠加双重打折。
+ */
+function applyGatewayCostDiscount(
+ quote: ModelPriceQuote,
+ discount: number | undefined,
+): ModelPriceQuote {
+ if (discount === undefined || quote.modelId.startsWith('codex/')) return quote;
+ const multiplier = 1 - discount;
+ return {
+ ...quote,
+ inputPerMtok: quote.inputPerMtok * multiplier,
+ outputPerMtok: quote.outputPerMtok * multiplier,
+ ...(quote.cacheReadPerMtok !== undefined
+ ? { cacheReadPerMtok: quote.cacheReadPerMtok * multiplier }
+ : {}),
+ ...(quote.cacheCreatePerMtok !== undefined
+ ? { cacheCreatePerMtok: quote.cacheCreatePerMtok * multiplier }
+ : {}),
+ discount,
+ originalInputPerMtok: quote.inputPerMtok,
+ originalOutputPerMtok: quote.outputPerMtok,
+ ...(quote.cacheReadPerMtok !== undefined
+ ? { originalCacheReadPerMtok: quote.cacheReadPerMtok }
+ : {}),
+ ...(quote.cacheCreatePerMtok !== undefined
+ ? { originalCacheCreatePerMtok: quote.cacheCreatePerMtok }
+ : {}),
+ };
+}
+
export function gatewayModelPriceQuote(
model: ModelAccessGatewayModel,
region: CindyRegion,
@@ -61,20 +100,26 @@ export function gatewayModelPriceQuote(
if (!modelId || inputPerMtok === undefined || outputPerMtok === undefined) {
return undefined;
}
+ // 0/0 判定用折扣前原价:100% 折扣(原价非零)仍是有效报价,展示为立省 100% 而非免费。
if (inputPerMtok === 0 && outputPerMtok === 0) return undefined;
const cacheReadPerMtok = perMtok(model.cacheReadInputTokenCost);
const cacheCreatePerMtok = perMtok(model.cacheCreationInputTokenCost);
- return applyCodexBudgetDiscount({
- providerId: 'xd',
- modelId,
- currency: gatewayCurrencyForRegion(region),
- source: 'gateway',
- approximate: false,
- inputPerMtok,
- outputPerMtok,
- ...(cacheReadPerMtok !== undefined ? { cacheReadPerMtok } : {}),
- ...(cacheCreatePerMtok !== undefined ? { cacheCreatePerMtok } : {}),
- });
+ return applyCodexBudgetDiscount(
+ applyGatewayCostDiscount(
+ {
+ providerId: 'xd',
+ modelId,
+ currency: gatewayCurrencyForRegion(region),
+ source: 'gateway',
+ approximate: false,
+ inputPerMtok,
+ outputPerMtok,
+ ...(cacheReadPerMtok !== undefined ? { cacheReadPerMtok } : {}),
+ ...(cacheCreatePerMtok !== undefined ? { cacheCreatePerMtok } : {}),
+ },
+ normalizedCostDiscount(model.costDiscount),
+ ),
+ );
}
export function gatewayPricingCatalog(
@@ -168,9 +213,7 @@ export function getModelPriceQuote(
);
}
-export function subscriptionDirectPriceQuote(
- modelId: string,
-): ModelPriceQuote | undefined {
+export function subscriptionDirectPriceQuote(modelId: string): ModelPriceQuote | undefined {
if (modelId.startsWith(CHATGPT_MODEL_PREFIX)) {
return providerReferencePriceQuote('openai', modelId);
}
@@ -193,15 +236,25 @@ export function regionalizeModelPriceQuote(
outputPerMtok: quote.outputPerMtok * USD_TO_CNY_FIXED_RATE,
...(quote.cacheReadPerMtok !== undefined
? {
- cacheReadPerMtok:
- quote.cacheReadPerMtok * USD_TO_CNY_FIXED_RATE,
+ cacheReadPerMtok: quote.cacheReadPerMtok * USD_TO_CNY_FIXED_RATE,
}
: {}),
...(quote.cacheCreatePerMtok !== undefined
? {
- cacheCreatePerMtok:
- quote.cacheCreatePerMtok * USD_TO_CNY_FIXED_RATE,
+ cacheCreatePerMtok: quote.cacheCreatePerMtok * USD_TO_CNY_FIXED_RATE,
}
: {}),
+ ...(quote.originalInputPerMtok !== undefined
+ ? { originalInputPerMtok: quote.originalInputPerMtok * USD_TO_CNY_FIXED_RATE }
+ : {}),
+ ...(quote.originalOutputPerMtok !== undefined
+ ? { originalOutputPerMtok: quote.originalOutputPerMtok * USD_TO_CNY_FIXED_RATE }
+ : {}),
+ ...(quote.originalCacheReadPerMtok !== undefined
+ ? { originalCacheReadPerMtok: quote.originalCacheReadPerMtok * USD_TO_CNY_FIXED_RATE }
+ : {}),
+ ...(quote.originalCacheCreatePerMtok !== undefined
+ ? { originalCacheCreatePerMtok: quote.originalCacheCreatePerMtok * USD_TO_CNY_FIXED_RATE }
+ : {}),
};
}
diff --git a/apps/desktop/src/shared/regionalMoney.ts b/apps/desktop/src/shared/regionalMoney.ts
index 8b5e0d987d..d42b745f42 100644
--- a/apps/desktop/src/shared/regionalMoney.ts
+++ b/apps/desktop/src/shared/regionalMoney.ts
@@ -23,6 +23,15 @@ export interface ModelPriceQuote {
outputPerMtok: number;
cacheReadPerMtok?: number;
cacheCreatePerMtok?: number;
+ /**
+ * Gateway 促销折扣(0..1]。存在时本报价的各单价**已是折后实付价**——用量记账与
+ * 选择器展示共用同一口径;折扣前原价由 original* 字段承载,仅供划线展示。
+ */
+ discount?: number;
+ originalInputPerMtok?: number;
+ originalOutputPerMtok?: number;
+ originalCacheReadPerMtok?: number;
+ originalCacheCreatePerMtok?: number;
}
export type ModelPricingCatalog = Record<string, Record<string, ModelPriceQuote>>;