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): show effective prices for cache-priced models (#456)

kl2a823e45d1f6
diff --git a/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts b/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts
index 0bd4cd4e9c..e447089b3a 100644
--- a/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts
+++ b/apps/desktop/src/renderer/__tests__/modelSelectorTriggerVariant.test.ts
@@ -476,7 +476,7 @@ describe('ModelSelector trigger variants', () => {
contextWindow: 200000,
efforts: ['high'],
defaultEffort: 'high',
- cost: { input: 6, output: 18 },
+ cost: { input: 6, output: 18, cacheRead: 1.2, cacheWrite: 7.5 },
},
{
id: 'claude-sonnet-4-6',
@@ -500,6 +500,8 @@ describe('ModelSelector trigger variants', () => {
approximate: false,
inputPerMtok: 12,
outputPerMtok: 36,
+ cacheReadPerMtok: 2.4,
+ cacheCreatePerMtok: 15,
},
},
};
@@ -807,7 +809,7 @@ describe('ModelSelector trigger variants', () => {
contextWindow: 200000,
efforts: ['high'],
defaultEffort: 'high',
- cost: { input: 6, output: 18 },
+ cost: { input: 6, output: 18, cacheRead: 1.2, cacheWrite: 7.5 },
},
],
},
@@ -823,6 +825,8 @@ describe('ModelSelector trigger variants', () => {
approximate: false,
inputPerMtok: 12,
outputPerMtok: 36,
+ cacheReadPerMtok: 2.4,
+ cacheCreatePerMtok: 15,
},
},
};
@@ -852,6 +856,10 @@ describe('ModelSelector trigger variants', () => {
expect(within(details).getByText('¥18')).toBeTruthy();
expect(within(details).queryByText('¥12')).toBeNull();
expect(within(details).queryByText('¥36')).toBeNull();
+ expect(within(details).getByText('¥1.2')).toBeTruthy();
+ expect(within(details).queryByText('¥2.4')).toBeNull();
+ expect(within(details).getByText('¥7.5')).toBeTruthy();
+ expect(within(details).queryByText('¥15')).toBeNull();
expect(details.querySelector('[data-model-promotion-badge]')).toBeNull();
} finally {
providersRef.providers = providersRef.DEFAULT_PROVIDERS;
diff --git a/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts b/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts
index 858c6c31c4..00955ec86f 100644
--- a/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts
+++ b/apps/desktop/src/renderer/lib/__tests__/modelPriceFormat.test.ts
@@ -64,6 +64,66 @@ describe('modelPriceFormat', () => {
]);
});
+ it('uses the effective Gateway price across Qwen cache dimensions', () => {
+ const standard = quote({
+ modelId: 'qwen/qwen3.7-max',
+ inputPerMtok: 12,
+ outputPerMtok: 36,
+ cacheReadPerMtok: 2.4,
+ cacheCreatePerMtok: 15,
+ });
+ const presentation = modelPricePresentation(standard, {
+ input: 6,
+ output: 18,
+ cacheRead: 1.2,
+ cacheWrite: 7.5,
+ });
+
+ expect(presentation).toEqual({
+ kind: 'priced',
+ current: quote({
+ modelId: 'qwen/qwen3.7-max',
+ inputPerMtok: 6,
+ outputPerMtok: 18,
+ cacheReadPerMtok: 1.2,
+ cacheCreatePerMtok: 7.5,
+ }),
+ });
+ expect(
+ presentation?.kind === 'priced' ? modelPriceDetailRows(presentation.current) : [],
+ ).toEqual([
+ { kind: 'input', value: '¥6' },
+ { kind: 'output', value: '¥18' },
+ { kind: 'cacheRead', value: '¥1.2' },
+ { kind: 'cacheCreate', value: '¥7.5' },
+ ]);
+ });
+
+ it('falls back to the standard price when a cache dimension cannot confirm the effective price', () => {
+ const standard = quote({
+ inputPerMtok: 12,
+ outputPerMtok: 36,
+ cacheReadPerMtok: 2.4,
+ cacheCreatePerMtok: 15,
+ });
+
+ expect(
+ modelPricePresentation(standard, {
+ input: 6,
+ output: 18,
+ cacheRead: 1.2,
+ }),
+ ).toEqual({ kind: 'priced', current: standard });
+ expect(
+ modelPricePresentation(standard, {
+ input: 6,
+ output: 18,
+ cacheRead: 2.4,
+ cacheWrite: 7.5,
+ }),
+ ).toEqual({ kind: 'priced', current: standard });
+ });
+
it('marks only an explicit double-zero model price with a confirmed missing quote as free', () => {
expect(modelPricePresentation(null, { input: 0, output: 0 })).toEqual({
kind: 'free',
diff --git a/apps/desktop/src/renderer/lib/modelPriceFormat.ts b/apps/desktop/src/renderer/lib/modelPriceFormat.ts
index b5e685da54..7acc7f46ca 100644
--- a/apps/desktop/src/renderer/lib/modelPriceFormat.ts
+++ b/apps/desktop/src/renderer/lib/modelPriceFormat.ts
@@ -3,9 +3,18 @@ import type { ModelPriceQuote, MoneyCurrency } from '../../shared/regionalMoney'
interface EffectiveModelCost {
input?: number;
output?: number;
+ cacheRead?: number;
+ cacheWrite?: number;
}
-// input / output 缩放比例小于这个阈值时视为浮点噪声,按标准价展示。
+interface CompleteEffectiveModelCost {
+ input: number;
+ output: number;
+ cacheRead: number | undefined;
+ cacheWrite: number | undefined;
+}
+
+// 各价格维度的缩放比例小于这个阈值时视为浮点噪声,按标准价展示。
const MIN_EFFECTIVE_PRICE_GAP = 0.0005;
export type ModelPricePresentation =
@@ -37,23 +46,25 @@ function isNonNegativeFinite(value: unknown): value is number {
}
/**
- * effectiveCost 能否直接当展示价:要求 input / output 相对标准价是同一个缩放比例。
- * effectiveCost 不带缓存维度,若 quote 有非零缓存价就不敢覆盖 —— 否则明细里的
- * input / output 来自实际价、缓存行来自标准价,两套价表混在一张表上。
+ * effectiveCost 能否直接当展示价:要求已展示的 input / output / 缓存价格
+ * 相对标准价都是同一个缩放比例。标准价中的非零缓存维度必须有实际价对应,
+ * 避免在同一张明细表中混用实际价与标准价。
*/
function effectiveCostIsConsistent(
quote: ModelPriceQuote,
- cost: Required<EffectiveModelCost>,
+ cost: CompleteEffectiveModelCost,
): boolean {
- if ((quote.cacheReadPerMtok && quote.cacheReadPerMtok > 0) ||
- (quote.cacheCreatePerMtok && quote.cacheCreatePerMtok > 0)) {
- return false;
- }
const gaps: number[] = [];
for (const [standard, effective] of [
[quote.inputPerMtok, cost.input],
[quote.outputPerMtok, cost.output],
+ [quote.cacheReadPerMtok, cost.cacheRead],
+ [quote.cacheCreatePerMtok, cost.cacheWrite],
] as const) {
+ if (standard === undefined) continue;
+ if (!isNonNegativeFinite(standard)) return false;
+ if (effective === undefined && standard === 0) continue;
+ if (!isNonNegativeFinite(effective)) return false;
if (standard === 0) {
if (effective !== 0) return false;
continue;
@@ -84,19 +95,20 @@ export function modelPricePresentation(
hasEffectiveCost &&
input === 0 &&
output === 0 &&
- (quote === null || (
- quote.inputPerMtok === 0 &&
- quote.outputPerMtok === 0 &&
- (!quote.cacheReadPerMtok || quote.cacheReadPerMtok === 0) &&
- (!quote.cacheCreatePerMtok || quote.cacheCreatePerMtok === 0)
- ))
+ (quote === null ||
+ (quote.inputPerMtok === 0 &&
+ quote.outputPerMtok === 0 &&
+ (!quote.cacheReadPerMtok || quote.cacheReadPerMtok === 0) &&
+ (!quote.cacheCreatePerMtok || quote.cacheCreatePerMtok === 0)))
) {
return { kind: 'free' };
}
if (quote === null) return null;
if (!hasEffectiveCost) return { kind: 'priced', current: quote };
- if (!effectiveCostIsConsistent(quote, { input, output })) {
+ const cacheRead = effectiveCost?.cacheRead;
+ const cacheWrite = effectiveCost?.cacheWrite;
+ if (!effectiveCostIsConsistent(quote, { input, output, cacheRead, cacheWrite })) {
return { kind: 'priced', current: quote };
}
return {
@@ -105,13 +117,17 @@ export function modelPricePresentation(
...quote,
inputPerMtok: input,
outputPerMtok: output,
+ ...(quote.cacheReadPerMtok !== undefined
+ ? { cacheReadPerMtok: cacheRead ?? quote.cacheReadPerMtok }
+ : {}),
+ ...(quote.cacheCreatePerMtok !== undefined
+ ? { cacheCreatePerMtok: cacheWrite ?? quote.cacheCreatePerMtok }
+ : {}),
},
};
}
-export function modelPriceDetailRows(
- quote: ModelPriceQuote,
-): Array<{
+export function modelPriceDetailRows(quote: ModelPriceQuote): Array<{
kind: 'input' | 'output' | 'cacheRead' | 'cacheCreate';
value: string;
}> {