diff --git a/src/models.ts b/src/models.ts index 7f480043..c37042c3 100644 --- a/src/models.ts +++ b/src/models.ts @@ -584,9 +584,12 @@ export function isFlatRateModel(model: string): boolean { } /// Shared unpriced-warning copy. Never tell the user to alias unconditionally: -/// mapping a subscription SKU onto a priced row invents spend. -export function unpricedModelHint(): string { - return 'If a model is billed per token, map it with: codeburn model-alias "" . If $0 is correct (subscription / flat-rate): codeburn model-flat-rate "".' +/// mapping a subscription SKU onto a priced row invents spend. Optional `model` +/// interpolates the sanitized id so the verbose calculateCost path names the +/// same two hatches. +export function unpricedModelHint(model = ''): string { + const safe = model.replace(/[\x00-\x1F\x7F-\x9F]/g, '?').slice(0, 200) + return `If a model is billed per token, map it with: codeburn model-alias "${safe}" . If $0 is correct (subscription / flat-rate): codeburn model-flat-rate "${safe}".` } /// Stable hash of the model-alias map, for the same staleness class as the @@ -995,10 +998,9 @@ export function calculateCost( // payloads written by external tools, so a hostile or corrupt file // could embed terminal escape sequences here. const safeName = sanitizeModelForDisplay(model) - const aliasHint = `Map it with: codeburn model-alias "${safeName}" , or track local-model savings with: codeburn model-savings "${safeName}" ` process.stderr.write( `codeburn: no pricing data for model "${safeName}" — costs for this model will show $0. ` + - `${aliasHint}, or update with: npx codeburn@latest.\n` + `${unpricedModelHint(safeName)} Or track local-model savings with: codeburn model-savings "${safeName}" , or update with: npx codeburn@latest.\n`, ) } return 0 diff --git a/tests/models.test.ts b/tests/models.test.ts index d5c74350..301e6498 100644 --- a/tests/models.test.ts +++ b/tests/models.test.ts @@ -1108,4 +1108,38 @@ describe('unpricedModelHint', () => { expect(unpricedModelHint()).toContain('model-flat-rate') expect(unpricedModelHint()).not.toContain('Fix: codeburn model-alias') }) + + it('names both hatches for a concrete unknown SKU', () => { + const hint = unpricedModelHint('zz-new-subscription-pass-sku') + expect(hint).toContain('codeburn model-alias "zz-new-subscription-pass-sku"') + expect(hint).toContain('codeburn model-flat-rate "zz-new-subscription-pass-sku"') + expect(hint).toContain('If a model is billed per token') + expect(hint).toContain('If $0 is correct') + }) +}) + +describe('calculateCost verbose unknown-model warning', () => { + it('does not present model-alias as the only fix for an unknown SKU', () => { + const previous = process.env['CODEBURN_VERBOSE'] + process.env['CODEBURN_VERBOSE'] = '1' + const chunks: string[] = [] + const originalWrite = process.stderr.write.bind(process.stderr) + process.stderr.write = ((chunk: string | Uint8Array, ...args: unknown[]) => { + chunks.push(typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString()) + return (originalWrite as (chunk: string | Uint8Array, ...rest: unknown[]) => boolean)(chunk, ...args) + }) as typeof process.stderr.write + try { + expect(calculateCost('zz-new-subscription-pass-sku', 10, 10, 0, 0, 0)).toBe(0) + } finally { + process.stderr.write = originalWrite + if (previous === undefined) delete process.env['CODEBURN_VERBOSE'] + else process.env['CODEBURN_VERBOSE'] = previous + } + const text = chunks.join('') + expect(text).toContain('zz-new-subscription-pass-sku') + expect(text).toContain('If a model is billed per token') + expect(text).toContain('model-flat-rate') + expect(text).toContain('model-alias') + expect(text).not.toMatch(/Map it with: codeburn model-alias/) + }) })