@@ -18,6 +18,9 @@ import { Anthropic } from "@anthropic-ai/sdk"
1818import OpenAI from "openai"
1919
2020import { providerIdentifiers } from "@roo-code/types"
21+ import type { ModelInfo } from "@roo-code/types"
22+
23+ import { applyOpenRouterMoonshotK3Profile } from "../fetchers/openrouter"
2124
2225import { OpenRouterHandler } from "../openrouter"
2326import { Package } from "../../../shared/package"
@@ -43,6 +46,14 @@ vitest.mock("@roo-code/telemetry", () => ({
4346 } ,
4447} ) )
4548
49+ const { mockGetModelEndpoints } = vitest . hoisted ( ( ) => ( {
50+ mockGetModelEndpoints : vitest . fn ( ) ,
51+ } ) )
52+
53+ vitest . mock ( "../fetchers/modelEndpointCache" , ( ) => ( {
54+ getModelEndpoints : mockGetModelEndpoints ,
55+ } ) )
56+
4657vitest . mock ( "../fetchers/modelCache" , ( ) => ( {
4758 getModels : vitest . fn ( ) . mockImplementation ( function ( ) {
4859 return Promise . resolve ( {
@@ -101,6 +112,19 @@ vitest.mock("../fetchers/modelCache", () => ({
101112 excludedTools : [ "existing_excluded" ] ,
102113 includedTools : [ "existing_included" ] ,
103114 } ,
115+ // Stale cache record simulating what users cached before the Moonshot K3
116+ // profile existed: the fabricated 0.2 context-window max_tokens and a
117+ // boolean supportsReasoningEffort with no default effort.
118+ "moonshotai/kimi-k3" : {
119+ maxTokens : 209716 ,
120+ contextWindow : 1000000 ,
121+ supportsImages : true ,
122+ supportsPromptCache : true ,
123+ inputPrice : 0.6 ,
124+ outputPrice : 3 ,
125+ description : "Kimi K3" ,
126+ supportsReasoningEffort : true ,
127+ } ,
104128 } )
105129 } ) ,
106130 refreshModels : vitest . fn ( async ( options ) => {
@@ -115,7 +139,13 @@ describe("OpenRouterHandler", () => {
115139 openRouterModelId : "anthropic/claude-sonnet-4" ,
116140 } )
117141
118- beforeEach ( ( ) => clearAllMocks ( ) )
142+ beforeEach ( ( ) => {
143+ clearAllMocks ( )
144+ // Endpoint records default to "not fetched" (same as the real guard for
145+ // missing options); tests that exercise the specific-provider branch
146+ // override this per test.
147+ mockGetModelEndpoints . mockResolvedValue ( { } )
148+ } )
119149
120150 it ( "initializes with correct options" , ( ) => {
121151 const handler = new OpenRouterHandler ( mockOptions )
@@ -237,8 +267,122 @@ describe("OpenRouterHandler", () => {
237267 expect ( result . info . excludedTools ) . toBeUndefined ( )
238268 expect ( result . info . includedTools ) . toBeUndefined ( )
239269 } )
270+
271+ it ( "applies the Moonshot K3 profile to stale cached model info" , async ( ) => {
272+ const handler = new OpenRouterHandler (
273+ makeApiHandlerOptions ( {
274+ openRouterApiKey : "test-key" ,
275+ openRouterModelId : "moonshotai/kimi-k3" ,
276+ } ) ,
277+ )
278+
279+ const result = await handler . fetchModel ( )
280+
281+ // The stale cache record carried a fabricated max_tokens (209716) and a
282+ // boolean supportsReasoningEffort with no default effort; the profile must
283+ // correct all of that before any request parameters are derived.
284+ expect ( result . id ) . toBe ( "moonshotai/kimi-k3" )
285+ expect ( result . maxTokens ) . toBe ( 32768 )
286+ expect ( result . temperature ) . toBe ( 1 )
287+ expect ( result . reasoningEffort ) . toBe ( "high" )
288+ expect ( result . reasoning ) . toEqual ( { effort : "high" } )
289+ expect ( result . info . maxTokens ) . toBe ( 32768 )
290+ expect ( result . info . supportsReasoningEffort ) . toEqual ( [ "low" , "high" , "max" ] )
291+ expect ( result . info . reasoningEffort ) . toBe ( "high" )
292+ expect ( result . info . supportsTemperature ) . toBe ( true )
293+ expect ( result . info . defaultTemperature ) . toBe ( 1 )
294+ } )
295+
296+ it ( "applies the Moonshot K3 profile to a stale specific-provider endpoint record" , async ( ) => {
297+ // Endpoint records are cached separately from the parent model cache and
298+ // are selected before the profile is applied. A stale endpoint record
299+ // (fabricated max_tokens, boolean supportsReasoningEffort, no reasoning
300+ // default, no temperature default) must be corrected for the selected
301+ // endpoint.
302+ mockGetModelEndpoints . mockResolvedValue ( {
303+ moonshotai : {
304+ maxTokens : 209716 ,
305+ contextWindow : 1000000 ,
306+ supportsImages : true ,
307+ supportsPromptCache : true ,
308+ inputPrice : 0.6 ,
309+ outputPrice : 3 ,
310+ description : "Kimi K3 (Moonshot endpoint)" ,
311+ supportsReasoningEffort : true ,
312+ } ,
313+ } )
314+
315+ const handler = new OpenRouterHandler (
316+ makeApiHandlerOptions ( {
317+ openRouterApiKey : "test-key" ,
318+ openRouterModelId : "moonshotai/kimi-k3" ,
319+ openRouterSpecificProvider : "moonshotai" ,
320+ } ) ,
321+ )
322+
323+ const result = await handler . fetchModel ( )
324+
325+ // The selected endpoint record (not the parent model cache entry) must be
326+ // profiled: a regression that applied the profile before endpoint
327+ // selection would send the stale endpoint values.
328+ expect ( result . id ) . toBe ( "moonshotai/kimi-k3" )
329+ expect ( result . info . maxTokens ) . toBe ( 32768 )
330+ expect ( result . info . supportsReasoningEffort ) . toEqual ( [ "low" , "high" , "max" ] )
331+ expect ( result . info . reasoningEffort ) . toBe ( "high" )
332+ expect ( result . info . supportsTemperature ) . toBe ( true )
333+ expect ( result . info . defaultTemperature ) . toBe ( 1 )
334+ expect ( result . maxTokens ) . toBe ( 32768 )
335+ expect ( result . temperature ) . toBe ( 1 )
336+ expect ( result . reasoningEffort ) . toBe ( "high" )
337+ expect ( result . reasoning ) . toEqual ( { effort : "high" } )
338+ } )
240339 } )
241340
341+ describe ( "applyOpenRouterMoonshotK3Profile" , ( ) => {
342+ // Stale cache record shape: fabricated 0.2 context-window max_tokens and a
343+ // boolean supportsReasoningEffort with no default effort (pre-profile).
344+ const staleK3Record : ModelInfo = {
345+ maxTokens : 209_716 ,
346+ contextWindow : 1_000_000 ,
347+ supportsImages : true ,
348+ supportsPromptCache : true ,
349+ inputPrice : 0.6 ,
350+ outputPrice : 3 ,
351+ description : "Kimi K3 (stale cache)" ,
352+ supportsReasoningEffort : true ,
353+ }
354+
355+ it ( "applies the profile to the exact moonshotai/kimi-k3 id" , ( ) => {
356+ const result = applyOpenRouterMoonshotK3Profile ( "moonshotai/kimi-k3" , { ...staleK3Record } )
357+
358+ expect ( result . maxTokens ) . toBe ( 32_768 )
359+ expect ( result . supportsReasoningEffort ) . toEqual ( [ "low" , "high" , "max" ] )
360+ expect ( result . reasoningEffort ) . toBe ( "high" )
361+ expect ( result . supportsTemperature ) . toBe ( true )
362+ expect ( result . defaultTemperature ) . toBe ( 1.0 )
363+ // Unprofiled fields pass through from the (stale) record
364+ expect ( result . contextWindow ) . toBe ( 1_000_000 )
365+ expect ( result . description ) . toBe ( "Kimi K3 (stale cache)" )
366+ } )
367+
368+ it ( "applies the profile to the tilde-prefixed rolling alias" , ( ) => {
369+ const result = applyOpenRouterMoonshotK3Profile ( "~moonshotai/kimi-latest" , { ...staleK3Record } )
370+
371+ // The catalogue keeps the ~ prefix on the rolling alias id, so the profile
372+ // must recognize the exact string rather than a stripped or bare alias
373+ expect ( result . maxTokens ) . toBe ( 32_768 )
374+ expect ( result . supportsReasoningEffort ) . toEqual ( [ "low" , "high" , "max" ] )
375+ expect ( result . reasoningEffort ) . toBe ( "high" )
376+ expect ( result . supportsTemperature ) . toBe ( true )
377+ expect ( result . defaultTemperature ) . toBe ( 1.0 )
378+ } )
379+
380+ it ( "leaves non-K3 model records untouched" , ( ) => {
381+ const record : ModelInfo = { ...staleK3Record }
382+
383+ expect ( applyOpenRouterMoonshotK3Profile ( "moonshotai/kimi-k2-thinking" , record ) ) . toBe ( record )
384+ } )
385+ } )
242386 describe ( "createMessage" , ( ) => {
243387 it ( "generates correct stream chunks" , async ( ) => {
244388 const handler = new OpenRouterHandler ( mockOptions )
@@ -546,6 +690,44 @@ describe("OpenRouterHandler", () => {
546690 expect ( endChunks [ 0 ] . id ) . toBe ( "call_openrouter_test" )
547691 } )
548692
693+ it ( "sends profiled max_tokens, explicit temperature 1.0, and reasoning effort for moonshotai/kimi-k3" , async ( ) => {
694+ const handler = new OpenRouterHandler (
695+ makeApiHandlerOptions ( {
696+ openRouterApiKey : "test-key" ,
697+ openRouterModelId : "moonshotai/kimi-k3" ,
698+ } ) ,
699+ )
700+
701+ const mockStream = asyncStreamFrom ( [ { id : "test-id" , choices : [ { delta : { content : "ok" } } ] } ] )
702+ const mockCreate = vitest . fn ( ) . mockResolvedValue ( mockStream )
703+ const chatStub = { completions : { create : mockCreate } }
704+ // The vitest-mocked OpenAI class is structurally incompatible with the narrow
705+ // chat stub; the double assertion routes through unknown (instead of any) to
706+ // keep this file's no-explicit-any budget flat.
707+ const openAiPrototype = OpenAI as unknown as { prototype : { chat ?: unknown } }
708+ const originalChat = openAiPrototype . prototype . chat
709+ openAiPrototype . prototype . chat = chatStub
710+
711+ try {
712+ const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" , content : "test message" } ]
713+ await collectStream ( handler . createMessage ( "test system prompt" , messages ) )
714+
715+ const [ requestParams ] = mockCreate . mock . calls [ 0 ] as [ Record < string , unknown > ]
716+ expect ( requestParams ) . toMatchObject ( {
717+ model : "moonshotai/kimi-k3" ,
718+ max_tokens : 32768 ,
719+ temperature : 1 ,
720+ reasoning : { effort : "high" } ,
721+ } )
722+ // K3 is fixed at temperature 1.0 upstream (issue #1316); the request
723+ // must carry it explicitly.
724+ } finally {
725+ // Restore the original prototype property so this stub cannot leak
726+ // into later tests; clearAllMocks does not undo prototype assignment.
727+ openAiPrototype . prototype . chat = originalChat
728+ }
729+ } )
730+
549731 it ( "emits completion only for identified calls and clears completed IDs" , async ( ) => {
550732 const toolCall = ( id ?: string ) => ( {
551733 id : "stream" ,
0 commit comments