Skip to content

Commit d733781

Browse files
committed
Merge branch 'main' into fix/issue-1316-openrouter-kimi-k3
Main-first parent shape for the CI mutation gate: the gate's resolvePullRequestBase resolves a merge-commit head to its first parent, so current main in the first-parent position makes the gate measure the true PR delta (the branch's last main sync predates the gate rewrite, which would otherwise inflate the measured delta with main commits).
2 parents 7328cbf + bc5d369 commit d733781

5 files changed

Lines changed: 402 additions & 3 deletions

File tree

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

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import { Anthropic } from "@anthropic-ai/sdk"
1818
import OpenAI from "openai"
1919

2020
import { providerIdentifiers } from "@roo-code/types"
21+
import type { ModelInfo } from "@roo-code/types"
22+
23+
import { applyOpenRouterMoonshotK3Profile } from "../fetchers/openrouter"
2124

2225
import { OpenRouterHandler } from "../openrouter"
2326
import { 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+
4657
vitest.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",

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

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ import * as path from "path"
44

55
import { back as nockBack } from "nock"
66

7-
import { getOpenRouterModelEndpoints, getOpenRouterModels, parseOpenRouterModel } from "../openrouter"
7+
import type { ModelInfo } from "@roo-code/types"
8+
9+
import {
10+
applyOpenRouterMoonshotK3Profile,
11+
getOpenRouterModelEndpoints,
12+
getOpenRouterModels,
13+
parseOpenRouterModel,
14+
} from "../openrouter"
815

916
nockBack.fixtures = path.join(__dirname, "fixtures")
1017
nockBack.setMode("lockdown")
@@ -522,6 +529,63 @@ describe("OpenRouter API", () => {
522529
expect(result.contextWindow).toBe(128000)
523530
})
524531

532+
it("applies the Moonshot K3 profile for moonshotai/kimi-k3", () => {
533+
const mockModel = {
534+
name: "Kimi K3",
535+
description: "Test model",
536+
context_length: 1000000,
537+
max_completion_tokens: null,
538+
pricing: {
539+
prompt: "0.0000006",
540+
completion: "0.000003",
541+
},
542+
}
543+
544+
const result = parseOpenRouterModel({
545+
id: "moonshotai/kimi-k3",
546+
model: mockModel,
547+
inputModality: ["text", "image"],
548+
outputModality: ["text"],
549+
maxTokens: null,
550+
supportedParameters: ["reasoning"],
551+
})
552+
553+
expect(result.maxTokens).toBe(32768)
554+
expect(result.contextWindow).toBe(1000000)
555+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
556+
expect(result.reasoningEffort).toBe("high")
557+
expect(result.supportsTemperature).toBe(true)
558+
expect(result.defaultTemperature).toBe(1)
559+
})
560+
561+
it("applies the Moonshot K3 profile for ~moonshotai/kimi-latest", () => {
562+
const mockModel = {
563+
name: "Kimi Latest",
564+
description: "Test model",
565+
context_length: 1000000,
566+
max_completion_tokens: null,
567+
pricing: {
568+
prompt: "0.0000006",
569+
completion: "0.000003",
570+
},
571+
}
572+
573+
const result = parseOpenRouterModel({
574+
id: "~moonshotai/kimi-latest",
575+
model: mockModel,
576+
inputModality: ["text", "image"],
577+
outputModality: ["text"],
578+
maxTokens: null,
579+
supportedParameters: ["reasoning"],
580+
})
581+
582+
expect(result.maxTokens).toBe(32768)
583+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
584+
expect(result.reasoningEffort).toBe("high")
585+
expect(result.supportsTemperature).toBe(true)
586+
expect(result.defaultTemperature).toBe(1)
587+
})
588+
525589
it("does not override max tokens for other models", () => {
526590
const mockModel = {
527591
name: "Other Model",
@@ -640,4 +704,59 @@ describe("OpenRouter API", () => {
640704
expect(resultWithoutTools.supportedParameters).toContain("max_tokens")
641705
})
642706
})
707+
708+
describe("applyOpenRouterMoonshotK3Profile", () => {
709+
it("overrides stale cached values for moonshotai/kimi-k3", () => {
710+
const stale: ModelInfo = {
711+
maxTokens: 209716,
712+
contextWindow: 1000000,
713+
supportsImages: true,
714+
supportsPromptCache: true,
715+
inputPrice: 0.6,
716+
outputPrice: 3,
717+
supportsReasoningEffort: true,
718+
}
719+
720+
const result = applyOpenRouterMoonshotK3Profile("moonshotai/kimi-k3", stale)
721+
722+
expect(result).toEqual({
723+
...stale,
724+
maxTokens: 32768,
725+
supportsReasoningEffort: ["low", "high", "max"],
726+
reasoningEffort: "high",
727+
supportsTemperature: true,
728+
defaultTemperature: 1,
729+
})
730+
// The original record (e.g. a shared cache entry) must not be mutated.
731+
expect(stale.maxTokens).toBe(209716)
732+
expect(stale.supportsReasoningEffort).toBe(true)
733+
})
734+
735+
it("applies the profile to ~moonshotai/kimi-latest", () => {
736+
const stale: ModelInfo = {
737+
maxTokens: 209716,
738+
contextWindow: 1000000,
739+
supportsPromptCache: true,
740+
supportsReasoningEffort: true,
741+
}
742+
743+
const result = applyOpenRouterMoonshotK3Profile("~moonshotai/kimi-latest", stale)
744+
745+
expect(result.maxTokens).toBe(32768)
746+
expect(result.supportsReasoningEffort).toEqual(["low", "high", "max"])
747+
expect(result.reasoningEffort).toBe("high")
748+
expect(result.supportsTemperature).toBe(true)
749+
expect(result.defaultTemperature).toBe(1)
750+
})
751+
752+
it("returns other models unchanged", () => {
753+
const info: ModelInfo = {
754+
maxTokens: 8192,
755+
contextWindow: 200000,
756+
supportsPromptCache: true,
757+
}
758+
759+
expect(applyOpenRouterMoonshotK3Profile("openai/gpt-4o", info)).toBe(info)
760+
})
761+
})
643762
})

0 commit comments

Comments
 (0)