Skip to content

Commit 6641383

Browse files
committed
fix(openrouter): address PR 1325 review feedback for Kimi K3
- Send explicit temperature: 1.0 for Moonshot K3 via OpenRouter instead of omitting it: issue Zoo-Code-Org#1316's expected result requires the request to carry temperature: 1.0, matching the direct Moonshot provider profile (defaultTemperature: 1.0, "temperature is fixed at 1.0"). - Add a handler-level regression test for the openRouterSpecificProvider endpoint branch: a stale endpoint record (fabricated max_tokens, boolean supportsReasoningEffort) is corrected at consumption time (CodeRabbit pre-merge check: regression evidence for the endpoint path). - Restore OpenAI.prototype.chat in the Kimi K3 wire test via try/finally so the stub cannot leak into later tests; clearAllMocks does not undo prototype assignment (CodeRabbit actionable comment).
1 parent 57d2cc5 commit 6641383

3 files changed

Lines changed: 99 additions & 25 deletions

File tree

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

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ vitest.mock("@roo-code/telemetry", () => ({
4242
},
4343
}))
4444

45+
const { mockGetModelEndpoints } = vitest.hoisted(() => ({
46+
mockGetModelEndpoints: vitest.fn(),
47+
}))
48+
49+
vitest.mock("../fetchers/modelEndpointCache", () => ({
50+
getModelEndpoints: mockGetModelEndpoints,
51+
}))
52+
4553
vitest.mock("../fetchers/modelCache", () => ({
4654
getModels: vitest.fn().mockImplementation(function () {
4755
return Promise.resolve({
@@ -127,7 +135,13 @@ describe("OpenRouterHandler", () => {
127135
openRouterModelId: "anthropic/claude-sonnet-4",
128136
})
129137

130-
beforeEach(() => clearAllMocks())
138+
beforeEach(() => {
139+
clearAllMocks()
140+
// Endpoint records default to "not fetched" (same as the real guard for
141+
// missing options); tests that exercise the specific-provider branch
142+
// override this per test.
143+
mockGetModelEndpoints.mockResolvedValue({})
144+
})
131145

132146
it("initializes with correct options", () => {
133147
const handler = new OpenRouterHandler(mockOptions)
@@ -265,13 +279,58 @@ describe("OpenRouterHandler", () => {
265279
// correct all of that before any request parameters are derived.
266280
expect(result.id).toBe("moonshotai/kimi-k3")
267281
expect(result.maxTokens).toBe(32768)
268-
expect(result.temperature).toBeUndefined()
282+
expect(result.temperature).toBe(1)
269283
expect(result.reasoningEffort).toBe("high")
270284
expect(result.reasoning).toEqual({ effort: "high" })
271285
expect(result.info.maxTokens).toBe(32768)
272286
expect(result.info.supportsReasoningEffort).toEqual(["low", "high", "max"])
273287
expect(result.info.reasoningEffort).toBe("high")
274-
expect(result.info.supportsTemperature).toBe(false)
288+
expect(result.info.supportsTemperature).toBe(true)
289+
expect(result.info.defaultTemperature).toBe(1)
290+
})
291+
292+
it("applies the Moonshot K3 profile to a stale specific-provider endpoint record", async () => {
293+
// Endpoint records are cached separately from the parent model cache and
294+
// are selected before the profile is applied. A stale endpoint record
295+
// (fabricated max_tokens, boolean supportsReasoningEffort, no reasoning
296+
// default, no temperature default) must be corrected for the selected
297+
// endpoint.
298+
mockGetModelEndpoints.mockResolvedValue({
299+
moonshotai: {
300+
maxTokens: 209716,
301+
contextWindow: 1000000,
302+
supportsImages: true,
303+
supportsPromptCache: true,
304+
inputPrice: 0.6,
305+
outputPrice: 3,
306+
description: "Kimi K3 (Moonshot endpoint)",
307+
supportsReasoningEffort: true,
308+
},
309+
})
310+
311+
const handler = new OpenRouterHandler(
312+
makeApiHandlerOptions({
313+
openRouterApiKey: "test-key",
314+
openRouterModelId: "moonshotai/kimi-k3",
315+
openRouterSpecificProvider: "moonshotai",
316+
}),
317+
)
318+
319+
const result = await handler.fetchModel()
320+
321+
// The selected endpoint record (not the parent model cache entry) must be
322+
// profiled: a regression that applied the profile before endpoint
323+
// selection would send the stale endpoint values.
324+
expect(result.id).toBe("moonshotai/kimi-k3")
325+
expect(result.info.maxTokens).toBe(32768)
326+
expect(result.info.supportsReasoningEffort).toEqual(["low", "high", "max"])
327+
expect(result.info.reasoningEffort).toBe("high")
328+
expect(result.info.supportsTemperature).toBe(true)
329+
expect(result.info.defaultTemperature).toBe(1)
330+
expect(result.maxTokens).toBe(32768)
331+
expect(result.temperature).toBe(1)
332+
expect(result.reasoningEffort).toBe("high")
333+
expect(result.reasoning).toEqual({ effort: "high" })
275334
})
276335
})
277336

@@ -581,7 +640,7 @@ describe("OpenRouterHandler", () => {
581640
expect(endChunks[0].id).toBe("call_openrouter_test")
582641
})
583642

584-
it("sends profiled max_tokens and reasoning effort, omitting temperature, for moonshotai/kimi-k3", async () => {
643+
it("sends profiled max_tokens, explicit temperature 1.0, and reasoning effort for moonshotai/kimi-k3", async () => {
585644
const handler = new OpenRouterHandler(
586645
makeApiHandlerOptions({
587646
openRouterApiKey: "test-key",
@@ -595,19 +654,28 @@ describe("OpenRouterHandler", () => {
595654
// The vitest-mocked OpenAI class is structurally incompatible with the narrow
596655
// chat stub; the double assertion routes through unknown (instead of any) to
597656
// keep this file's no-explicit-any budget flat.
598-
;(OpenAI as unknown as { prototype: { chat: typeof chatStub } }).prototype.chat = chatStub
599-
600-
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "test message" }]
601-
await collectStream(handler.createMessage("test system prompt", messages))
602-
603-
const [requestParams] = mockCreate.mock.calls[0] as [Record<string, unknown>]
604-
expect(requestParams).toMatchObject({
605-
model: "moonshotai/kimi-k3",
606-
max_tokens: 32768,
607-
reasoning: { effort: "high" },
608-
})
609-
// K3 ignores temperature (fixed at 1.0 server-side); the request must omit it.
610-
expect(requestParams.temperature).toBeUndefined()
657+
const openAiPrototype = OpenAI as unknown as { prototype: { chat?: unknown } }
658+
const originalChat = openAiPrototype.prototype.chat
659+
openAiPrototype.prototype.chat = chatStub
660+
661+
try {
662+
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "test message" }]
663+
await collectStream(handler.createMessage("test system prompt", messages))
664+
665+
const [requestParams] = mockCreate.mock.calls[0] as [Record<string, unknown>]
666+
expect(requestParams).toMatchObject({
667+
model: "moonshotai/kimi-k3",
668+
max_tokens: 32768,
669+
temperature: 1,
670+
reasoning: { effort: "high" },
671+
})
672+
// K3 is fixed at temperature 1.0 upstream (issue #1316); the request
673+
// must carry it explicitly.
674+
} finally {
675+
// Restore the original prototype property so this stub cannot leak
676+
// into later tests; clearAllMocks does not undo prototype assignment.
677+
openAiPrototype.prototype.chat = originalChat
678+
}
611679
})
612680
})
613681

‎src/api/providers/fetchers/__tests__/openrouter.spec.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ describe("OpenRouter API", () => {
454454
expect(result.contextWindow).toBe(1000000)
455455
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
456456
expect(result.reasoningEffort).toBe("high")
457-
expect(result.supportsTemperature).toBe(false)
457+
expect(result.supportsTemperature).toBe(true)
458+
expect(result.defaultTemperature).toBe(1)
458459
})
459460

460461
it("applies the Moonshot K3 profile for moonshotai/kimi-latest", () => {
@@ -481,7 +482,8 @@ describe("OpenRouter API", () => {
481482
expect(result.maxTokens).toBe(32768)
482483
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
483484
expect(result.reasoningEffort).toBe("high")
484-
expect(result.supportsTemperature).toBe(false)
485+
expect(result.supportsTemperature).toBe(true)
486+
expect(result.defaultTemperature).toBe(1)
485487
})
486488

487489
it("does not override max tokens for other models", () => {
@@ -622,7 +624,8 @@ describe("OpenRouter API", () => {
622624
maxTokens: 32768,
623625
supportsReasoningEffort: ["low", "high", "max"],
624626
reasoningEffort: "high",
625-
supportsTemperature: false,
627+
supportsTemperature: true,
628+
defaultTemperature: 1,
626629
})
627630
// The original record (e.g. a shared cache entry) must not be mutated.
628631
expect(stale.maxTokens).toBe(209716)
@@ -642,7 +645,8 @@ describe("OpenRouter API", () => {
642645
expect(result.maxTokens).toBe(32768)
643646
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
644647
expect(result.reasoningEffort).toBe("high")
645-
expect(result.supportsTemperature).toBe(false)
648+
expect(result.supportsTemperature).toBe(true)
649+
expect(result.defaultTemperature).toBe(1)
646650
})
647651

648652
it("returns other models unchanged", () => {

‎src/api/providers/fetchers/openrouter.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,19 @@ export async function getOpenRouterModelEndpoints(
186186
* OpenRouter reports `max_completion_tokens: null` for these models, so the
187187
* generic 0.2 context-window fallback would fabricate an inflated max_tokens
188188
* value (e.g. 209,716 for a 1M context window). K3 also always reasons with a
189-
* low/high/max effort ladder (default "high") and ignores temperature
190-
* (fixed at 1.0), so its wire-safe capability flags are profiled here instead
191-
* of being derived from the catalogue.
189+
* low/high/max effort ladder (default "high") and is fixed at temperature 1.0
190+
* (issue #1316 expects requests to carry an explicit `temperature: 1.0`), so
191+
* its wire-safe capability flags and temperature default are profiled here
192+
* instead of being derived from the catalogue.
192193
*/
193194
export const OPENROUTER_MOONSHOT_K3_MODELS = new Set<string>(["moonshotai/kimi-k3", "moonshotai/kimi-latest"])
194195

195196
const MOONSHOT_K3_OPENROUTER_PROFILE: Partial<ModelInfo> = {
196197
maxTokens: 32_768,
197198
supportsReasoningEffort: ["low", "high", "max"],
198199
reasoningEffort: "high",
199-
supportsTemperature: false,
200+
supportsTemperature: true,
201+
defaultTemperature: 1.0, // K3 is fixed at 1.0 upstream; send it explicitly (issue #1316)
200202
}
201203

202204
/**

0 commit comments

Comments
 (0)