Files
CLIProxyAPI/internal/runtime/executor/helps/devin_models.go
hkfires f110136af3 fix(thinking): clamp budgets to levels for OpenAI models
OpenAI Chat/Responses and xAI appliers accept effort levels
only, so numeric budgets on hybrid models were not clamped
to supported levels. Clear Min/Max on a copy of the model
info before conversion, preserving catalog capabilities.

Use ClampLevel for the Devin swe-1-7 medium variant so
budget-derived levels select the correct upstream UID.

Add regression tests for Copilot, Devin, and OpenAI-compat
chat covering body and suffix budgets across formats.
2026-11-23 22:45:56 +08:00

331 lines
9.6 KiB
Go

package helps
import (
"strings"
"github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
"github.com/router-for-me/CLIProxyAPI/v8/internal/thinking"
)
// knownDevinSuffixes lists recognized model uid suffixes.
var knownDevinSuffixes = []string{
"-none",
"-low",
"-medium",
"-high",
"-xhigh",
"-max",
"-fast",
"-slow",
"-priority",
"-low-priority",
"-medium-priority",
"-high-priority",
"-xhigh-priority",
"-max-priority",
"-low-fast",
"-medium-fast",
"-high-fast",
"-xhigh-fast",
"-max-fast",
"-none-fast",
"-thinking-1m",
"-thinking",
"-max-1m",
"-none-1m",
"-minimal-lightning",
"-low-lightning",
"-medium-lightning",
"-high-lightning",
"-xhigh-lightning",
"-max-lightning",
"-none-lightning",
"_none",
"_minimal",
"_low",
"_medium",
"_high",
"_xhigh",
"_max",
"_thinking",
}
// Special private Devin upstream aliases that cannot be dynamically inferred.
var specialDevinAliases = map[string]string{
"claude-haiku-4-5": "MODEL_PRIVATE_11",
"gpt-4-1": "MODEL_CHAT_GPT_4_1_2025_04_14",
}
var (
devinGPT52Model = &registry.ModelInfo{
ID: "MODEL_GPT_5_2",
Thinking: &registry.ThinkingSupport{Levels: []string{"none", "low", "medium", "high", "xhigh"}},
}
devinGeminiFlashModel = &registry.ModelInfo{
ID: "MODEL_GOOGLE_GEMINI_3_0_FLASH",
Thinking: &registry.ThinkingSupport{Levels: []string{"minimal", "low", "medium", "high"}},
}
)
// HasDevinEffortSuffix reports whether the model name already ends with a known Devin effort suffix.
func HasDevinEffortSuffix(model string) bool {
lower := strings.ToLower(strings.TrimSpace(model))
for _, s := range knownDevinSuffixes {
if strings.HasSuffix(lower, s) {
return true
}
}
return false
}
// normalizeDevinThinkingLevel resolves aliases in a lowercased, trimmed effort.
func normalizeDevinThinkingLevel(level string) string {
switch level {
case "fast":
return level
case "none", "off", "disabled":
return "none"
case "auto", "adaptive":
return string(thinking.LevelAuto)
default:
effort, _ := thinking.ParseLevelSuffix(level)
return string(effort)
}
}
// ResolveDevinChatModelUID resolves a model identifier into a valid upstream Devin chat_model_uid.
// It prioritizes dynamic catalog metadata from devin_models.json, automatically clamps
// requested efforts to supported levels, applies sensible default efforts for thinking models,
// and ensures bare non-thinking models remain bare.
func ResolveDevinChatModelUID(rawModel string, thinkingLevel string, budgetTokens int) string {
model := strings.TrimSpace(rawModel)
if model == "" {
return "swe-2-high"
}
// 1. Strip devin/ prefix if present (case-insensitive)
cleanModel := model
if strings.HasPrefix(strings.ToLower(cleanModel), "devin/") {
cleanModel = cleanModel[6:]
}
// 2. If already ends with an exact Devin effort suffix, use directly
if HasDevinEffortSuffix(cleanModel) {
return cleanModel
}
// 3. Strip CPA colon or parenthesis suffix (suffix overrides body per CPA convention)
parsedSuffix := thinking.ParseSuffix(cleanModel)
baseModel := strings.TrimSpace(parsedSuffix.ModelName)
if parsedSuffix.HasSuffix {
thinkingLevel = strings.TrimSpace(parsedSuffix.RawSuffix)
if mode, ok := thinking.ParseSpecialSuffix(thinkingLevel); ok {
thinkingLevel = mode.String()
} else if budget, ok := thinking.ParseNumericSuffix(thinkingLevel); ok {
budgetTokens = budget
if budget == 0 {
thinkingLevel = "none"
}
}
} else if colonIdx := strings.LastIndex(cleanModel, ":"); colonIdx != -1 {
baseModel = strings.TrimSpace(cleanModel[:colonIdx])
thinkingLevel = strings.TrimSpace(cleanModel[colonIdx+1:])
}
// 4. Normalize requested effort. Compound "<level>-fast" requests select upstream
// fast-mode variants (see step 10); normalization cannot represent them,
// so keep the raw level alongside the normalized effort.
rawLevel := strings.ToLower(strings.TrimSpace(thinkingLevel))
effort := normalizeDevinThinkingLevel(rawLevel)
if effort == "" && budgetTokens > 0 {
effort, _ = thinking.ConvertBudgetToLevel(budgetTokens)
}
lowerBase := strings.ToLower(baseModel)
canonicalBase := strings.ReplaceAll(lowerBase, ".", "-")
// 5. Check special private upstream aliases
if alias, exists := specialDevinAliases[canonicalBase]; exists {
return alias
}
if canonicalBase == "claude-sonnet-4-5" || strings.Contains(canonicalBase, "sonnet-4-5") {
if effort != "" && effort != "none" {
return "MODEL_PRIVATE_3"
}
return "MODEL_PRIVATE_2"
}
if canonicalBase == "gemini-3-flash" {
canonicalBase = "gemini-3-8-flash"
}
// 6. Resolve automatic effort against the catalog before choosing a wire UID.
modelInfo := registry.LookupDevinModel(canonicalBase)
if modelInfo == nil && canonicalBase != lowerBase {
modelInfo = registry.LookupDevinModel(lowerBase)
}
if effort == string(thinking.LevelAuto) {
effort = string(thinking.ClampLevel(thinking.LevelMedium, modelInfo, "devin"))
}
normalizedUnder := strings.ReplaceAll(canonicalBase, "-", "_")
switch normalizedUnder {
case "model_gpt_5_2":
eff := clampEffort(effort, devinGPT52Model, "low")
return "MODEL_GPT_5_2_" + strings.ToUpper(eff)
case "model_google_gemini_3_0_flash":
eff := clampEffort(effort, devinGeminiFlashModel, "high")
return "MODEL_GOOGLE_GEMINI_3_0_FLASH_" + strings.ToUpper(eff)
case "model_claude_4_5_opus":
if effort != "" && effort != "none" {
return "MODEL_CLAUDE_4_5_OPUS_THINKING"
}
return "MODEL_CLAUDE_4_5_OPUS"
}
var allowedLevels []string
if modelInfo != nil && modelInfo.Thinking != nil && len(modelInfo.Thinking.Levels) > 0 {
allowedLevels = modelInfo.Thinking.Levels
}
// 7. Special base models that default to bare name unless specific variant requested
switch canonicalBase {
case "swe-1-7":
if thinking.ClampLevel(thinking.ThinkingLevel(effort), modelInfo, "devin") == thinking.LevelMedium {
return "swe-1-7-medium"
}
return "swe-1-7"
case "swe-1-6":
if effort == "fast" {
return "swe-1-6-fast"
}
return "swe-1-6"
case "glm-5-2":
if effort == "none" {
return "glm-5-2-none"
}
if effort == "max" {
return "glm-5-2-max"
}
return "glm-5-2"
case "glm-5-2-1m":
if effort == "none" {
return "glm-5-2-none-1m"
}
if effort == "max" {
return "glm-5-2-max-1m"
}
return "glm-5-2-1m"
case "claude-opus-4-6", "claude-sonnet-4-6":
if effort != "" && effort != "none" {
return canonicalBase + "-thinking"
}
return canonicalBase
case "claude-opus-4-6-1m":
if effort != "" && effort != "none" {
return "claude-opus-4-6-thinking-1m"
}
return canonicalBase
case "claude-sonnet-4-6-1m":
if effort != "" && effort != "none" {
return "claude-sonnet-4-6-thinking-1m"
}
return canonicalBase
}
// 8. Lightning tier: upstream UIDs keep the effort before the suffix
// (swe-2-high-lightning), unlike every other model which appends effort.
if strings.HasSuffix(canonicalBase, "-lightning") {
stem := strings.TrimSuffix(canonicalBase, "-lightning")
if HasDevinEffortSuffix(stem) {
// Already a full upstream UID such as swe-2-high-lightning
return canonicalBase
}
if len(allowedLevels) == 0 {
return canonicalBase
}
defaultEffort := selectDefaultDevinEffort(stem, allowedLevels)
clamped := clampEffort(effort, modelInfo, defaultEffort)
return stem + "-" + clamped + "-lightning"
}
// 9. If model has no thinking levels defined in catalog, treat as bare model
if len(allowedLevels) == 0 {
return canonicalBase
}
// 10. Fast-mode variants: a strict "<level>-fast" request keeps its level
// clamped to the catalog, never to the plain default.
if base, isFast := strings.CutSuffix(rawLevel, "-fast"); isFast {
if level, ok := thinking.ParseLevelSuffix(strings.TrimSpace(base)); ok {
defaultEffort := selectDefaultDevinEffort(canonicalBase, allowedLevels)
clamped := clampEffort(string(level), modelInfo, defaultEffort)
return canonicalBase + "-" + clamped + "-fast"
}
}
// 11. Model has thinking levels: determine default effort and clamp
defaultEffort := selectDefaultDevinEffort(canonicalBase, allowedLevels)
clamped := clampEffort(effort, modelInfo, defaultEffort)
if effort == "fast" {
return canonicalBase + "-" + clamped + "-fast"
}
return canonicalBase + "-" + clamped
}
func selectDefaultDevinEffort(baseModel string, levels []string) string {
if strings.Contains(baseModel, "swe-2") {
return "high"
}
hasNone := false
hasLow := false
hasMedium := false
hasHigh := false
for _, l := range levels {
switch l {
case "none":
hasNone = true
case "low":
hasLow = true
case "medium":
hasMedium = true
case "high":
hasHigh = true
}
}
// For OpenAI GPT-5.x families in Devin CLI (which support "none" and "low"), default to low
if hasNone && hasLow && strings.HasPrefix(baseModel, "gpt-5") {
return "low"
}
// For models with high thinking (e.g. deepseek, gemini, grok, glm, kimi, nemotron), prefer high
if hasHigh && (strings.Contains(baseModel, "gemini") ||
strings.Contains(baseModel, "grok") ||
strings.Contains(baseModel, "glm") ||
strings.Contains(baseModel, "deepseek") ||
strings.Contains(baseModel, "kimi") ||
strings.Contains(baseModel, "nemotron")) {
return "high"
}
if hasMedium {
return "medium"
}
if hasHigh {
return "high"
}
if hasLow {
return "low"
}
return levels[0]
}
func clampEffort(requested string, modelInfo *registry.ModelInfo, defaultEffort string) string {
if requested == "" {
return defaultEffort
}
clamped := thinking.ClampLevel(thinking.ThinkingLevel(strings.TrimSpace(requested)), modelInfo, "devin")
for _, allowed := range modelInfo.Thinking.Levels {
if strings.EqualFold(string(clamped), strings.TrimSpace(allowed)) {
return allowed
}
}
return defaultEffort
}