Skip to content

Commit 07fb3e1

Browse files
committed
Merge remote-tracking branch 'origin/main' into fix/lint-knip-hygiene-2jj44n69c6pch
# Conflicts: # webview-ui/eslint.config.mjs
2 parents 841c94d + 57307f6 commit 07fb3e1

44 files changed

Lines changed: 1564 additions & 588 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { ModelInfo } from "../model.js"
2+
import { mistralModels } from "../providers/mistral.js"
3+
4+
const modelEntries: [string, ModelInfo][] = Object.entries(mistralModels)
5+
6+
describe("mistral registry cache pricing", () => {
7+
it("covers all nine catalog entries", () => {
8+
expect(modelEntries).toHaveLength(9)
9+
})
10+
11+
it.each(modelEntries)("%s bills cache reads at 10%% of its input price", (id, info) => {
12+
expect(id).toBeTruthy()
13+
expect(info.inputPrice).toBeGreaterThan(0)
14+
expect(info.cacheReadsPrice).toBeDefined()
15+
// Mistral bills cached prompt tokens at 10% of the standard input price.
16+
expect(info.cacheReadsPrice).toBeCloseTo((info.inputPrice as number) * 0.1, 10)
17+
})
18+
19+
it.each(modelEntries)("%s keeps the request-side cache marker off", (id, info) => {
20+
// Mistral takes no cache_control markers in the request body, so the flag
21+
// that gates marker insertion stays false even though reads are reported.
22+
expect(id).toBeTruthy()
23+
expect(info.supportsPromptCache).toBe(false)
24+
})
25+
})

‎packages/types/src/providers/mistral.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import type { ModelInfo } from "../model.js"
33
// https://docs.mistral.ai/getting-started/models/models_overview/
44
export type MistralModelId = keyof typeof mistralModels
55

6+
// Mistral bills cached prompt tokens at 10% of the standard input price
7+
// (https://docs.mistral.ai/studio-api/conversations/advanced/prompt-caching),
8+
// so every entry below carries `cacheReadsPrice` at one tenth of its own
9+
// `inputPrice`. Reporting cache reads without this price would bill them at $0
10+
// instead of 0.1x.
611
export const mistralDefaultModelId: MistralModelId = "codestral-latest"
712

813
export const mistralModels = {
@@ -13,6 +18,7 @@ export const mistralModels = {
1318
supportsPromptCache: false,
1419
inputPrice: 2.0,
1520
outputPrice: 5.0,
21+
cacheReadsPrice: 0.2,
1622
},
1723
"devstral-medium-latest": {
1824
maxTokens: 8192,
@@ -21,6 +27,7 @@ export const mistralModels = {
2127
supportsPromptCache: false,
2228
inputPrice: 0.4,
2329
outputPrice: 2.0,
30+
cacheReadsPrice: 0.04,
2431
},
2532
"mistral-medium-latest": {
2633
maxTokens: 8192,
@@ -29,6 +36,7 @@ export const mistralModels = {
2936
supportsPromptCache: false,
3037
inputPrice: 0.4,
3138
outputPrice: 2.0,
39+
cacheReadsPrice: 0.04,
3240
},
3341
"codestral-latest": {
3442
maxTokens: 8192,
@@ -37,6 +45,7 @@ export const mistralModels = {
3745
supportsPromptCache: false,
3846
inputPrice: 0.3,
3947
outputPrice: 0.9,
48+
cacheReadsPrice: 0.03,
4049
},
4150
"mistral-large-latest": {
4251
maxTokens: 8192,
@@ -45,6 +54,7 @@ export const mistralModels = {
4554
supportsPromptCache: false,
4655
inputPrice: 2.0,
4756
outputPrice: 6.0,
57+
cacheReadsPrice: 0.2,
4858
},
4959
"ministral-8b-latest": {
5060
maxTokens: 8192,
@@ -53,6 +63,7 @@ export const mistralModels = {
5363
supportsPromptCache: false,
5464
inputPrice: 0.1,
5565
outputPrice: 0.1,
66+
cacheReadsPrice: 0.01,
5667
},
5768
"ministral-3b-latest": {
5869
maxTokens: 8192,
@@ -61,6 +72,7 @@ export const mistralModels = {
6172
supportsPromptCache: false,
6273
inputPrice: 0.04,
6374
outputPrice: 0.04,
75+
cacheReadsPrice: 0.004,
6476
},
6577
"mistral-small-latest": {
6678
maxTokens: 8192,
@@ -69,6 +81,7 @@ export const mistralModels = {
6981
supportsPromptCache: false,
7082
inputPrice: 0.2,
7183
outputPrice: 0.6,
84+
cacheReadsPrice: 0.02,
7285
},
7386
"pixtral-large-latest": {
7487
maxTokens: 8192,
@@ -77,6 +90,7 @@ export const mistralModels = {
7790
supportsPromptCache: false,
7891
inputPrice: 2.0,
7992
outputPrice: 6.0,
93+
cacheReadsPrice: 0.2,
8094
},
8195
} as const satisfies Record<string, ModelInfo>
8296

‎pnpm-lock.yaml‎

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/api/providers/__tests__/mistral.spec.ts‎

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ import type {
6060
ApiStreamUsageChunk,
6161
} from "../../transform/stream"
6262
import { makeCreateMessageMetadata } from "../../../test-utils/api"
63+
import { mistralModels } from "@roo-code/types"
64+
import { calculateApiCostOpenAI } from "../../../shared/cost"
6365

6466
describe("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

Comments
 (0)