feat(opencode): use adaptive thinking effort for kimi family on anthr… (#37696)

Co-authored-by: liqiping <liqiping@msh.team>
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
Qiping Li
2026-07-20 12:54:00 +08:00
committed by GitHub
parent cd46f22d51
commit 20a3a2138e
2 changed files with 144 additions and 8 deletions
+32 -7
View File
@@ -26,6 +26,20 @@ export function sanitizeSurrogates(content: string) {
return content.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "\uFFFD")
}
function isKimiFamily(model: Provider.Model) {
if (
[model.providerID, model.api.id].some((id) => {
const value = id.toLowerCase()
return value.includes("kimi") || value.includes("moonshot")
})
)
return true
const url = model.api.url.toLowerCase()
return ["api.kimi.com", "api.moonshot.ai", "api.moonshot.cn", "api.moonshotai.cn"].some((host) =>
url.includes(host),
)
}
// Maps npm package to the key the AI SDK expects for providerOptions
function sdkKey(npm: string): string | undefined {
switch (npm) {
@@ -707,6 +721,15 @@ export function variants(model: Provider.Model): Record<string, Record<string, a
max: { effort: "max" },
}
}
// Kimi's Anthropic-compatible transports implement adaptive thinking effort.
if (isKimiFamily(model) && ["@ai-sdk/anthropic", "@ai-sdk/google-vertex/anthropic"].includes(model.api.npm)) {
return Object.fromEntries(
["low", "medium", "high", "xhigh", "max"].map((effort) => [
effort,
{ thinking: { type: "adaptive", display: "summarized" }, effort },
]),
)
}
if (
id.includes("deepseek-chat") ||
id.includes("deepseek-reasoner") ||
@@ -1173,15 +1196,15 @@ export function options(input: {
result["thinking"] = { type: "adaptive" }
}
// Enable thinking by default for kimi models using anthropic SDK
// Moonshot's Anthropic-compatible API uses adaptive effort rather than token budgets.
// Request summaries so thinking content survives replay on subsequent turns.
if (
(input.model.api.npm === "@ai-sdk/anthropic" || input.model.api.npm === "@ai-sdk/google-vertex/anthropic") &&
(modelId.includes("k2p") || modelId.includes("kimi-k2.") || modelId.includes("kimi-k2p"))
["@ai-sdk/anthropic", "@ai-sdk/google-vertex/anthropic"].includes(input.model.api.npm) &&
isKimiFamily(input.model) &&
input.model.capabilities.reasoning
) {
result["thinking"] = {
type: "enabled",
budgetTokens: Math.min(16_000, Math.floor(input.model.limit.output / 2 - 1)),
}
result["thinking"] = { type: "adaptive", display: "summarized" }
result["effort"] = "high"
}
// Enable thinking for reasoning models on alibaba-cn (DashScope).
@@ -1705,6 +1728,8 @@ function reasoningEffort(model: Provider.Model, effort: string) {
function anthropicEffort(model: Provider.Model, effort: string) {
if (["opus-4-5", "opus-4.5"].some((value) => model.api.id.includes(value))) return { effort }
// Kimi defaults to omitting adaptive thinking text unless summarized display is requested.
if (isKimiFamily(model)) return { thinking: { type: "adaptive", display: "summarized" }, effort }
if (!anthropicAdaptiveEfforts(model.api.id)) return
return {
thinking: {