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): 目录币种裁决只统计计价条目,无价条目不再拖垮 CNY 价格目录 (#600)

cnxpfcv-oss200ac36556e6
diff --git a/apps/desktop/src/main/model-access/index.ts b/apps/desktop/src/main/model-access/index.ts
index 493373f48d..e67a83c0fc 100644
--- a/apps/desktop/src/main/model-access/index.ts
+++ b/apps/desktop/src/main/model-access/index.ts
@@ -18,6 +18,7 @@ import {
replaceGatewayModelPricing,
trackGatewayModelPricingSync,
} from '../usage/modelPricing.js';
+import { isPricedGatewayModel } from '../../shared/modelPriceQuote.js';
import { throwIpcError } from '../utils/ipcValidate.js';
import {
MODEL_ACCESS_STATUS_CHANNEL,
@@ -143,7 +144,17 @@ function applyGatewayModels(models: ModelAccessGatewayModel[]): void {
// 同一次 /models 响应先建立 provider-scoped 价格投影,再做仅影响展示元数据的
// dev overlay。空成功响应会同时清空模型和价格;请求失败不会调用本函数,
// 因而保留上一份完整成功快照。
- replaceGatewayModelPricing(models);
+ const pricing = replaceGatewayModelPricing(models);
+ // 分母只算会产生报价的条目:免费/无价条目按设计不出报价,不该把健康目录
+ // 也报成覆盖不足。此时覆盖缺口只剩一种成因——币种声明与目录冲突被丢弃,
+ // 这在 #587 之前是全程静默的,这行日志让现场可判。
+ const pricedCount = models.filter(isPricedGatewayModel).length;
+ const quoteCount = Object.keys(pricing.xd ?? {}).length;
+ if (quoteCount < pricedCount) {
+ log.warn(
+ `xd gateway pricing quotes cover ${quoteCount}/${pricedCount} priced models (${models.length} total)`,
+ );
+ }
// dev:本地目录文件(catalog/providers.json)的 cindyModelMeta 段覆盖服务端下发的
// 元数据,改本地 json + 重启即可自测,无需发 OSS / 等服务端热加载;只覆盖同 id,
// 清单成员资格仍以网关为准。packaged 不走此分支(语义见 devMetaOverlay.ts)。
diff --git a/apps/desktop/src/shared/__tests__/modelPriceQuote.test.ts b/apps/desktop/src/shared/__tests__/modelPriceQuote.test.ts
index 08b0f0df4f..db5722a643 100644
--- a/apps/desktop/src/shared/__tests__/modelPriceQuote.test.ts
+++ b/apps/desktop/src/shared/__tests__/modelPriceQuote.test.ts
@@ -58,6 +58,40 @@ describe('resolveGatewayCatalogCurrency', () => {
]),
).toBe('USD');
});
+
+ it('excludes price-less entries from the vote — a metadata-only sibling cannot pin CNY catalogs to USD', () => {
+ expect(
+ resolveGatewayCatalogCurrency([
+ model('a', { currency: 'CNY' }),
+ model('doc', {
+ inputCostPerToken: undefined,
+ outputCostPerToken: undefined,
+ }),
+ ]),
+ ).toBe('CNY');
+ });
+
+ it('excludes zero-priced (free) entries from the vote', () => {
+ expect(
+ resolveGatewayCatalogCurrency([
+ model('a', { currency: 'CNY' }),
+ model('free', { inputCostPerToken: 0, outputCostPerToken: 0 }),
+ ]),
+ ).toBe('CNY');
+ });
+
+ it('still keeps the catalog USD when a priced entry lacks the declaration', () => {
+ expect(
+ resolveGatewayCatalogCurrency([
+ model('a', { currency: 'CNY' }),
+ model('b'),
+ model('doc', {
+ inputCostPerToken: undefined,
+ outputCostPerToken: undefined,
+ }),
+ ]),
+ ).toBe('USD');
+ });
});
describe('gatewayPricingCatalog currency', () => {
@@ -93,4 +127,21 @@ describe('gatewayPricingCatalog currency', () => {
expect(Object.keys(catalog.xd)).toEqual(['b']);
expect(catalog.xd.b.currency).toBe('USD');
});
+
+ it('keeps every CNY quote when the only undeclared sibling is price-less (#587 regression)', () => {
+ const catalog = gatewayPricingCatalog([
+ model('a', { currency: 'CNY' }),
+ model('b', { currency: 'CNY' }),
+ model('doc', {
+ inputCostPerToken: undefined,
+ outputCostPerToken: undefined,
+ }),
+ model('free', { inputCostPerToken: 0, outputCostPerToken: 0 }),
+ ]);
+ expect(Object.keys(catalog.xd)).toEqual(['a', 'b']);
+ expect(Object.values(catalog.xd).map((quote) => quote.currency)).toEqual([
+ 'CNY',
+ 'CNY',
+ ]);
+ });
});
diff --git a/apps/desktop/src/shared/modelAccess.ts b/apps/desktop/src/shared/modelAccess.ts
index e81fe4ff82..0c7ac067e3 100644
--- a/apps/desktop/src/shared/modelAccess.ts
+++ b/apps/desktop/src/shared/modelAccess.ts
@@ -156,9 +156,10 @@ export interface ModelAccessGatewayModel extends ModelGroupPricing {
/**
* 本条目价格字段的计费币种声明(透传 Gateway)。缺省表示 Gateway 原生口径
* USD;客户端不得按构建区域改标或折算——单位永远跟随下发数据。
- * 注意:本地记账账本是单币种的,只有**每个**条目都显式声明同一非 USD 币种
- * 时目录才整体切换;与目录币种冲突的声明条目不出报价(费用退回 SDK 实报
- * USD 兜底),见 modelPriceQuote.resolveGatewayCatalogCurrency。
+ * 注意:本地记账账本是单币种的,只有**每个会产生报价**的条目都显式声明同一
+ * 非 USD 币种时目录才整体切换(免费/无价条目不参与裁决);与目录币种冲突的
+ * 声明条目不出报价(费用退回 SDK 实报 USD 兜底),见
+ * modelPriceQuote.resolveGatewayCatalogCurrency。
*/
currency?: 'USD' | 'CNY';
/** 进哪些 runtime tab;缺省 = 仅 claude-code(网关 /v1/messages 翻译覆盖面最广)。 */
diff --git a/apps/desktop/src/shared/modelPriceQuote.ts b/apps/desktop/src/shared/modelPriceQuote.ts
index d0d679e085..1920e3de60 100644
--- a/apps/desktop/src/shared/modelPriceQuote.ts
+++ b/apps/desktop/src/shared/modelPriceQuote.ts
@@ -60,18 +60,27 @@ function declaredCurrency(
/**
* 目录级币种:本地记账账本(daily / session / schedule)都是单币种,逐条目
* 混用币种会造成同端多币种金额被聚合层丢弃。规则:
- * - 未声明条目恒为 Gateway 原生 USD(契约缺省),绝不被其它条目的声明改标;
- * - 只有当**每个**条目都显式声明同一非 USD 币种时,目录才整体切换;
+ * - 只有**会产生报价**的条目参与裁决:免费/无价条目本就不出报价,它们缺失
+ * currency 声明不能把整个目录钉回 USD、连带丢弃全部已声明的报价
+ * (对外 CNY 目录曾因此全军覆没,#587);
+ * - 未声明的计价条目恒为 Gateway 原生 USD(契约缺省),绝不被其它条目的声明改标;
+ * - 只有当每个计价条目都显式声明同一非 USD 币种时,目录才整体切换;
* - 与目录币种冲突的声明条目由 gatewayPricingCatalog 丢弃报价(退回 SDK
* 实报 USD 兜底),而不是改标币种——错标单位正是本模块要杜绝的事。
*/
+/** 该条目是否会产生报价(与币种无关;目录币种裁决与覆盖率统计共用此判定)。 */
+export function isPricedGatewayModel(model: ModelAccessGatewayModel): boolean {
+ return gatewayModelPriceQuote(model) !== undefined;
+}
+
export function resolveGatewayCatalogCurrency(
models: readonly ModelAccessGatewayModel[],
): MoneyCurrency {
- if (models.length === 0) return GATEWAY_NATIVE_CURRENCY;
- const first = declaredCurrency(models[0]);
+ const priced = models.filter(isPricedGatewayModel);
+ if (priced.length === 0) return GATEWAY_NATIVE_CURRENCY;
+ const first = declaredCurrency(priced[0]);
if (!first || first === GATEWAY_NATIVE_CURRENCY) return GATEWAY_NATIVE_CURRENCY;
- return models.every((model) => declaredCurrency(model) === first)
+ return priced.every((model) => declaredCurrency(model) === first)
? first
: GATEWAY_NATIVE_CURRENCY;
}