| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | |
| 8 | "reasonix/internal/provider/openai" |
| 9 | ) |
| 10 | |
| 11 | var mimoVisionModels = map[string]bool{ |
| 12 | "mimo-v2.6-pro": true, |
| 13 | "mimo-v2.6-flash": true, |
| 14 | "mimo-v2.5": true, |
| 15 | "mimo-v2-omni": true, |
| 16 | } |
| 17 | |
| 18 | // VisionCapability is the model-level image-input fact used by settings and |
| 19 | // turn preparation. Unknown is deliberately distinct from unsupported: an |
| 20 | // unknown model is kept safe by the text-only path without claiming that the |
| 21 | // provider can never accept images. |
| 22 | type VisionCapability string |
| 23 | |
| 24 | const ( |
| 25 | VisionCapabilityUnknown VisionCapability = "unknown" |
| 26 | VisionCapabilitySupported VisionCapability = "supported" |
| 27 | VisionCapabilityUnsupported VisionCapability = "unsupported" |
| 28 | ) |
| 29 | |
| 30 | // InferVisionModels returns model IDs that look like chat models with image-input |
| 31 | // support. It is intentionally conservative and meant for Settings hints; an |
| 32 | // explicit capability declaration remains the runtime source of truth. |
| 33 | func InferVisionModels(models []string) []string { |
| 34 | out := make([]string, 0, len(models)) |
| 35 | seen := map[string]bool{} |
| 36 | for _, model := range models { |
| 37 | model = strings.TrimSpace(model) |
| 38 | if model == "" || seen[model] || !IsLikelyChatModel(model) || !IsLikelyVisionModel(model) { |
| 39 | continue |
| 40 | } |
| 41 | seen[model] = true |
| 42 | out = append(out, model) |
| 43 | } |
| 44 | return out |
| 45 | } |
| 46 | |
| 47 | func IsLikelyVisionModel(model string) bool { |
| 48 | model = strings.TrimSpace(model) |
| 49 | if model == "" { |
| 50 | return false |
| 51 | } |
| 52 | lower := strings.ToLower(model) |
| 53 | if mimoVisionModels[lower] { |
| 54 | return true |
| 55 | } |
| 56 | tokens := strings.FieldsFunc(lower, modelTokenSeparator) |
| 57 | if slices.Contains(tokens, "audio") { |
| 58 | return false |
| 59 | } |
| 60 | if strings.HasPrefix(lower, "gpt-4o") { |
| 61 | return true |
| 62 | } |
| 63 | for _, token := range tokens { |
| 64 | switch token { |
| 65 | case "vl", "vision", "visual", "multimodal", "omni": |
| 66 | return true |
| 67 | } |
| 68 | } |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | func modelTokenSeparator(r rune) bool { |
| 73 | return r == '-' || r == '_' || r == '.' || r == '/' || r == ':' |
| 74 | } |
| 75 | |
| 76 | // CanConfigureVision is retained for the desktop payload contract. Capability |
| 77 | // choices are now derived from model metadata; the wire layer still refuses |
| 78 | // unsupported official DeepSeek Flash/Pro image payloads. |
| 79 | func CanConfigureVision(e *ProviderEntry) bool { |
| 80 | return e != nil |
| 81 | } |
| 82 | |
| 83 | // AllowsImages reports whether image input may be sent to the model itself: |
| 84 | // declared supported, or undeclared with no fallback model to route through. |
| 85 | // Only a declared text-only model is blocked. |
| 86 | func (v VisionCapability) AllowsImages(fallbackConfigured bool) bool { |
| 87 | return v == VisionCapabilitySupported || (v == VisionCapabilityUnknown && !fallbackConfigured) |
| 88 | } |
| 89 | |
| 90 | // EffectiveVision resolves whether the selected model accepts image input. |
| 91 | // Official DeepSeek Settings can mark any enabled model for image input, but |
| 92 | // only the pinned vision SKU actually receives image parts. An unconfigured |
| 93 | // catalog (VisionModels == nil) still enables that SKU so CLI/model-switch |
| 94 | // users keep image input without a Settings round-trip. Explicit provider |
| 95 | // vision still wins for custom gateways; the MiMo endpoint heuristic is |
| 96 | // deliberately limited to known MiMo endpoints so arbitrary OpenAI-compatible |
| 97 | // proxies do not get image payloads unexpectedly. |
| 98 | func EffectiveVision(e *ProviderEntry) bool { |
| 99 | return VisionCapabilityForModel(e) == VisionCapabilitySupported |
| 100 | } |
| 101 | |
| 102 | // VisionCapabilityForModel resolves the selected model's image-input |
| 103 | // capability without requiring a user-maintained provider-level list. Legacy |
| 104 | // Vision/VisionModels values remain valid fallbacks while model overrides take |
| 105 | // precedence after ResolveModel applies them. |
| 106 | func VisionCapabilityForModel(e *ProviderEntry) VisionCapability { |
| 107 | if e == nil { |
| 108 | return VisionCapabilityUnknown |
| 109 | } |
| 110 | if openai.IsDeepSeek(e.BaseURL) { |
| 111 | if officialDeepSeekEffectiveVision(e) { |
| 112 | return VisionCapabilitySupported |
| 113 | } |
| 114 | return VisionCapabilityUnsupported |
| 115 | } |
| 116 | if e.visionOverride != nil { |
| 117 | if *e.visionOverride { |
| 118 | return VisionCapabilitySupported |
| 119 | } |
| 120 | return VisionCapabilityUnsupported |
| 121 | } |
| 122 | if e.Vision { |
| 123 | return VisionCapabilitySupported |
| 124 | } |
| 125 | if e.HasVisionModel(e.Model) { |
| 126 | return VisionCapabilitySupported |
| 127 | } |
| 128 | if isOfficialMimoVisionEntry(e) { |
| 129 | return VisionCapabilitySupported |
| 130 | } |
| 131 | if e.VisionModels != nil { |
| 132 | return VisionCapabilityUnsupported |
| 133 | } |
| 134 | return VisionCapabilityUnknown |
| 135 | } |
| 136 | |
| 137 | // ExplicitModelVision reports whether the selected model has an explicit, |
| 138 | // positive image capability declaration that the endpoint is allowed to use. |
| 139 | // Keep this query separate from EffectiveVision so callers can distinguish a |
| 140 | // model-scoped capability from provider-wide or endpoint-inferred support. |
| 141 | func ExplicitModelVision(e *ProviderEntry) bool { |
| 142 | if e == nil { |
| 143 | return false |
| 144 | } |
| 145 | if openai.IsDeepSeek(e.BaseURL) { |
| 146 | return officialDeepSeekEffectiveVision(e) |
| 147 | } |
| 148 | enabled, explicit := explicitModelVision(e) |
| 149 | return explicit && enabled |
| 150 | } |
| 151 | |
| 152 | func officialDeepSeekEffectiveVision(e *ProviderEntry) bool { |
| 153 | if e == nil || openai.IsOfficialDeepSeekTextModel(e.Model) { |
| 154 | return false |
| 155 | } |
| 156 | if enabled, explicit := explicitModelVision(e); explicit { |
| 157 | return enabled |
| 158 | } |
| 159 | if !openai.IsOfficialDeepSeekImageModel(e.Model) { |
| 160 | return false |
| 161 | } |
| 162 | if e.Vision { |
| 163 | return true |
| 164 | } |
| 165 | // An explicitly emptied list means the user turned image input off for the |
| 166 | // provider. A list curated before the V4.1 SKUs still omits them, so a |
| 167 | // non-empty list must not veto a model the vendor reports as capable. |
| 168 | if e.VisionModels != nil && len(e.VisionModels) == 0 { |
| 169 | return false |
| 170 | } |
| 171 | return true |
| 172 | } |
| 173 | |
| 174 | func explicitModelVision(e *ProviderEntry) (enabled, explicit bool) { |
| 175 | if e == nil { |
| 176 | return false, false |
| 177 | } |
| 178 | if e.visionOverride != nil { |
| 179 | return *e.visionOverride, true |
| 180 | } |
| 181 | if e.HasVisionModel(e.Model) { |
| 182 | return true, true |
| 183 | } |
| 184 | return false, false |
| 185 | } |
| 186 | |
| 187 | func (e *ProviderEntry) HasVisionModel(model string) bool { |
| 188 | model = strings.TrimSpace(model) |
| 189 | if model == "" { |
| 190 | return false |
| 191 | } |
| 192 | for _, candidate := range e.VisionModels { |
| 193 | if strings.TrimSpace(candidate) == model { |
| 194 | return true |
| 195 | } |
| 196 | } |
| 197 | return false |
| 198 | } |
| 199 | |
| 200 | func isOfficialMimoVisionEntry(e *ProviderEntry) bool { |
| 201 | if !isOpenAIProviderKind(e) || !mimoVisionModels[strings.ToLower(strings.TrimSpace(e.Model))] { |
| 202 | return false |
| 203 | } |
| 204 | switch officialMimoHost(e.BaseURL) { |
| 205 | case "api.xiaomimimo.com", "token-plan-cn.xiaomimimo.com", "token-plan-sgp.xiaomimimo.com", "token-plan-ams.xiaomimimo.com": |
| 206 | return true |
| 207 | default: |
| 208 | return false |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func officialMimoHost(baseURL string) string { |
| 213 | u, err := url.Parse(strings.TrimSpace(baseURL)) |
| 214 | if err != nil { |
| 215 | return "" |
| 216 | } |
| 217 | return strings.ToLower(u.Hostname()) |
| 218 | } |
| 219 |