@@ -53,7 +53,14 @@ import type OpenAI from "openai"
5353import { MistralHandler } from "../mistral"
5454import type { ApiHandlerOptions } from "../../../shared/api"
5555import type { ApiHandlerCreateMessageMetadata } from "../../index"
56- import type { ApiStreamTextChunk , ApiStreamReasoningChunk , ApiStreamToolCallPartialChunk } from "../../transform/stream"
56+ import type {
57+ ApiStreamTextChunk ,
58+ ApiStreamReasoningChunk ,
59+ ApiStreamToolCallPartialChunk ,
60+ ApiStreamUsageChunk ,
61+ } from "../../transform/stream"
62+ import { calculateApiCostOpenAI } from "../../../shared/cost"
63+ import { mistralModels , type ModelInfo } from "@roo-code/types"
5764
5865describe ( "MistralHandler" , ( ) => {
5966 let handler : MistralHandler
@@ -233,6 +240,128 @@ describe("MistralHandler", () => {
233240 expect ( results [ 1 ] ) . toEqual ( { type : "reasoning" , text : "Some reasoning" } )
234241 expect ( results [ 2 ] ) . toEqual ( { type : "text" , text : "Second text" } )
235242 } )
243+
244+ it ( "should yield usage event with totalCost when stream contains usage data" , async ( ) => {
245+ // Mock stream with usage data in Mistral SSE format
246+ mockCreate . mockImplementationOnce ( async ( _options ) =>
247+ asyncStreamFrom ( [
248+ {
249+ data : {
250+ choices : [
251+ {
252+ delta : { content : "Hello" } ,
253+ index : 0 ,
254+ } ,
255+ ] ,
256+ usage : {
257+ promptTokens : 100 ,
258+ completionTokens : 50 ,
259+ } ,
260+ } ,
261+ } ,
262+ ] ) ,
263+ )
264+
265+ const iterator = handler . createMessage ( systemPrompt , messages )
266+ const results : ApiStreamUsageChunk [ ] = [ ]
267+
268+ for await ( const chunk of iterator ) {
269+ if ( chunk . type === "usage" ) {
270+ results . push ( chunk as ApiStreamUsageChunk )
271+ }
272+ }
273+
274+ expect ( results ) . toHaveLength ( 1 )
275+
276+ const modelInfo = mistralModels [ "codestral-latest" ]
277+ const expectedCost = calculateApiCostOpenAI ( modelInfo , 100 , 50 , 0 , 0 ) . totalCost
278+
279+ expect ( results [ 0 ] ) . toEqual ( {
280+ type : "usage" ,
281+ inputTokens : 100 ,
282+ outputTokens : 50 ,
283+ totalCost : expectedCost ,
284+ } )
285+ } )
286+
287+ it ( "should yield totalCost: 0 when modelInfo is not available" , async ( ) => {
288+ // Mock stream with usage data but no model info available
289+ mockCreate . mockImplementationOnce ( async ( _options ) =>
290+ asyncStreamFrom ( [
291+ {
292+ data : {
293+ choices : [
294+ {
295+ delta : { content : "Hello" } ,
296+ index : 0 ,
297+ } ,
298+ ] ,
299+ usage : {
300+ promptTokens : 100 ,
301+ completionTokens : 50 ,
302+ } ,
303+ } ,
304+ } ,
305+ ] ) ,
306+ )
307+
308+ // Spy on getModel to return undefined info.
309+ // maxTokens must be provided so that line 94 (`maxTokens ?? info.maxTokens`)
310+ // short-circuits before accessing info.maxTokens (which would crash).
311+ vi . spyOn ( handler , "getModel" ) . mockReturnValueOnce ( {
312+ id : "codestral-latest" ,
313+ // Intentionally undefined to test error handling when model info is missing
314+ info : undefined as unknown as ModelInfo ,
315+ maxTokens : 8192 ,
316+ temperature : 0 ,
317+ } as ReturnType < typeof handler . getModel > )
318+
319+ const iterator = handler . createMessage ( systemPrompt , messages )
320+ const results : ApiStreamUsageChunk [ ] = [ ]
321+
322+ for await ( const chunk of iterator ) {
323+ if ( chunk . type === "usage" ) {
324+ results . push ( chunk as ApiStreamUsageChunk )
325+ }
326+ }
327+
328+ expect ( results ) . toHaveLength ( 1 )
329+ expect ( results [ 0 ] ) . toEqual ( {
330+ type : "usage" ,
331+ inputTokens : 100 ,
332+ outputTokens : 50 ,
333+ totalCost : 0 ,
334+ } )
335+ } )
336+
337+ it ( "should not yield usage event when stream has no usage data" , async ( ) => {
338+ // Mock stream without usage field
339+ mockCreate . mockImplementationOnce ( async ( _options ) =>
340+ asyncStreamFrom ( [
341+ {
342+ data : {
343+ choices : [
344+ {
345+ delta : { content : "Hello" } ,
346+ index : 0 ,
347+ } ,
348+ ] ,
349+ } ,
350+ } ,
351+ ] ) ,
352+ )
353+
354+ const iterator = handler . createMessage ( systemPrompt , messages )
355+ const results : ApiStreamUsageChunk [ ] = [ ]
356+
357+ for await ( const chunk of iterator ) {
358+ if ( chunk . type === "usage" ) {
359+ results . push ( chunk as ApiStreamUsageChunk )
360+ }
361+ }
362+
363+ expect ( results ) . toHaveLength ( 0 )
364+ } )
236365 } )
237366
238367 describe ( "native tool calling" , ( ) => {
0 commit comments