@@ -60,6 +60,8 @@ import type {
6060 ApiStreamUsageChunk ,
6161} from "../../transform/stream"
6262import { makeCreateMessageMetadata } from "../../../test-utils/api"
63+ import { mistralModels } from "@roo-code/types"
64+ import { calculateApiCostOpenAI } from "../../../shared/cost"
6365
6466describe ( "MistralHandler" , ( ) => {
6567 let handler : MistralHandler
@@ -451,6 +453,216 @@ describe("MistralHandler", () => {
451453 expect ( results [ 0 ] ) . toEqual ( { type : "text" , text : "Test response" } )
452454 expect ( results [ 1 ] ) . toEqual ( { type : "usage" , inputTokens : 12 , outputTokens : 34 } )
453455 } )
456+
457+ it ( "reports cache reads from prompt_tokens_details.cached_tokens without inflating input tokens" , async ( ) => {
458+ // Mistral's documented wire shape: prompt_tokens is the TOTAL input
459+ // count and cached_tokens is the cached subset. The pinned SDK has no
460+ // typed field for the details object, so it arrives under
461+ // additionalProperties after the SDK parse.
462+ mockCreate . mockImplementationOnce ( async ( _options ) =>
463+ asyncStreamFrom ( [
464+ {
465+ data : {
466+ choices : [ { delta : { content : "Test response" } , index : 0 } ] ,
467+ } ,
468+ } ,
469+ {
470+ data : {
471+ choices : [ ] ,
472+ usage : {
473+ promptTokens : 1013 ,
474+ completionTokens : 30 ,
475+ additionalProperties : { prompt_tokens_details : { cached_tokens : 1008 } } ,
476+ } ,
477+ } ,
478+ } ,
479+ ] ) ,
480+ )
481+
482+ const results = await collectStream ( handler . createMessage ( systemPrompt , messages ) )
483+
484+ expect ( results [ 1 ] ) . toEqual ( {
485+ type : "usage" ,
486+ inputTokens : 1013 ,
487+ outputTokens : 30 ,
488+ cacheReadTokens : 1008 ,
489+ } )
490+ } )
491+
492+ it ( "omits cacheReadTokens when the usage carries no cached_tokens detail" , async ( ) => {
493+ mockCreate . mockImplementationOnce ( async ( _options ) =>
494+ asyncStreamFrom ( [
495+ {
496+ data : {
497+ choices : [ ] ,
498+ usage : { promptTokens : 12 , completionTokens : 34 } ,
499+ } ,
500+ } ,
501+ ] ) ,
502+ )
503+
504+ const results = await collectStream ( handler . createMessage ( systemPrompt , messages ) )
505+
506+ expect ( results [ 0 ] ) . toEqual ( { type : "usage" , inputTokens : 12 , outputTokens : 34 } )
507+ } )
508+
509+ it ( "omits cacheReadTokens when cached_tokens is zero" , async ( ) => {
510+ // Mistral reports 0 (or omits the field) when nothing was served from
511+ // cache; a zero read must not become a reported cache-read chunk field.
512+ mockCreate . mockImplementationOnce ( async ( _options ) =>
513+ asyncStreamFrom ( [
514+ {
515+ data : {
516+ choices : [ ] ,
517+ usage : {
518+ promptTokens : 1013 ,
519+ completionTokens : 30 ,
520+ additionalProperties : { prompt_tokens_details : { cached_tokens : 0 } } ,
521+ } ,
522+ } ,
523+ } ,
524+ ] ) ,
525+ )
526+
527+ const results = await collectStream ( handler . createMessage ( systemPrompt , messages ) )
528+
529+ expect ( results [ 0 ] ) . toEqual ( { type : "usage" , inputTokens : 1013 , outputTokens : 30 } )
530+ } )
531+
532+ it ( "ignores a non-integer cached_tokens value" , async ( ) => {
533+ mockCreate . mockImplementationOnce ( async ( _options ) =>
534+ asyncStreamFrom ( [
535+ {
536+ data : {
537+ choices : [ ] ,
538+ usage : {
539+ promptTokens : 1013 ,
540+ completionTokens : 30 ,
541+ additionalProperties : { prompt_tokens_details : { cached_tokens : 100.5 } } ,
542+ } ,
543+ } ,
544+ } ,
545+ ] ) ,
546+ )
547+
548+ const results = await collectStream ( handler . createMessage ( systemPrompt , messages ) )
549+
550+ expect ( results [ 0 ] ) . toEqual ( { type : "usage" , inputTokens : 1013 , outputTokens : 30 } )
551+ } )
552+
553+ it ( "drops cached_tokens that exceed prompt_tokens instead of billing an impossible read" , async ( ) => {
554+ // Subset guard, mirroring the OpenAI sibling that reads the same nested
555+ // field (openai.ts:304-313): a malformed upstream count larger than the
556+ // prompt total must not bill reads at 0.1x or surface cacheReads > tokensIn.
557+ mockCreate . mockImplementationOnce ( async ( _options ) =>
558+ asyncStreamFrom ( [
559+ {
560+ data : {
561+ choices : [ ] ,
562+ usage : {
563+ promptTokens : 100 ,
564+ completionTokens : 30 ,
565+ additionalProperties : { prompt_tokens_details : { cached_tokens : 1008 } } ,
566+ } ,
567+ } ,
568+ } ,
569+ ] ) ,
570+ )
571+
572+ const results = await collectStream ( handler . createMessage ( systemPrompt , messages ) )
573+
574+ expect ( results [ 0 ] ) . toEqual ( { type : "usage" , inputTokens : 100 , outputTokens : 30 } )
575+ } )
576+
577+ it ( "keeps a cached_tokens value equal to prompt_tokens (inclusive subset bound)" , async ( ) => {
578+ // The sibling guard is `<=`, not `<`: a fully cached prompt is valid.
579+ mockCreate . mockImplementationOnce ( async ( _options ) =>
580+ asyncStreamFrom ( [
581+ {
582+ data : {
583+ choices : [ ] ,
584+ usage : {
585+ promptTokens : 1008 ,
586+ completionTokens : 30 ,
587+ additionalProperties : { prompt_tokens_details : { cached_tokens : 1008 } } ,
588+ } ,
589+ } ,
590+ } ,
591+ ] ) ,
592+ )
593+
594+ const results = await collectStream ( handler . createMessage ( systemPrompt , messages ) )
595+
596+ expect ( results [ 0 ] ) . toEqual ( {
597+ type : "usage" ,
598+ inputTokens : 1008 ,
599+ outputTokens : 30 ,
600+ cacheReadTokens : 1008 ,
601+ } )
602+ } )
603+
604+ it ( "bills a cached read at 0.1x and the uncached remainder at full input price" , async ( ) => {
605+ // End-to-end cost proof for the coupled change: the reported usage
606+ // chunk from the handler feeds calculateApiCostOpenAI, which subtracts
607+ // the reported reads from prompt_tokens and bills them at cacheReadsPrice.
608+ const info = mistralModels [ "mistral-large-latest" ]
609+ const inputTokens = 1013
610+ const cachedTokens = 1008
611+ const outputTokens = 30
612+
613+ mockCreate . mockImplementationOnce ( async ( _options ) =>
614+ asyncStreamFrom ( [
615+ {
616+ data : {
617+ choices : [ ] ,
618+ usage : {
619+ promptTokens : inputTokens ,
620+ completionTokens : outputTokens ,
621+ additionalProperties : { prompt_tokens_details : { cached_tokens : cachedTokens } } ,
622+ } ,
623+ } ,
624+ } ,
625+ ] ) ,
626+ )
627+
628+ const chunks = await collectStream ( handler . createMessage ( systemPrompt , messages ) )
629+ const usage = chunks . find ( ( chunk ) => chunk . type === "usage" )
630+ expect ( usage ) . toMatchObject ( { type : "usage" , inputTokens, outputTokens, cacheReadTokens : cachedTokens } )
631+
632+ const { totalCost } = calculateApiCostOpenAI (
633+ info ,
634+ inputTokens ,
635+ outputTokens ,
636+ 0 ,
637+ usage ?. type === "usage" ? usage . cacheReadTokens : undefined ,
638+ )
639+
640+ const uncachedCost = ( ( inputTokens - cachedTokens ) / 1_000_000 ) * ( info . inputPrice as number )
641+ const cachedCost = ( cachedTokens / 1_000_000 ) * ( info . cacheReadsPrice as number )
642+ const outputCost = ( outputTokens / 1_000_000 ) * ( info . outputPrice as number )
643+
644+ expect ( totalCost ) . toBeCloseTo ( uncachedCost + cachedCost + outputCost , 12 )
645+
646+ // Pin the two wrong variants this PR pairs together, so neither can
647+ // regress alone: reporting reads with no price bills them at $0, and
648+ // billing the full prompt at inputPrice over-reports by 9x on the
649+ // cached fraction.
650+ const mappingWithoutPrice = calculateApiCostOpenAI (
651+ { ...info , cacheReadsPrice : undefined } ,
652+ inputTokens ,
653+ outputTokens ,
654+ 0 ,
655+ cachedTokens ,
656+ )
657+ expect ( mappingWithoutPrice . totalCost ) . toBeLessThan ( totalCost )
658+
659+ const noMapping = calculateApiCostOpenAI ( info , inputTokens , outputTokens , 0 , undefined )
660+ expect ( noMapping . totalCost ) . toBeGreaterThan ( totalCost )
661+ expect ( noMapping . totalCost - totalCost ) . toBeCloseTo (
662+ ( cachedTokens / 1_000_000 ) * ( ( info . inputPrice as number ) - ( info . cacheReadsPrice as number ) ) ,
663+ 12 ,
664+ )
665+ } )
454666 } )
455667
456668 it ( "wraps an Error raised by the stream iterator during for await" , async ( ) => {
@@ -922,8 +1134,13 @@ describe("MistralHandler", () => {
9221134 // the request. Both signals are already aborted when completePrompt's catch runs,
9231135 // so the test really pins the first-cause classification.
9241136 mockComplete . mockImplementation (
925- ( ) =>
1137+ ( _options : unknown , requestOptions ?: { fetchOptions ?: { signal ?: AbortSignal } } ) =>
9261138 new Promise < never > ( ( _resolve , reject ) => {
1139+ // Attach a listener the way a real fetch does: Node's composite
1140+ // AbortSignal.any materializes its first-cause reason at abort time
1141+ // only when something is listening, and reads the live source
1142+ // signals otherwise.
1143+ requestOptions ?. fetchOptions ?. signal ?. addEventListener ( "abort" , ( ) => { } , { once : true } )
9271144 rejectRequest = reject
9281145 } ) ,
9291146 )
0 commit comments