diff --git a/app/resources/atc/models.json b/app/resources/atc/models.json new file mode 100644 index 00000000..9d6fe81c --- /dev/null +++ b/app/resources/atc/models.json @@ -0,0 +1,190 @@ +[ + { "match": { "name": "(?:.*/)?glm[^/]*" }, + "metadata": { "contextWindow": 1000000, "efforts": ["low", "high", "max"] } }, + { "match": { "name": "(?:.*/)?glm-5(?:-[0-9]{6,8})?" }, + "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "GLM-5\\.2.*" }, + "metadata": { "contextWindow": 1000000, "efforts": ["high", "max"], "thinking": true } }, + { "match": { "name": "GLM-5-Turbo.*" }, "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-5\\.3.*" }, "metadata": { "contextWindow": 1000000, "efforts": ["low", "high", "max"] } }, + { "match": { "name": "glm-5\\.1.*" }, "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-5\\.1-highspeed.*" }, + "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-5v-turbo.*" }, "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.7.*" }, "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.7-flash.*" }, + "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.7-flashx.*" }, + "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.6.*" }, "metadata": { "contextWindow": 200000, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.5.*" }, "metadata": { "contextWindow": 131072, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.5-air.*" }, + "metadata": { "contextWindow": 131072, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.6v.*" }, "metadata": { "contextWindow": 131072 } }, + { "match": { "name": "glm-4\\.6v-flash.*" }, "metadata": { "contextWindow": 131072 } }, + { "match": { "name": "glm-4\\.6v-flashx.*" }, "metadata": { "contextWindow": 131072 } }, + { "match": { "name": "glm-4\\.1v-thinking-flash.*" }, + "metadata": { "contextWindow": 65536, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4\\.1v-thinking-flashx.*" }, "metadata": { "contextWindow": 65536 } }, + { "match": { "name": "glm-4-flashx-250414.*" }, + "metadata": { "contextWindow": 131072, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4-flash-250414.*" }, + "metadata": { "contextWindow": 131072, "efforts": [], "thinking": true } }, + { "match": { "name": "glm-4v-flash.*" }, "metadata": { "contextWindow": 16384, "efforts": [], "thinking": true } }, + + { "match": { "name": "codegeex-4.*" }, "metadata": { "contextWindow": 131072 } }, + + { "match": { "name": "charglm-4.*" }, "metadata": { "contextWindow": 8192 } }, + + { "match": { "name": "emohaa.*" }, "metadata": { "contextWindow": 8192 } }, + + { "match": { "name": "gpt-5\\.6.*" }, + "metadata": { "contextWindow": 1050000, "efforts": ["none", "low", "medium", "high", "xhigh", "max"] } }, + { "match": { "name": "gpt-5\\.3-codex.*" }, + "metadata": { "contextWindow": 400000, "efforts": ["low", "medium", "high", "xhigh"] } }, + { "match": { "name": "gpt-6-astra.*" }, + "metadata": { "contextWindow": 1050000, "efforts": ["low", "medium", "high", "xhigh", "max"] } }, + { "match": { "name": "gpt-5\\.4.*" }, + "metadata": { "contextWindow": 1050000, "efforts": ["none", "low", "medium", "high", "xhigh"] } }, + { "match": { "name": "gpt-5\\.4-pro.*" }, + "metadata": { "contextWindow": 1050000, "efforts": ["medium", "high", "xhigh"] } }, + { "match": { "name": "gpt-5\\.4-mini.*" }, + "metadata": { "contextWindow": 400000, "efforts": ["none", "low", "medium", "high", "xhigh"] } }, + { "match": { "name": "gpt-5\\.4-nano.*" }, + "metadata": { "contextWindow": 400000, "efforts": ["none", "low", "medium", "high", "xhigh"] } }, + + { "match": { "name": "claude-(?:fable|opus|sonnet)-5.*" }, + "metadata": { "efforts": ["low", "medium", "high", "xhigh", "max"] } }, + { "match": { "name": "claude-haiku-4[.-]5.*" }, "metadata": { "contextWindow": 200000 } }, + { "match": { "name": "claude-(?:fable|mythos)-5[.-]1.*" }, + "metadata": { "efforts": ["low", "medium", "high", "xhigh", "max"] } }, + + { "match": { "name": "grok-4\\.6.*" }, + "metadata": { "contextWindow": 500000, "efforts": ["low", "medium", "high", "xhigh"] } }, + { "match": { "name": "grok-build-0\\.1.*" }, "metadata": { "contextWindow": 256000 } }, + + { "match": { "name": "kimi-k3.*" }, "metadata": { "contextWindow": 1048576, "efforts": ["low", "high", "max"] } }, + + { "match": { "name": "k3.*" }, "metadata": { "contextWindow": 1048576, "efforts": ["low", "high", "max"] } }, + { "match": { "name": "k3-256k.*" }, "metadata": { "contextWindow": 262144, "efforts": ["low", "high", "max"] } }, + + { "match": { "name": "kimi-k2\\.7-code.*" }, + "metadata": { "contextWindow": 262144, "efforts": [], "thinking": true } }, + { "match": { "name": "kimi-k2\\.6.*" }, "metadata": { "contextWindow": 262144, "efforts": [], "thinking": true } }, + { "match": { "name": "kimi-k2\\.5.*" }, "metadata": { "contextWindow": 262144, "efforts": [], "thinking": true } }, + + { "match": { "name": "moonshot-v1-8k.*" }, "metadata": { "contextWindow": 8192 } }, + { "match": { "name": "moonshot-v1-32k.*" }, "metadata": { "contextWindow": 32768 } }, + { "match": { "name": "moonshot-v1-128k.*" }, "metadata": { "contextWindow": 131072 } }, + { "match": { "name": "moonshot-v1-8k-vision-preview.*" }, "metadata": { "contextWindow": 8192 } }, + { "match": { "name": "moonshot-v1-32k-vision-preview.*" }, "metadata": { "contextWindow": 32768 } }, + { "match": { "name": "moonshot-v1-128k-vision-preview.*" }, "metadata": { "contextWindow": 131072 } }, + + { "match": { "name": "MiniMax-M2.*" }, "metadata": { "contextWindow": 204800 } }, + { "match": { "name": "MiniMax-M2\\.7.*" }, "metadata": { "contextWindow": 204800 } }, + { "match": { "name": "MiniMax-M2\\.7-highspeed.*" }, "metadata": { "contextWindow": 204800 } }, + { "match": { "name": "MiniMax-M2\\.5.*" }, "metadata": { "contextWindow": 204800 } }, + { "match": { "name": "MiniMax-M2\\.5-highspeed.*" }, "metadata": { "contextWindow": 204800 } }, + { "match": { "name": "MiniMax-M2\\.1.*" }, "metadata": { "contextWindow": 204800 } }, + { "match": { "name": "MiniMax-M2\\.1-highspeed.*" }, "metadata": { "contextWindow": 204800 } }, + + { "match": { "name": "deepseek-v4-flash.*" }, "metadata": { "efforts": ["low", "high", "max"], "thinking": true } }, + { "match": { "name": "deepseek-v4-pro.*" }, "metadata": { "efforts": ["low", "high", "max"], "thinking": true } }, + { "match": { "name": "deepseek-flash.*" }, "metadata": { "efforts": ["low", "high", "max"], "thinking": true } }, + { "match": { "name": "deepseek-v4[.-]1-flash.*" }, + "metadata": { "efforts": ["low", "high", "max"], "thinking": true } }, + + { "match": { "name": "qwen3\\.8-(?:max|flash).*" }, "metadata": { "efforts": ["low", "medium", "xhigh"] } }, + { "match": { "name": "qwen3\\.5-plus.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3\\.5-flash.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3-max.*" }, "metadata": { "contextWindow": 262144 } }, + { "match": { "name": "qwen-plus.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen-flash.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3-vl-plus.*" }, + "metadata": { "contextWindow": 262144, "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3\\.7-max.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3\\.7-plus.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3\\.7-flash.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3\\.6-plus.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3\\.6-flash.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "qwen3\\.8-omni-flash.*" }, + "metadata": { "contextWindow": 65536, "efforts": ["none", "minimal", "low", "medium", "high", "xhigh", "max"] } }, + + { "match": { "name": "mimo-v2\\.6-(?:pro|flash).*" }, + "metadata": { "contextWindow": 1000000, "efforts": [], "thinking": true } }, + { "match": { "name": "mimo-v2\\.5.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "mimo-v2\\.5-pro.*" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "mimo-v2-omni.*" }, "metadata": { "contextWindow": 262144 } }, + { "match": { "name": "mimo-v2-flash.*" }, + "metadata": { "contextWindow": 262144, "efforts": [], "thinking": true } }, + + { "match": { "name": "(?:ox-alpha|glm-x-preview-f|x-preview-f-free).*" }, + "metadata": { "efforts": ["low", "high", "max"] } }, + + { "match": { "name": "glm-5\\.3" }, "metadata": { "thinking": true } }, + + { "match": { "name": "deepseek-v4\\.1-flash" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "deepseek-v4-pro" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "deepseek-v4-flash" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "deepseek-v4-flash-vision-exp" }, "metadata": { "contextWindow": 1000000 } }, + + { "match": { "name": "mimo-v2\\.5" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "mimo-v2\\.5-pro" }, "metadata": { "contextWindow": 1048576 } }, + + { "match": { "name": "hy4-preview" }, "metadata": { "contextWindow": 1024000, "efforts": ["none", "high"] } }, + { "match": { "name": "hy3" }, "metadata": { "contextWindow": 256000, "efforts": ["none", "low", "high"] } }, + + { "match": { "name": "minimax-m3" }, "metadata": { "contextWindow": 1000000, "efforts": [], "thinking": true } }, + { "match": { "name": "minimax-m2\\.7" }, "metadata": { "efforts": [], "thinking": true } }, + { "match": { "name": "minimax-m2\\.5" }, "metadata": { "efforts": [], "thinking": true } }, + + { "match": { "name": "qwen3\\.8-max" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "qwen3\\.8-flash" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "qwen3\\.7-max" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "qwen3\\.7-plus" }, "metadata": { "contextWindow": 1000000 } }, + { "match": { "name": "qwen3\\.6-plus" }, "metadata": { "contextWindow": 1000000 } }, + + { "match": { "name": "deepseek-v4[.-]1-flash(?:[.\\-:/\\[].*)?" }, "metadata": { "contextWindow": 1048576 } }, + + { "match": { "name": "gpt-6\\.1-sol" }, + "metadata": { "contextWindow": 1050000, "efforts": ["low", "medium", "high", "xhigh", "max"] } }, + { "match": { "name": "gpt-6\\.1-terra" }, + "metadata": { "contextWindow": 1050000, "efforts": ["none", "low", "medium", "high", "xhigh", "max"] } }, + { "match": { "name": "gpt-6-luna" }, + "metadata": { "contextWindow": 1050000, "efforts": ["none", "low", "medium", "high", "xhigh", "max"] } }, + + { "match": { "name": "claude-haiku-5-5" }, "metadata": { "efforts": ["low", "medium", "high", "xhigh", "max"] } }, + + { "match": { "name": "grok-4\\.7" }, + "metadata": { "contextWindow": 500000, "efforts": ["low", "medium", "high", "xhigh"] } }, + + { "match": { "name": "claude-haiku-5\\.5" }, "metadata": { "efforts": ["low", "medium", "high", "xhigh", "max"] } }, + + { "match": { "name": "doubao-seed-evolving" }, + "metadata": { "contextWindow": 1024000, "efforts": ["none", "low", "medium", "high"] } }, + { "match": { "name": "doubao-seed-2-1-pro-260915" }, + "metadata": { "contextWindow": 1024000, "efforts": ["none", "low", "medium", "high"] } }, + { "match": { "name": "doubao-seed-2-1-lite-260915" }, + "metadata": { "contextWindow": 1024000, "efforts": ["none", "low", "medium", "high"] } }, + { "match": { "name": "doubao-seed-2-1-turbo-260628" }, + "metadata": { "contextWindow": 256000, "efforts": ["none", "low", "medium", "high"] } }, + { "match": { "name": "doubao-seed-2\\.1-pro" }, + "metadata": { "contextWindow": 1024000, "efforts": ["none", "low", "medium", "high"] } }, + { "match": { "name": "doubao-seed-2\\.1-lite" }, + "metadata": { "contextWindow": 1024000, "efforts": ["none", "low", "medium", "high"] } }, + + { "match": { "name": "step-5-preview" }, + "metadata": { "contextWindow": 1000000, "efforts": ["low", "medium", "high"] } }, + { "match": { "name": "step-3\\.7-flash" }, + "metadata": { "contextWindow": 262144, "efforts": ["low", "medium", "high"] } }, + + { "match": { "name": "kimi-for-coding" }, + "metadata": { "contextWindow": 1048576, "efforts": ["low", "high", "max"] } }, + { "match": { "name": "kimi-for-coding-highspeed" }, + "metadata": { "contextWindow": 262144, "efforts": [], "thinking": true } }, + + { "match": { "name": "MiniMax-M3\\.1-Flash-Preview" }, + "metadata": { "contextWindow": 1000000, "efforts": ["low", "medium", "high", "xhigh", "max"] } }, + { "match": { "name": "MiniMax-M3\\.1-Flash" }, + "metadata": { "contextWindow": 1000000, "efforts": ["low", "medium", "high", "xhigh", "max"] } } +] diff --git a/app/src/atc/config/Layers.scala b/app/src/atc/config/Layers.scala index a7e8699a..ba512a51 100644 --- a/app/src/atc/config/Layers.scala +++ b/app/src/atc/config/Layers.scala @@ -182,12 +182,16 @@ final case class Configuration( def hiddenEnvironmentVariables: Set[String] = keyVariables ++ keys.names ++ KeyBindings.KnownSecretVariables /** Every configured model, resolved, with its provider's key. */ - def catalog: ModelCatalog = ModelCatalog.from(settings, keys) + def catalog: ModelCatalog = ModelCatalog.from(settings, keys, presets = presets) /** As [[catalog]], with the providers that configure no models listed by `discover` * and their lists kept in `store` between sessions. */ def catalog(discover: ModelSpec => List[ModelSpec], store: ModelListStore): ModelCatalog = - ModelCatalog.from(settings, keys, Some(discover), Some(store)) + ModelCatalog.from(settings, keys, Some(discover), Some(store), presets) + + /** The preset model catalog: the one shipped with the release, with the + * user's own `~/.atc/models.json` layered over it. */ + private def presets: ModelPresets = ModelPresets.load(Config.globalDir) /** Configured child-process environment override pre-approvals from every active layer. */ def environmentRules: List[atc.perms.EnvironmentRule] = layers.flatMap: layer => diff --git a/app/src/atc/config/ModelCatalog.scala b/app/src/atc/config/ModelCatalog.scala index 8eb9afc1..a269e06c 100644 --- a/app/src/atc/config/ModelCatalog.scala +++ b/app/src/atc/config/ModelCatalog.scala @@ -74,6 +74,12 @@ final class ModelCatalog( /** Every configured provider's name, turned off or not: [[find]] reads a reference that * starts with one and `/` as naming that provider's model. */ providerNames: Set[String] = Set.empty, + /** Metadata for models whose provider reports none, filled in as a model + * leaves this catalog. Presets are applied here, on the way out, and never + * before `store` keeps a provider's list: a stored list holds only what the + * provider itself said, so a corrected preset takes effect at once instead + * of staying frozen in the cache of an earlier session. */ + presets: ModelPresets = ModelPresets.none, ): private def lower(s: String) = s.toLowerCase(Locale.ROOT) @@ -137,7 +143,9 @@ final class ModelCatalog( available /** Every model known without waiting: the configured ones, then each provider's newest list. */ - private def available: List[ModelSpec] = configured ++ discoverable.flatMap(known(_).getOrElse(Nil)) + private def available: List[ModelSpec] = + val all = configured ++ discoverable.flatMap(known(_).getOrElse(Nil)) + all.map(presets.apply) /** The shortest name that identifies `m` on its own. */ def label(m: ModelSpec): String = @@ -150,7 +158,9 @@ final class ModelCatalog( * naming the configured models when it matches none, or both candidates when a name is * ambiguous. A reference that starts with a provider's name and `/` names a model of that * provider only, never a listed id of another one. */ - def find(reference: String): ModelSpec = + def find(reference: String): ModelSpec = presets(found(reference)) + + private def found(reference: String): ModelSpec = val trimmed = reference.trim val wanted = lower(trimmed) if wanted.isEmpty then throw IllegalArgumentException("No model given") @@ -226,6 +236,7 @@ object ModelCatalog: keys: KeyBindings = KeyBindings.empty, discover: Option[ModelSpec => List[ModelSpec]] = None, store: Option[ModelListStore] = None, + presets: ModelPresets = ModelPresets.builtin, ): ModelCatalog = val defaults = ModelConfig(webSearch = config.webSearch) val providers = config.providers.toList.sortBy(_._1).filter(_._2.enabled).map: (name, p) => @@ -245,7 +256,14 @@ object ModelCatalog: if p.models.isEmpty && !ProviderConfig.canonicalApi(endpoint.api).contains("echo") && (endpoint.apiKey.isDefined || (p.key.isEmpty && p.keyEnv.isEmpty)) => endpoint } - ModelCatalog(configured, discoverable, discover, store, providerNames = config.providers.keySet) + ModelCatalog( + configured, + discoverable, + discover, + store, + providerNames = config.providers.keySet, + presets = presets, + ) /** The provider `name` as an endpoint whose models can be listed, whatever models it * configures and whether it is on; `None` when its key is not bound. */ diff --git a/app/src/atc/config/ModelPresets.scala b/app/src/atc/config/ModelPresets.scala new file mode 100644 index 00000000..69890c65 --- /dev/null +++ b/app/src/atc/config/ModelPresets.scala @@ -0,0 +1,246 @@ +package atc.config + +import atc.{Debug, Resources} + +import upickle.default.* + +import java.nio.file.{Files, Path} +import java.util.Locale +import java.util.regex.Pattern +import scala.util.control.NonFatal + +/** What a catalog entry applies to. + * + * `name` is a regular expression matched against the provider's model id: the + * whole id, ignoring case. An id a provider lists under a vendor prefix + * (OpenRouter's `z-ai/glm-5.4`) is also tried with that prefix removed, so + * `glm-5\.4` covers both spellings and no entry needs to spell the prefix out. + * + * `provider` and `api` narrow an entry to one provider of the user's config or + * to one wire protocol. Both are exact names, matched ignoring case, and both + * default to every provider and every protocol; `"*"` says that explicitly. + * Protocol names go through [[ProviderConfig.canonicalApi]], so `claude` and + * `anthropic` are one scope. The catalog shipped with the release uses neither: + * its entries name a model and nothing else, so the same description applies + * wherever that model is served. The scopes are there for a user's own + * `models.json`, where one provider is often exactly what needs correcting. */ +final case class PresetMatch( + name: String, + provider: Option[String] = None, + api: Option[String] = None, +) derives ReadWriter: + /** Whether this scope is one provider rather than all of them. */ + private[config] def scopesProvider: Boolean = PresetMatch.narrow(provider) + /** Whether this scope is one protocol rather than all of them. */ + private[config] def scopesApi: Boolean = PresetMatch.narrow(api) + +private[config] object PresetMatch: + def narrow(value: Option[String]): Boolean = value.exists(v => v.trim.nonEmpty && v.trim != "*") + + /** Whether `value` names `actual`, with an absent or `"*"` scope naming everything. */ + def names(value: Option[String], actual: String): Boolean = + !narrow(value) || value.exists(_.trim.equalsIgnoreCase(actual.trim)) + +/** One entry of the preset model catalog: the models it describes and the + * metadata to fill in for them. + * + * `metadata` is a [[ModelConfig]], but only the fields that are facts about a + * model ([[ModelConfig.contextWindow]], [[ModelConfig.displayName]]) or + * constraints on what the user may choose ([[ModelConfig.efforts]], + * [[ModelConfig.thinking]]) are ever applied. A catalog never sets a choice + * the user owns, such as `reasoning` or `webSearch`: see + * [[ModelPresets.Settable]]. + * + * `only` drops every lower-priority entry that also matches the model: the way + * out when a shipped entry is wrong for one model and a user's own catalog has + * to replace it rather than refine it. */ +final case class PresetEntry( + @upickle.implicits.key("match") when: PresetMatch, + metadata: ModelConfig, + only: Boolean = false, +) derives ReadWriter + +/** The preset model catalog: metadata for models whose provider reports none. + * + * A provider's own model list is the better source and always wins; these + * presets only fill in what it leaves out, and what the user configures wins + * over both. Within the catalog the entries that match a model are merged + * field by field, the more specific one winning each field: one entry can give + * a model's effort levels for every provider while another corrects its + * context window on one of them. + * + * Specificity is computed, never the order entries are written in: an entry + * scoped to a provider beats one scoped to a protocol, which beats an unscoped + * one, and between two entries of the same scope the one whose `name` spells + * out more of the id wins. A user's own catalog outranks the shipped one + * whatever either one's specificity, so that an unscoped correction still + * takes effect over a shipped entry written for one provider. */ +final class ModelPresets private[config] (private val presets: List[ModelPresets.Preset]): + + /** `spec` with every missing field the catalog can supply filled in. */ + def apply(spec: ModelSpec): ModelSpec = + matching(spec) match + case Nil => spec + case found => spec.copy(settings = ModelPresets.fill(spec.settings, merged(found))) + + private def matching(spec: ModelSpec): List[ModelPresets.Preset] = + presets.filter(_.matches(spec.provider, spec.api, spec.modelId)).sortBy(_.rank) + + /** The matching entries folded into one metadata record, the most specific + * winning each field, and everything below an `only` entry dropped. */ + private def merged(found: List[ModelPresets.Preset]): ModelConfig = + val applicable = found.lastIndexWhere(_.entry.only) match + case -1 => found + case cut => found.drop(cut) + applicable.foldLeft(ModelConfig())((under, preset) => ModelPresets.over(under, preset.entry.metadata)) + + private[config] def size: Int = presets.size + +object ModelPresets: + /** Where the catalog shipped with the release lives on the classpath. */ + private val Resource = "/atc/models.json" + + /** A user's own catalog, read from `~/.atc/models.json`. */ + val FileName = "models.json" + + /** No presets: every model keeps exactly what its provider and the user's config say. */ + val none: ModelPresets = ModelPresets(Nil) + + /** The fields a catalog entry may fill in: facts about a model and the + * choices it allows, never a choice the user owns. `maxTokens` is not among + * them on purpose — an output limit would be sent with every request and + * reserved from the context window, which is a change in behavior rather + * than a description of the model. */ + val Settable: List[String] = List("contextWindow", "efforts", "thinking", "displayName") + + /** One entry of a catalog, with its `name` compiled and its source ranked + * (`0` shipped with the release, `1` the user's own). */ + private[config] final case class Preset(entry: PresetEntry, source: Int, pattern: Pattern): + /** Ascending priority: the last of a sorted run wins each field it sets. A + * provider names one entry of the user's own config and so outranks a + * protocol, which names every provider that speaks it. */ + def rank: (Int, Int, Int, Int) = + ( + source, + if entry.when.scopesProvider then 1 else 0, + if entry.when.scopesApi then 1 else 0, + literals, + ) + + /** How much of a model id the pattern spells out literally, as the count of + * its plainly literal characters: `glm-4\.7-flash` outranks `glm-4\.7`. */ + private def literals: Int = + entry.when.name.count(c => c.isLetterOrDigit || c == '-' || c == '_') + + def matches(provider: String, api: String, modelId: String): Boolean = + PresetMatch.names(entry.when.provider, provider) && namesApi(api) && matchesId(modelId) + + private def namesApi(api: String): Boolean = + if !entry.when.scopesApi then true + else + def canonical(name: String) = + ProviderConfig.canonicalApi(name).getOrElse(name.trim.toLowerCase(Locale.ROOT)) + entry.when.api.exists(wanted => canonical(wanted) == canonical(api)) + + /** The whole id, or the id without a vendor prefix such as OpenRouter's `z-ai/`. */ + private def matchesId(modelId: String): Boolean = + val id = modelId.trim + pattern.matcher(id).matches() || { + val slash = id.lastIndexOf('/') + slash >= 0 && pattern.matcher(id.substring(slash + 1)).matches() + } + + /** `base` with the fields it leaves unset taken from `preset`. Only + * [[Settable]] fields are ever taken, so a catalog cannot change behavior + * the user did not ask for even if an entry sets something else. */ + private[config] def fill(base: ModelConfig, preset: ModelConfig): ModelConfig = + base.copy( + contextWindow = base.contextWindow.orElse(preset.contextWindow), + efforts = base.efforts.orElse(preset.efforts), + thinking = base.thinking.orElse(preset.thinking), + displayName = base.displayName.orElse(preset.displayName), + ) + + /** `under` with every [[Settable]] field `over` sets replaced by it. */ + private[config] def over(under: ModelConfig, overriding: ModelConfig): ModelConfig = + under.copy( + contextWindow = overriding.contextWindow.orElse(under.contextWindow), + efforts = overriding.efforts.orElse(under.efforts), + thinking = overriding.thinking.orElse(under.thinking), + displayName = overriding.displayName.orElse(under.displayName), + ) + + /** The fields of `metadata` a catalog must not set, by name: reported so that + * a mistake in a hand-written catalog is visible instead of silently + * ignored by [[fill]]. `enabled` is left out because its default cannot be + * told from an explicit `true`. */ + private[config] def unsettable(metadata: ModelConfig): List[String] = + List( + "name" -> metadata.name.isDefined, + "webSearch" -> metadata.webSearch.isDefined, + "reasoning" -> metadata.reasoning.isDefined, + "reasoningSummary" -> metadata.reasoningSummary.isDefined, + "maxTokens" -> metadata.maxTokens.isDefined, + "temperature" -> metadata.temperature.isDefined, + "webSearchVersion" -> metadata.webSearchVersion.isDefined, + ).collect { case (field, true) => field } + + /** Whether `entry` fills nothing in: every [[Settable]] field unset. A + * hand-written entry like this is a mistake — most often a misspelled field + * name, which the JSON reader ignores without complaint. */ + private[config] def fillsNothing(entry: PresetEntry): Boolean = + fill(ModelConfig(), entry.metadata) == ModelConfig() + + /** The entries of one catalog document, with unusable ones dropped: an entry + * whose `name` is not a valid regular expression, and one that fills nothing + * in. A broken catalog never fails a session; it is reported and skipped. */ + private[config] def compile(entries: List[PresetEntry], source: Int, origin: String): List[Preset] = + entries.flatMap: entry => + val unsettable = ModelPresets.unsettable(entry.metadata) + if unsettable.nonEmpty then + Debug.log(s"$origin: ${entry.when.name} sets ${unsettable.mkString(", ")}, which a catalog cannot set") + if fillsNothing(entry) then + Debug.log(s"$origin: ${entry.when.name} fills nothing in; check its field names") + None + else + try Some(Preset(entry, source, Pattern.compile(entry.when.name, Pattern.CASE_INSENSITIVE))) + catch + case NonFatal(e) => + Debug.log(s"$origin: '${entry.when.name}' is not a valid pattern (${Debug.message(e)})") + None + + private def parse(text: String, source: Int, origin: String): List[Preset] = + try compile(read[List[PresetEntry]](text), source, origin) + catch + case NonFatal(e) => + Debug.log(s"could not read $origin: ${Debug.describe(e)}") + Nil + + /** The catalog shipped with the release; empty when the build did not bundle it. */ + private[config] lazy val shipped: List[Preset] = + Resources.text(Resource).fold(Nil)(parse(_, 0, s"the preset model catalog ($Resource)")) + + /** The shipped catalog, with a user's own `models.json` in `atcDir` layered + * over it. A user entry wins over every shipped entry, however specific. */ + def load(atcDir: Path): ModelPresets = + val file = atcDir.resolve(FileName).nn + val own = + if !Files.isRegularFile(file) then Nil + else + try parse(Files.readString(file).nn, 1, file.toString) + catch + case NonFatal(e) => + Debug.log(s"could not read $file: ${Debug.describe(e)}") + Nil + ModelPresets(shipped ++ own) + + /** The shipped catalog alone. */ + def builtin: ModelPresets = ModelPresets(shipped) + + /** A catalog of `entries`, for tests and for callers that supply their own. */ + def of(entries: List[PresetEntry]): ModelPresets = + ModelPresets(compile(entries, 0, "a supplied catalog")) + + /** A shipped catalog with a user's own entries over it, as [[load]] builds it. */ + private[config] def layered(shipped: List[PresetEntry], own: List[PresetEntry]): ModelPresets = + ModelPresets(compile(shipped, 0, "a supplied catalog") ++ compile(own, 1, "a supplied user catalog")) diff --git a/app/src/atc/ide/IdeServer.scala b/app/src/atc/ide/IdeServer.scala index afe4aa19..e702db82 100644 --- a/app/src/atc/ide/IdeServer.scala +++ b/app/src/atc/ide/IdeServer.scala @@ -20,6 +20,7 @@ import atc.config.{ ModelCatalog, ModelConfig, ModelListStore, + ModelPresets, ModelSpec, ObjectText, Origin, @@ -743,7 +744,9 @@ private[atc] final class IdeServer( val fetched = ChatModel.listModels(spec) if existingOpt.isDefined && allowStoredKeys then lists.foreach(_.save(spec, fetched)) else if fetched.nonEmpty then probedModels.put(providerName, spec -> fetched) - fetched + // What the stored list and the probe keep is what the provider itself said; the + // preset catalog only describes the choices shown for it. + fetched.map(ModelPresets.load(globalDir).apply) val choices = ProviderEdits.choices(effectiveProvider, listed) val usedForProvider = inUseSpecs.filter(_.provider == providerName) diff --git a/app/test/src/atc/config/ModelPresetsSuite.scala b/app/test/src/atc/config/ModelPresetsSuite.scala new file mode 100644 index 00000000..2d40ad66 --- /dev/null +++ b/app/test/src/atc/config/ModelPresetsSuite.scala @@ -0,0 +1,321 @@ +package atc.config + +import atc.TestEnv + +import java.nio.file.{Files, Path} + +/** The preset model catalog: how an entry matches a model, how matching entries + * merge, where a preset ranks against what a provider reports and what the + * user configures, and that the catalog shipped with the release is sound. */ +class ModelPresetsSuite extends munit.FunSuite: + + private def spec( + provider: String = "zai", + api: String = "anthropic", + modelId: String = "glm-5.4", + settings: ModelConfig = ModelConfig(), + ): ModelSpec = + ModelSpec(provider, modelId, api, modelId, baseUrl = None, apiKey = None, settings = settings) + + private def entry( + name: String, + provider: Option[String] = None, + api: Option[String] = None, + only: Boolean = false, + )(metadata: ModelConfig): PresetEntry = + PresetEntry(PresetMatch(name, provider, api), metadata, only) + + private def window(n: Int): Option[Tokens] = Some(Tokens(n)) + + // ---- matching ---- + + test("an entry matches the whole model id, ignoring case"): + val presets = ModelPresets.of(List(entry("glm-5\\.4")(ModelConfig(efforts = Some(List("low")))))) + assertEquals(presets(spec(modelId = "glm-5.4")).settings.efforts, Some(List("low"))) + assertEquals(presets(spec(modelId = "GLM-5.4")).settings.efforts, Some(List("low"))) + // Anchored: a longer id is a different model, not a match. + assertEquals(presets(spec(modelId = "glm-5.4-mini")).settings.efforts, None) + + test("an id listed under a vendor prefix matches without the entry spelling it out"): + val presets = ModelPresets.of(List(entry("glm-5\\.4")(ModelConfig(efforts = Some(List("max")))))) + val listed = spec(provider = "openrouter", api = "openai", modelId = "z-ai/glm-5.4") + assertEquals(presets(listed).settings.efforts, Some(List("max"))) + + test("provider and api scopes narrow an entry, and `*` does not"): + val scoped = ModelPresets.of( + List( + entry("glm-5\\.4", provider = Some("zai"))(ModelConfig(displayName = Some("on zai"))), + entry("glm-5\\.4", api = Some("openai"))(ModelConfig(reasoningSummary = None, maxTokens = None)), + ) + ) + assertEquals(scoped(spec(provider = "zai")).settings.displayName, Some("on zai")) + assertEquals(scoped(spec(provider = "openrouter")).settings.displayName, None) + val starred = ModelPresets.of( + List(entry("glm-5\\.4", provider = Some("*"))(ModelConfig(displayName = Some("anywhere")))) + ) + assertEquals(starred(spec(provider = "openrouter")).settings.displayName, Some("anywhere")) + + test("an api scope accepts every spelling of its protocol"): + val presets = ModelPresets.of( + List(entry("glm-5\\.4", api = Some("claude"))(ModelConfig(efforts = Some(List("high"))))) + ) + assertEquals(presets(spec(api = "anthropic")).settings.efforts, Some(List("high"))) + assertEquals(presets(spec(api = "openai")).settings.efforts, None) + + // ---- merging ---- + + test("matching entries merge field by field, the more specific winning each field"): + val presets = ModelPresets.of( + List( + entry("glm-5\\.4")(ModelConfig(efforts = Some(List("low", "high", "max")), contextWindow = window(1000000))), + entry("glm-5\\.4", provider = Some("openrouter"))(ModelConfig(contextWindow = window(128000))), + ) + ) + val onZai = presets(spec(provider = "zai")).settings + assertEquals(onZai.efforts, Some(List("low", "high", "max"))) + assertEquals(onZai.contextWindow.map(_.toInt), Some(1000000)) + // The narrow entry corrects only the window; the effort levels still come from the broad one. + val onRouter = presets(spec(provider = "openrouter")).settings + assertEquals(onRouter.efforts, Some(List("low", "high", "max"))) + assertEquals(onRouter.contextWindow.map(_.toInt), Some(128000)) + + test("between entries of the same scope the one that spells out more of the id wins"): + val presets = ModelPresets.of( + List( + entry("glm-4\\.7-flash")(ModelConfig(displayName = Some("flash"))), + entry("glm-4\\.7")(ModelConfig(displayName = Some("plain"))), + ) + ) + // Written in the less specific order on purpose: specificity is computed, not positional. + assertEquals(presets(spec(modelId = "glm-4.7-flash")).settings.displayName, Some("flash")) + assertEquals(presets(spec(modelId = "glm-4.7")).settings.displayName, Some("plain")) + + test("`only` drops the lower-priority entries that also match"): + val presets = ModelPresets.layered( + shipped = List( + entry("glm-5\\.4")(ModelConfig(efforts = Some(List("low", "high", "max")), contextWindow = window(1000000))) + ), + own = List(entry("glm-5\\.4", only = true)(ModelConfig(efforts = Some(List("max"))))), + ) + val settings = presets(spec()).settings + assertEquals(settings.efforts, Some(List("max"))) + // The shipped window went with the entry it was written on. + assertEquals(settings.contextWindow, None) + + test("an empty effort list survives merging: the model takes no effort setting"): + val presets = ModelPresets.of(List(entry("glm-4\\.7")(ModelConfig(efforts = Some(Nil), thinking = Some(true))))) + val settings = presets(spec(modelId = "glm-4.7")).settings + assertEquals(settings.efforts, Some(Nil)) + assertEquals(settings.thinking, Some(true)) + + // ---- precedence ---- + + test("what the provider reports or the user configures wins over a preset"): + val presets = ModelPresets.of( + List(entry("glm-5\\.4")(ModelConfig(efforts = Some(List("low", "high", "max")), contextWindow = window(1000000)))) + ) + val reported = ModelConfig(contextWindow = window(200000), efforts = Some(List("high"))) + val settings = presets(spec(settings = reported)).settings + assertEquals(settings.contextWindow.map(_.toInt), Some(200000)) + assertEquals(settings.efforts, Some(List("high"))) + + test("a user entry outranks a shipped one however specific the shipped one is"): + val presets = ModelPresets.layered( + shipped = List(entry("glm-5\\.4", provider = Some("zai"))(ModelConfig(efforts = Some(List("low"))))), + own = List(entry("glm-5\\.4")(ModelConfig(efforts = Some(List("max"))))), + ) + assertEquals(presets(spec()).settings.efforts, Some(List("max"))) + + // ---- robustness ---- + + test("an entry that cannot be used is skipped, not fatal"): + val presets = ModelPresets.of( + List( + entry("glm-5\\.4(")(ModelConfig(efforts = Some(List("low")))), + entry("glm-5\\.4")(ModelConfig(efforts = Some(List("max")))), + ) + ) + assertEquals(presets.size, 1) + assertEquals(presets(spec()).settings.efforts, Some(List("max"))) + + test("an entry that fills nothing in is dropped: a misspelled field is silently ignored by JSON"): + // `"contextWindows"` is not a field, so the reader leaves the entry empty. + val read = upickle.default.read[List[PresetEntry]]( + """[{"match": {"name": "glm-5\\.4"}, "metadata": {"contextWindows": 1000}}]""" + ) + assert(ModelPresets.fillsNothing(read.head)) + assertEquals(ModelPresets.of(read).size, 0) + + test("a catalog cannot set a choice the user owns"): + val metadata = ModelConfig(reasoning = Some("max"), webSearch = Some(true), maxTokens = Some(1000)) + assertEquals(ModelPresets.unsettable(metadata).sorted, List("maxTokens", "reasoning", "webSearch")) + // Even if such an entry is loaded, filling never applies those fields. + val presets = ModelPresets.of(List(entry("glm-5\\.4")(metadata.copy(efforts = Some(List("max")))))) + val settings = presets(spec()).settings + assertEquals(settings.efforts, Some(List("max"))) + assertEquals(settings.reasoning, None) + assertEquals(settings.webSearch, None) + assertEquals(settings.maxTokens, None) + + // ---- the catalog shipped with the release ---- + + test("the shipped catalog is readable, and every entry of it is usable"): + val presets = ModelPresets.builtin + assert(presets.size > 0, "the build did not bundle /atc/models.json") + val entries = upickle.default.read[List[PresetEntry]](Config.resource("/atc/models.json")) + assertEquals(entries.size, presets.size, "some entry of the shipped catalog was skipped") + for e <- entries do + assert(!ModelPresets.fillsNothing(e), s"${e.when.name} fills nothing in; check its field names") + assertEquals(ModelPresets.unsettable(e.metadata), Nil, s"${e.when.name} sets a field a catalog cannot set") + + test("the shipped catalog gives GLM-5.4 the three effort levels Z.ai honors"): + val settings = ModelPresets.builtin(spec(modelId = "glm-5.4")).settings + assertEquals(settings.efforts, Some(List("low", "high", "max"))) + assertEquals(settings.contextWindow.map(_.toInt), Some(1000000)) + + test("the shipped catalog gives the GLM models that only switch thinking no effort levels"): + assertEquals(ModelPresets.builtin(spec(modelId = "glm-4.7")).settings.efforts, Some(Nil)) + assertEquals(ModelPresets.builtin(spec(modelId = "glm-5")).settings.efforts, Some(Nil)) + + test("the shipped catalog covers every model family it was built from"): + // One id per vendor family, so regenerating the catalog cannot quietly drop one. + val families = List( + "glm-5.4" -> Some(List("low", "high", "max")), + "glm-4.7" -> Some(Nil), + "gpt-5.6" -> Some(List("none", "low", "medium", "high", "xhigh", "max")), + "gpt-5.4-mini" -> Some(List("none", "low", "medium", "high", "xhigh")), + "claude-opus-5-5" -> Some(List("low", "medium", "high", "xhigh", "max")), + "grok-4.6" -> Some(List("low", "medium", "high", "xhigh")), + "kimi-k3" -> Some(List("low", "high", "max")), + "kimi-k2.6" -> Some(Nil), + "moonshot-v1-32k" -> None, + // Only a gateway's listing described this one, so it used to be scoped to that + // route; collapsed to the model, it says what it says everywhere: a thinking + // switch and no effort levels. + "MiniMax-M2.7" -> Some(Nil), + "deepseek-v4-pro" -> Some(List("low", "high", "max")), + "qwen3.8-max" -> Some(List("low", "medium", "xhigh")), + "qwen3.7-plus" -> Some(Nil), + "mimo-v2.6-pro" -> Some(Nil), + "codegeex-4" -> None, + "charglm-4" -> None, + ) + for (id, efforts) <- families do + assertEquals(ModelPresets.builtin(spec(modelId = id)).settings.efforts, efforts, id) + + test("the shipped catalog knows a context window for the models whose provider reports none"): + val windows = List( + "glm-5.4" -> 1000000, + "glm-4.5" -> 131072, + "gpt-5.6" -> 1050000, + "gpt-5.4-mini" -> 400000, + "claude-haiku-4-5" -> 200000, + "grok-4.6" -> 500000, + "kimi-k3" -> 1048576, + "k3-256k" -> 262144, + "moonshot-v1-32k" -> 32768, + "MiniMax-M2.5-highspeed" -> 204800, + "qwen3-max" -> 262144, + "mimo-v2-omni" -> 262144, + "charglm-4" -> 8192, + ) + for (id, tokens) <- windows do + assertEquals(ModelPresets.builtin(spec(modelId = id)).settings.contextWindow.map(_.toInt), Some(tokens), id) + + test("an id a gateway lists under a vendor prefix is described by the same entry"): + for (listed, bare) <- List( + "z-ai/glm-5.4" -> "glm-5.4", + "openai/gpt-5.6" -> "gpt-5.6", + "x-ai/grok-4.6" -> "grok-4.6", + "moonshotai/kimi-k3" -> "kimi-k3", + ) + do + val under = ModelPresets.builtin(spec(provider = "openrouter", api = "openai", modelId = listed)).settings + val plain = ModelPresets.builtin(spec(provider = "openrouter", api = "openai", modelId = bare)).settings + assertEquals(under.efforts, plain.efforts, listed) + assertEquals(under.contextWindow.map(_.toInt), plain.contextWindow.map(_.toInt), listed) + + test("every shipped entry names a model and nothing else"): + // The catalog describes models, not providers or endpoints: the same model + // behind a vendor and behind a gateway gets the same description, and no + // entry depends on what the user called a provider or where it points. + val entries = upickle.default.read[List[PresetEntry]](Config.resource("/atc/models.json")) + val scoped = entries.filter(e => e.when.provider.isDefined || e.when.api.isDefined) + assertEquals(scoped.map(_.when.name), Nil, "a shipped entry is scoped to a provider or an api") + + test("a model is described the same however its provider is reached"): + // Whatever provider name, protocol or address serves GLM-5.3, it is one model. + val everywhere = List( + spec(provider = "zai", api = "anthropic", modelId = "glm-5.3"), + spec(provider = "mine", api = "openai", modelId = "glm-5.3"), + spec(provider = "openrouter", api = "openai", modelId = "z-ai/glm-5.3"), + ) + for s <- everywhere do + val settings = ModelPresets.builtin(s.copy(baseUrl = Some("https://gw.example/v1"))).settings + assertEquals(settings.efforts, Some(List("low", "high", "max")), s.toString) + assertEquals(settings.contextWindow.map(_.toInt), Some(1000000), s.toString) + + test("a model OpenAI serves is given no thinking switch, which OpenAI rejects"): + for id <- List("gpt-5.6", "gpt-5.4", "gpt-5.4-pro", "gpt-5.3-codex", "gpt-6-astra") do + assertEquals(ModelPresets.builtin(spec(api = "openai-responses", modelId = id)).settings.thinking, None, id) + + // ---- wiring ---- + + test("a listed model is enriched through the catalog, by name and in the model list"): + val provider = + ProviderConfig(api = Some("anthropic"), url = Some("https://api.z.ai/api/anthropic"), key = Some("k")) + val config = Config(providers = Map("zai" -> provider)) + val listed = (p: ModelSpec) => List(p.listed("glm-5.4", p.settings)) + val catalog = ModelCatalog.from(config, discover = Some(listed), presets = ModelPresets.builtin) + assertEquals(catalog.find("zai/glm-5.4").settings.efforts, Some(List("low", "high", "max"))) + assertEquals(catalog.models.map(_.settings.efforts), List(Some(List("low", "high", "max")))) + + test("an unknown id taken as given is enriched too"): + val provider = + ProviderConfig(api = Some("anthropic"), url = Some("https://api.z.ai/api/anthropic"), key = Some("k")) + val config = Config(providers = Map("zai" -> provider)) + // No discovery, so `find` takes the id as given; the catalog still describes it. + val catalog = ModelCatalog.from(config, presets = ModelPresets.builtin) + assertEquals(catalog.find("zai/glm-5.4").settings.efforts, Some(List("low", "high", "max"))) + + test("a stored model list keeps what the provider said, never a preset"): + val dir = TestEnv.tempDir("atc-presets") + val store = ModelListStore.in(dir) + val provider = + ProviderConfig(api = Some("anthropic"), url = Some("https://api.z.ai/api/anthropic"), key = Some("k")) + val config = Config(providers = Map("zai" -> provider)) + val listed = (p: ModelSpec) => List(p.listed("glm-5.4", p.settings)) + val catalog = + ModelCatalog.from(config, discover = Some(listed), store = Some(store), presets = ModelPresets.builtin) + assertEquals(catalog.models.map(_.settings.efforts), List(Some(List("low", "high", "max")))) + val saved = Files.readString(dir.resolve("model-lists.json")).nn + assert(!saved.contains("efforts"), s"a preset reached the stored list: $saved") + assert(!saved.contains("contextWindow"), s"a preset reached the stored list: $saved") + // Read back, the stored list is still bare, and the catalog enriches it again. + assertEquals(store.load(catalog.discoverable).values.flatten.map(_.settings.efforts).toList, List(None)) + + test("a user catalog in the atc directory is layered over the shipped one"): + val dir = TestEnv.tempDir("atc-presets") + Files.writeString( + dir.resolve(ModelPresets.FileName), + """[{"match": {"name": "glm-5\\.4"}, "metadata": {"efforts": ["max"], "contextWindow": "128k"}}]""", + ) + val settings = ModelPresets.load(dir)(spec(modelId = "glm-5.4")).settings + assertEquals(settings.efforts, Some(List("max"))) + assertEquals(settings.contextWindow.map(_.toInt), Some(128000)) + + test("a user entry refines a shipped one rather than replacing it"): + val dir = TestEnv.tempDir("atc-presets") + // Only the effort levels are corrected; the shipped window still applies. + Files.writeString( + dir.resolve(ModelPresets.FileName), + """[{"match": {"name": "glm-5\\.4"}, "metadata": {"efforts": ["max"]}}]""", + ) + val settings = ModelPresets.load(dir)(spec(modelId = "glm-5.4")).settings + assertEquals(settings.efforts, Some(List("max"))) + assertEquals(settings.contextWindow.map(_.toInt), Some(1000000)) + + test("an unreadable user catalog leaves the shipped one in place"): + val dir = TestEnv.tempDir("atc-presets") + Files.writeString(dir.resolve(ModelPresets.FileName), "{ not json") + assertEquals(ModelPresets.load(dir)(spec(modelId = "glm-5.4")).settings.efforts, Some(List("low", "high", "max"))) diff --git a/doc/development.md b/doc/development.md index 2615cc41..2fe5bcab 100644 --- a/doc/development.md +++ b/doc/development.md @@ -977,10 +977,19 @@ conversation (renewed by `/new`) for gateways that route by session, such as Ope The `zai` preset is Z.ai's GLM Coding Plan through its Anthropic-compatible endpoint (`api` `anthropic`, `url` `https://api.z.ai/api/anthropic`, key `${ZAI_API_KEY}`); the Anthropic adapter needs nothing of its own for it. Its model list has display names but -no input limits or capabilities, so a listed GLM model has no `contextWindow` (set one to -have the conversation trimmed and compacted) and takes adaptive thinking. GLM ignores -`thinking: {"type": "disabled"}` and `output_config.effort`: calls that ask for no -thinking, such as next-request prediction, still think, and `/effort` changes nothing. +no input limits or capabilities, so nothing a listed GLM model needs comes from the +provider: the [preset model catalog](#the-preset-model-catalog) supplies its window and +its effort levels instead. + +Z.ai does honor `output_config.effort`, translating it to GLM's own `reasoning_effort`, +but it validates nothing: an effort GLM does not take is accepted and silently treated as +the default rather than refused. GLM-5.4, GLM-5.3 and GLM-5.3-Flash take `low`, `high` and +`max` only, so the five levels the Anthropic adapter would otherwise offer would leave +`/effort medium` and `/effort xhigh` looking as though they had worked. The catalog narrows +the choice to the three that act. GLM-5.4 and GLM-5.3 also cannot stop thinking at all +(`thinking.type` takes only `enabled`), so the `thinking: {"type": "disabled"}` that calls +asking for no thinking send — next-request prediction, for one — is accepted and ignored, +and those calls still think, though less than an unconstrained one. `reasoningStyle` describes a Chat Completions provider (`api: openai`) that asks for reasoning and returns it in its own way. `request` is a JSON object merged into the body of a @@ -1016,6 +1025,80 @@ skipped. Anthropic's and DeepSeek's lists supply context windows and effort leve output limit is not taken, since it would be sent with every request and reserved from the window. Without `-m` or `model`, a session starts with the model last chosen with `/model`. +#### The preset model catalog + +Some providers report almost nothing about the models they list — Z.ai's list has display +names and no capabilities at all — which leaves a model with no context window, so nothing +is compacted or trimmed, and with the full effort ladder of its protocol whether or not it +takes those levels. `app/resources/atc/models.json` (`ModelPresets`) describes the models +that need it, and a user's own `~/.atc/models.json` is layered over it. + +The shipped catalog is translated from ZCode's own model-config rules: 575 rules, of which +169 state a context window, an effort ladder or a thinking switch — the rest set only +`enabled`, map request fields or describe capabilities ATC has no field for — collapsed into +115 entries covering GLM, GPT, Claude, Grok, Kimi and Moonshot, MiniMax, DeepSeek, Qwen, +MiMo, Doubao, StepFun, Hunyuan, CodeGeeX and others. Many of those models reach ATC through +a provider that reports their capabilities, in which case the catalog is never consulted +for them — it is the gateway, the OpenAI-compatible server and the vendor that reports +nothing where the entries earn their place. + +Every shipped entry names a model and nothing else, so one model has one description +wherever it is served; `ModelPresetsSuite` holds the catalog to that. ZCode instead states +some facts per address, because a vendor, OpenRouter and OpenCode Zen serve the same model +with different limits. Collapsing those needs a policy: a fact ZCode states about the model +itself wins, and an address or a gateway listing may only fill a field no such rule gives — +a gateway that lists GLM-5.3 with one effort level is describing its listing, not the model. +Where only addresses state a field, the value the most of them agree on wins, except for a +context window, where the smallest wins: overestimating a window makes a request fail at the +provider, while underestimating it only compacts earlier. A tie on effort levels goes to the +longer list, since a level the model does not take is ignored while a level the catalog +hides cannot be asked for at all. + +An entry matches models and supplies metadata for them: + +```json +{ "match": { "name": "glm-5\\.4(-[0-9]{6,8})?" }, + "metadata": { "displayName": "GLM-5.4", "contextWindow": "1m", + "efforts": ["low", "high", "max"], "thinking": true } } +``` + +`name` is a regular expression matched against the whole model id, ignoring case; an id a +provider lists under a vendor prefix (OpenRouter's `z-ai/glm-5.4`) is also tried without it, +so no entry spells the prefix out. `provider` and `api` narrow an entry to one provider of +the config or one wire protocol, exact names matched ignoring case, both defaulting to all +of them (`"*"` says so explicitly); protocol names are canonical, so `claude` and +`anthropic` are one scope. `api` is what an entry setting `thinking` ordinarily wants, since +that field means a different thing on each protocol. The shipped catalog uses neither scope; +they are there for a user's own `models.json`, where one provider is often exactly what +needs correcting. + +`metadata` is read as a model's own settings, but only the fields that describe a model +(`contextWindow`, `displayName`) or constrain what the user may choose (`efforts`, +`thinking`) are ever applied (`ModelPresets.Settable`). A catalog never sets a choice the +user owns, such as `reasoning` or `webSearch`, and never `maxTokens`, which would be sent +with every request and reserved from the window rather than describing the model. + +What a provider reports wins over the catalog, and what the user configures wins over both: +the catalog only fills in what is missing. Within it, every entry that matches a model is +merged field by field, the more specific winning each field — one entry can give a model's +effort levels everywhere while another corrects its window on one provider. Specificity is +computed, never the order entries are written in: a provider scope beats an api scope, which +beats none, and between two of the same scope the `name` that spells out more of the id +wins. A user's entry outranks every shipped entry whatever either one's specificity, so an +unscoped correction still takes effect over a shipped entry written for one provider; +`"only": true` drops the lower-priority entries that match as well, for when a shipped entry +has to be replaced rather than refined. + +An entry that cannot be used is reported under `--debug` and skipped, never fatal: one whose +`name` is not a valid regular expression, and one that fills nothing in, which is what a +misspelled field name amounts to, since the JSON reader ignores members it does not know. +`ModelPresetsSuite` holds the shipped catalog to both rules. + +Presets are applied as a model leaves `ModelCatalog` — by `models`, `knownModels` and +`find` — and never before a provider's list is stored. `~/.atc/model-lists.json` keeps only +what the provider itself said, so correcting the catalog takes effect at once instead of +staying frozen in the list a previous session cached. + **First run.** An interactive start without `~/.atc/config.json` or `-c` runs `FirstRun`: the user chooses one of the `atc/providers.json` presets, gives its key unless one is already bound (read by `Tui.askSecret`, masked and kept out of the prompt history) or signs