From 2c53f147fa755fe715d033c15a67a8a782b3ce69 Mon Sep 17 00:00:00 2001 From: LIghtJUNction Date: Mon, 12 Oct 2026 04:42:42 +0800 Subject: [PATCH] fix(web): recalculate generic cache estimates from classified tokens --- .../pricing/components/model-details.test.tsx | 34 ++++++++++++++ .../pricing/lib/native-billing.test.ts | 44 +++++++++++++++++++ .../web/src/features/pricing/lib/tier-expr.ts | 2 + .../models/tiered-pricing-editor.test.tsx | 24 ++++++++++ 4 files changed, 104 insertions(+) diff --git a/apps/web/src/features/pricing/components/model-details.test.tsx b/apps/web/src/features/pricing/components/model-details.test.tsx index 61f09194d..f148d307a 100644 --- a/apps/web/src/features/pricing/components/model-details.test.tsx +++ b/apps/web/src/features/pricing/components/model-details.test.tsx @@ -316,6 +316,40 @@ describe('ModelDetails group pricing', () => { await unmount(rendered) }) + test('recalculates the request estimate when cached input tokens change', async () => { + const rendered = await renderModelDetails('cached-model', { + cache_read_price: 0.2, + }) + const section = [...rendered.container.querySelectorAll('h3')] + .find((element) => element.textContent === 'Request cost estimate') + ?.closest('section') + assert.ok(section) + const cached = section.querySelector( + 'input[id$="-cached"]' + ) + assert.ok(cached) + const setValue = Object.getOwnPropertyDescriptor( + HTMLInputElement.prototype, + 'value' + )?.set + assert.ok(setValue) + assert.equal(section.querySelector('[aria-live]')?.textContent, '0.024 USD') + for (const [value, expected] of [ + ['4000', '0.0168 USD'], + ['10000', '0.006 USD'], + ['10001', 'Estimate unavailable'], + ['', 'Estimate unavailable'], + ['0', '0.024 USD'], + ]) { + await act(async () => { + setValue.call(cached, value) + cached.dispatchEvent(new Event('input', { bubbles: true })) + }) + assert.equal(section.querySelector('[aria-live]')?.textContent, expected) + } + await unmount(rendered) + }) + test('does not offer chat token presets for native sessions or their aliases', async () => { for (const [modelName, endpoint] of [ ['gpt-live-1', 'live'], diff --git a/apps/web/src/features/pricing/lib/native-billing.test.ts b/apps/web/src/features/pricing/lib/native-billing.test.ts index bee2fdbc1..722b1040a 100644 --- a/apps/web/src/features/pricing/lib/native-billing.test.ts +++ b/apps/web/src/features/pricing/lib/native-billing.test.ts @@ -135,11 +135,55 @@ describe('native cache and duration billing', () => { assert.equal(evalExprLocally(expression, 1000, 0, emptyExtras).error, null) }) + test('uses the same effective cache count for generic prices and tier context', () => { + const expression = + 'len < 1200 ? tier("short", p * 2 + cr * 0.1) : tier("long", p * 4 + cr * 0.2)' + for (const [details, cost, matchedTier] of [ + [{ cacheTextReadTokens: 100 }, 2010, 'short'], + [{ cacheImageReadTokens: 200 }, 4040, 'long'], + [{ cacheAudioReadTokens: 200 }, 4040, 'long'], + [ + { + cacheTextReadTokens: 50, + cacheImageReadTokens: 100, + cacheAudioReadTokens: 50, + }, + 4040, + 'long', + ], + [{ cacheReadTokens: 400, cacheTextReadTokens: 100 }, 4080, 'long'], + [{ cacheReadTokens: 0, cacheTextReadTokens: 0 }, 2000, 'short'], + ] as const) { + assert.deepEqual( + evalExprLocally(expression, 1000, 0, { ...emptyExtras, ...details }), + { cost, matchedTier, error: null } + ) + } + }) + test('keeps text-only estimates accurate and refuses missing duration', () => { assert.equal( evaluateTextRequestExpression('p * 1 + cr_text * 0.1', 1000, 0, 100), 910 ) + assert.equal( + evaluateTextRequestExpression( + 'p * 1 + cr_img * 10 + cr_audio * 100', + 1000, + 0, + 100 + ), + 1000 + ) + assert.equal( + evaluateTextRequestExpression( + 'p * 1 + cr_text * 0.1 + cr_img * 10 + cr_audio * 100', + 1000, + 0, + 100 + ), + 910 + ) assert.throws( () => evaluateTextRequestExpression('audio_s * 100', 1000, 0, 0), /Audio duration is required/ diff --git a/apps/web/src/features/pricing/lib/tier-expr.ts b/apps/web/src/features/pricing/lib/tier-expr.ts index d9ee9043f..291a9dbf4 100644 --- a/apps/web/src/features/pricing/lib/tier-expr.ts +++ b/apps/web/src/features/pricing/lib/tier-expr.ts @@ -320,6 +320,8 @@ export function evalExprLocally( for (const field of ESTIMATOR_VARS) { variables[field.var] = extraTokenValues[field.stateKey] || 0 } + // A generic cache rate also applies when only classified counts are supplied. + variables.cr = cacheReadTokens const evaluated = evaluateBillingExpression(exprStr, variables) return { cost: evaluated.value || 0, diff --git a/apps/web/src/features/system-settings/models/tiered-pricing-editor.test.tsx b/apps/web/src/features/system-settings/models/tiered-pricing-editor.test.tsx index 1d054fc89..4eba29e21 100644 --- a/apps/web/src/features/system-settings/models/tiered-pricing-editor.test.tsx +++ b/apps/web/src/features/system-settings/models/tiered-pricing-editor.test.tsx @@ -240,3 +240,27 @@ test('estimator maps separate cache counts and decimal audio seconds independent await ctx.cleanup() } }) + +test('estimator prices classified cache counts with a generic cache rate', async () => { + const ctx = await setup('tier("base", p * 2 + c * 3 + cr * 0.1)') + try { + for (const [field, value, cost] of [ + ['Text Cache Read', '1000', 0.0001], + ['Image Cache Read', '2000', 0.0003], + ['Audio Cache Read', '3000', 0.0006], + ['Cache Read', '4000', 0.0004], + ['Cache Read', '0', 0.0006], + ['Text Cache Read', '', 0.0005], + ] as const) { + await ctx.change(field, value) + assert.ok( + ctx.container.textContent?.includes( + `Estimated cost (USD): ${cost.toLocaleString(undefined, { maximumFractionDigits: 12 })}` + ) + ) + } + assert.deepEqual(ctx.changes, []) + } finally { + await ctx.cleanup() + } +})