GeezerVison is showing github.com — fix(server): Claude effort picker marks levels capped by maxEffortLev… · Pipyakas/t3code@a91d6c8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/ClaudeModelCatalog.test.ts +240Lines changed: 240 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,8 @@ import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts"; | 3 | 3 |
| 4 | 4 | import { resolveProviderCatalog, type ModelManifestData } from "./ModelManifest.ts"; | 5 | 5 | import { | | 6 | + capClaudeCatalogEffort, | | 7 | + type ClaudeModelCatalog, | 6 | 8 | formatClaudeVersionUpgradeMessage, | 7 | 9 | getClaudeCatalogModelCapabilities, | 8 | 10 | normalizeClaudeCatalogEffort, | @@ -230,4 +232,242 @@ describe("Claude model catalog", () => { | 230 | 232 | ["claude-synthetic-next", "claude-custom-tuned"], | 231 | 233 | ); | 232 | 234 | }); | | 235 | + | | 236 | + it("marks effort options that would run above the cap without changing ids or defaults", () => { | | 237 | + const effortModel = (slug: string) => ({ | | 238 | + slug, | | 239 | + name: slug, | | 240 | + isCustom: false, | | 241 | + capabilities: { | | 242 | + optionDescriptors: [ | | 243 | + { | | 244 | + id: "effort", | | 245 | + label: "Reasoning", | | 246 | + type: "select" as const, | | 247 | + options: [ | | 248 | + { id: "medium", label: "Medium" }, | | 249 | + { id: "high", label: "High", isDefault: true }, | | 250 | + { id: "xhigh", label: "Extra High" }, | | 251 | + { id: "max", label: "Max" }, | | 252 | + { id: "ultracode", label: "Ultracode" }, | | 253 | + { id: "ultrathink", label: "Ultrathink" }, | | 254 | + ], | | 255 | + }, | | 256 | + ], | | 257 | + }, | | 258 | + }); | | 259 | + const models = [effortModel("claude-plain"), effortModel("claude-remapped")]; | | 260 | + const catalog: ClaudeModelCatalog = { | | 261 | + models: [ | | 262 | + { | | 263 | + model: models[0]!, | | 264 | + runtime: { effortMap: { ultracode: "xhigh", ultrathink: null } }, | | 265 | + compatibility: {}, | | 266 | + }, | | 267 | + { | | 268 | + model: models[1]!, | | 269 | + runtime: { effortMap: { max: "high", xhigh: "max", ultrathink: null } }, | | 270 | + compatibility: {}, | | 271 | + }, | | 272 | + ], | | 273 | + }; | | 274 | + const options = (cap: "high" | "xhigh") => | | 275 | + capClaudeCatalogEffort(catalog, models, [{ maxEffortLevel: cap }]).map((model) => { | | 276 | + const effort = model.capabilities?.optionDescriptors?.[0]; | | 277 | + return effort?.type === "select" ? effort.options : undefined; | | 278 | + }); | | 279 | + | | 280 | + assert.deepStrictEqual(options("high"), [ | | 281 | + [ | | 282 | + { id: "medium", label: "Medium" }, | | 283 | + { id: "high", label: "High", isDefault: true }, | | 284 | + { id: "xhigh", label: "Extra High (capped to High)" }, | | 285 | + { id: "max", label: "Max (capped to High)" }, | | 286 | + { id: "ultracode", label: "Ultracode (capped to High)" }, | | 287 | + { id: "ultrathink", label: "Ultrathink" }, | | 288 | + ], | | 289 | + [ | | 290 | + { id: "medium", label: "Medium" }, | | 291 | + { id: "high", label: "High", isDefault: true }, | | 292 | + { id: "xhigh", label: "Extra High (capped to High)" }, | | 293 | + { id: "max", label: "Max" }, | | 294 | + { id: "ultracode", label: "Ultracode" }, | | 295 | + { id: "ultrathink", label: "Ultrathink" }, | | 296 | + ], | | 297 | + ]); | | 298 | + // An effortMap that sends Extra High as max stays unmarked under an xhigh cap: the CLI runs | | 299 | + // it at xhigh, which is what the label says. | | 300 | + assert.deepStrictEqual( | | 301 | + options("xhigh").map((model) => model?.map((option) => option.label)), | | 302 | + [ | | 303 | + ["Medium", "High", "Extra High", "Max (capped to Extra High)", "Ultracode", "Ultrathink"], | | 304 | + ["Medium", "High", "Extra High", "Max", "Ultracode", "Ultrathink"], | | 305 | + ], | | 306 | + ); | | 307 | + | | 308 | + // A per-model cap applies without a top-level one and leaves other models uncapped. | | 309 | + assert.deepStrictEqual( | | 310 | + capClaudeCatalogEffort(catalog, models, [ | | 311 | + { modelMaxEffortLevels: new Map([["claude-plain", "high"]]) }, | | 312 | + ]).map((model) => { | | 313 | + const effort = model.capabilities?.optionDescriptors?.[0]; | | 314 | + return effort?.type === "select" ? effort.options.map((option) => option.label) : []; | | 315 | + }), | | 316 | + [ | | 317 | + [ | | 318 | + "Medium", | | 319 | + "High", | | 320 | + "Extra High (capped to High)", | | 321 | + "Max (capped to High)", | | 322 | + "Ultracode (capped to High)", | | 323 | + "Ultrathink", | | 324 | + ], | | 325 | + ["Medium", "High", "Extra High", "Max", "Ultracode", "Ultrathink"], | | 326 | + ], | | 327 | + ); | | 328 | + | | 329 | + // Without an Extra High option (Opus 4.6), the CLI clamps an xhigh cap down to High. | | 330 | + const noXhigh = { | | 331 | + slug: "claude-no-xhigh", | | 332 | + name: "claude-no-xhigh", | | 333 | + isCustom: false, | | 334 | + capabilities: { | | 335 | + optionDescriptors: [ | | 336 | + { | | 337 | + id: "effort", | | 338 | + label: "Reasoning", | | 339 | + type: "select" as const, | | 340 | + options: [ | | 341 | + { id: "low", label: "Low" }, | | 342 | + { id: "medium", label: "Medium" }, | | 343 | + { id: "high", label: "High", isDefault: true }, | | 344 | + { id: "max", label: "Max" }, | | 345 | + ], | | 346 | + }, | | 347 | + ], | | 348 | + }, | | 349 | + }; | | 350 | + const [capped] = capClaudeCatalogEffort( | | 351 | + { models: [] }, | | 352 | + [noXhigh], | | 353 | + [{ maxEffortLevel: "xhigh" }], | | 354 | + ); | | 355 | + const cappedEffort = capped?.capabilities?.optionDescriptors?.[0]; | | 356 | + assert.deepStrictEqual( | | 357 | + cappedEffort?.type === "select" ? cappedEffort.options.map((option) => option.label) : [], | | 358 | + ["Low", "Medium", "High", "Max (capped to High)"], | | 359 | + ); | | 360 | + }); | | 361 | + | | 362 | + it("leaves custom model effort labels alone under a cap", () => { | | 363 | + const custom = { | | 364 | + slug: "claude-custom-copy", | | 365 | + name: "Copy", | | 366 | + isCustom: true, | | 367 | + capabilities: { | | 368 | + optionDescriptors: [ | | 369 | + { | | 370 | + id: "effort", | | 371 | + label: "Reasoning", | | 372 | + type: "select" as const, | | 373 | + options: [ | | 374 | + { id: "high", label: "High", isDefault: true }, | | 375 | + { id: "max", label: "Max (capped to High)" }, | | 376 | + ], | | 377 | + }, | | 378 | + ], | | 379 | + }, | | 380 | + }; | | 381 | + | | 382 | + assert.deepStrictEqual( | | 383 | + capClaudeCatalogEffort({ models: [] }, [custom], [{ maxEffortLevel: "high" }]), | | 384 | + [custom], | | 385 | + ); | | 386 | + }); | | 387 | + | | 388 | + it("resolves maxEffortLevel per settings source and per model key like Claude Code", () => { | | 389 | + const opus = { | | 390 | + slug: "claude-opus-5-5", | | 391 | + name: "Opus 5.5", | | 392 | + isCustom: false, | | 393 | + aliases: ["opus", "opus-5.5", "claude-opus-5.5"], | | 394 | + capabilities: { | | 395 | + optionDescriptors: [ | | 396 | + { | | 397 | + id: "effort", | | 398 | + label: "Reasoning", | | 399 | + type: "select" as const, | | 400 | + options: [ | | 401 | + { id: "low", label: "Low" }, | | 402 | + { id: "medium", label: "Medium" }, | | 403 | + { id: "high", label: "High", isDefault: true }, | | 404 | + { id: "max", label: "Max" }, | | 405 | + ], | | 406 | + }, | | 407 | + ], | | 408 | + }, | | 409 | + }; | | 410 | + const catalog: ClaudeModelCatalog = { | | 411 | + models: [ | | 412 | + { model: opus, runtime: {}, compatibility: {} }, | | 413 | + { | | 414 | + model: { | | 415 | + slug: "claude-opus-5", | | 416 | + name: "Opus 5", | | 417 | + isCustom: false, | | 418 | + capabilities: { optionDescriptors: [] }, | | 419 | + }, | | 420 | + runtime: {}, | | 421 | + compatibility: {}, | | 422 | + }, | | 423 | + ], | | 424 | + }; | | 425 | + const maxLabel = (sources: Parameters<typeof capClaudeCatalogEffort>[2]) => { | | 426 | + const effort = capClaudeCatalogEffort(catalog, [opus], sources)[0]?.capabilities | | 427 | + ?.optionDescriptors?.[0]; | | 428 | + return effort?.type === "select" ? effort.options.at(-1)?.label : undefined; | | 429 | + }; | | 430 | + const perModel = (entries: Record<string, "low" | "medium" | "high" | "max">) => ({ | | 431 | + modelMaxEffortLevels: new Map(Object.entries(entries)), | | 432 | + }); | | 433 | + | | 434 | + // Claude Code matches family aliases and [1m] spellings case-insensitively. | | 435 | + assert.strictEqual(maxLabel([perModel({ Opus: "medium" })]), "Max (capped to Medium)"); | | 436 | + assert.strictEqual( | | 437 | + maxLabel([perModel({ "claude-opus-5-5[1M]": "medium" })]), | | 438 | + "Max (capped to Medium)", | | 439 | + ); | | 440 | + assert.strictEqual(maxLabel([perModel({ " OPUS[1m] ": "medium" })]), "Max (capped to Medium)"); | | 441 | + // Other keys name the most specific model id they contain, so dated and provider-prefixed ids | | 442 | + // match while the catalog's dotted aliases and a less specific model id do not. | | 443 | + for (const key of [ | | 444 | + "claude-opus-5-5-20260101", | | 445 | + "CLAUDE-OPUS-5-5-20260101[1m]", | | 446 | + "us.anthropic.claude-opus-5-5-v1:0", | | 447 | + "claude-opus-5-5-latest", | | 448 | + ]) { | | 449 | + assert.strictEqual(maxLabel([perModel({ [key]: "medium" })]), "Max (capped to Medium)", key); | | 450 | + } | | 451 | + for (const key of ["claude-opus-5.5", "opus-5.5", "opus-5-5", "opus [1m]", "claude-opus-5"]) { | | 452 | + assert.strictEqual(maxLabel([perModel({ [key]: "medium" })]), "Max", key); | | 453 | + } | | 454 | + // Keys naming the same model keep the lowest cap, and a `max` key does not exempt it. | | 455 | + assert.strictEqual( | | 456 | + maxLabel([perModel({ opus: "max", "claude-opus-5-5": "low" })]), | | 457 | + "Max (capped to Low)", | | 458 | + ); | | 459 | + // A per-model `max` only exempts the model from its own source's top-level cap. | | 460 | + assert.strictEqual( | | 461 | + maxLabel([{ maxEffortLevel: "medium", ...perModel({ opus: "max" }) }]), | | 462 | + "Max", | | 463 | + ); | | 464 | + assert.strictEqual( | | 465 | + maxLabel([perModel({ opus: "max" }), { maxEffortLevel: "high" }]), | | 466 | + "Max (capped to High)", | | 467 | + ); | | 468 | + assert.strictEqual( | | 469 | + maxLabel([{ maxEffortLevel: "medium" }, perModel({ opus: "high" })]), | | 470 | + "Max (capped to Medium)", | | 471 | + ); | | 472 | + }); | 233 | 473 | }); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/ClaudeModelCatalog.ts +132-1Lines changed: 132 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -322,14 +322,145 @@ export function formatClaudeVersionUpgradeMessage( | 322 | 322 | return `Claude Code ${versionLabel} is too old for ${unavailable.model.name}. Upgrade to v${unavailable.compatibility.minVersion} or newer to access it.`; | 323 | 323 | } | 324 | 324 |
| | 325 | +/** Claude Code's effort levels, lowest first. */ | | 326 | +export const CLAUDE_EFFORT_LEVELS = ["low", "medium", "high", "xhigh", "max"] as const; | | 327 | +export type ClaudeEffortLevel = (typeof CLAUDE_EFFORT_LEVELS)[number]; | | 328 | + | | 329 | +function claudeEffortRank(level: string | undefined): number { | | 330 | + return CLAUDE_EFFORT_LEVELS.indexOf(level as ClaudeEffortLevel); | | 331 | +} | | 332 | + | | 333 | +/** One settings source's effort caps, with `modelSettings` keys as the user wrote them. */ | | 334 | +export interface ClaudeEffortCapSource { | | 335 | + readonly maxEffortLevel?: ClaudeEffortLevel; | | 336 | + readonly modelMaxEffortLevels?: ReadonlyMap<string, ClaudeEffortLevel>; | | 337 | +} | | 338 | + | | 339 | +const CLAUDE_1M_SUFFIX = "[1m]"; | | 340 | +const CLAUDE_CODE_FAMILY_ALIASES: ReadonlySet<string> = new Set([ | | 341 | + "opus", | | 342 | + "sonnet", | | 343 | + "haiku", | | 344 | + "fable", | | 345 | +]); | | 346 | + | | 347 | +/** | | 348 | + * Names the model a `modelSettings` key caps the way Claude Code does, which is narrower than the | | 349 | + * catalog's aliases: only a family alias (optionally `[1m]`) resolves as an alias, and any other key | | 350 | + * names the most specific model id it contains, so `claude-opus-5-5-20260101` and Bedrock ids match | | 351 | + * while `claude-opus-5.5` matches nothing. | | 352 | + */ | | 353 | +function resolveClaudeModelSettingsKey( | | 354 | + catalog: ClaudeModelCatalog, | | 355 | + key: string, | | 356 | +): string | undefined { | | 357 | + const lowered = key.trim().toLowerCase(); | | 358 | + const unsuffixed = lowered.endsWith(CLAUDE_1M_SUFFIX) | | 359 | + ? lowered.slice(0, -CLAUDE_1M_SUFFIX.length) | | 360 | + : lowered; | | 361 | + if (CLAUDE_CODE_FAMILY_ALIASES.has(unsuffixed)) { | | 362 | + return catalog.models.find( | | 363 | + (entry) => !entry.model.isCustom && entry.model.aliases?.includes(unsuffixed), | | 364 | + )?.model.slug; | | 365 | + } | | 366 | + let match: string | undefined; | | 367 | + for (const { model } of catalog.models) { | | 368 | + if (!model.isCustom && lowered.includes(model.slug) && model.slug.length > (match?.length ?? 0)) | | 369 | + match = model.slug; | | 370 | + } | | 371 | + return match; | | 372 | +} | | 373 | + | | 374 | +/** Keys `modelSettings` caps by canonical slug, keeping the lowest cap when several keys name one model. */ | | 375 | +function resolveClaudeModelMaxEffortLevels( | | 376 | + catalog: ClaudeModelCatalog, | | 377 | + levels: ReadonlyMap<string, ClaudeEffortLevel> | undefined, | | 378 | +): ReadonlyMap<string, ClaudeEffortLevel> { | | 379 | + const resolved = new Map<string, ClaudeEffortLevel>(); | | 380 | + for (const [key, level] of levels ?? []) { | | 381 | + const slug = resolveClaudeModelSettingsKey(catalog, key); | | 382 | + if (!slug) continue; | | 383 | + const current = resolved.get(slug); | | 384 | + if (!current || claudeEffortRank(level) < claudeEffortRank(current)) resolved.set(slug, level); | | 385 | + } | | 386 | + return resolved; | | 387 | +} | | 388 | + | | 389 | +/** | | 390 | + * Claude Code clamps efforts above `maxEffortLevel` to the model's highest level under it, so | | 391 | + * such options are relabelled, not removed, keeping stored selections valid. Per-model caps | | 392 | + * replace their own source's top-level cap and `max` means none; across sources the lowest wins. | | 393 | + */ | | 394 | +export function capClaudeCatalogEffort( | | 395 | + catalog: ClaudeModelCatalog, | | 396 | + models: ReadonlyArray<ServerProviderModel>, | | 397 | + sources: ReadonlyArray<ClaudeEffortCapSource>, | | 398 | +): ReadonlyArray<ServerProviderModel> { | | 399 | + if (sources.length === 0) return models; | | 400 | + const resolvedSources = sources.map((source) => ({ | | 401 | + maxEffortLevel: source.maxEffortLevel, | | 402 | + modelMaxEffortLevels: resolveClaudeModelMaxEffortLevels(catalog, source.modelMaxEffortLevels), | | 403 | + })); | | 404 | + return models.map((model) => { | | 405 | + // Custom model labels are user-authored, so they are left alone. | | 406 | + if (model.isCustom || !model.capabilities?.optionDescriptors) return model; | | 407 | + const modelMaxEffortLevel = resolvedSources.reduce<ClaudeEffortLevel | undefined>( | | 408 | + (lowest, source) => { | | 409 | + const level = source.modelMaxEffortLevels.get(model.slug) ?? source.maxEffortLevel; | | 410 | + return level && | | 411 | + level !== "max" && | | 412 | + (!lowest || claudeEffortRank(level) < claudeEffortRank(lowest)) | | 413 | + ? level | | 414 | + : lowest; | | 415 | + }, | | 416 | + undefined, | | 417 | + ); | | 418 | + if (!modelMaxEffortLevel) return model; | | 419 | + const capRank = claudeEffortRank(modelMaxEffortLevel); | | 420 | + return { | | 421 | + ...model, | | 422 | + capabilities: { | | 423 | + ...model.capabilities, | | 424 | + optionDescriptors: model.capabilities.optionDescriptors.map((descriptor) => { | | 425 | + if (descriptor.id !== "effort" || descriptor.type !== "select") return descriptor; | | 426 | + const cap = descriptor.options.reduce<{ rank: number; label: string } | undefined>( | | 427 | + (best, option) => { | | 428 | + const rank = claudeEffortRank(option.id); | | 429 | + return rank >= 0 && rank <= capRank && rank > (best?.rank ?? -1) | | 430 | + ? { rank, label: option.label } | | 431 | + : best; | | 432 | + }, | | 433 | + undefined, | | 434 | + ); | | 435 | + if (!cap) return descriptor; | | 436 | + return { | | 437 | + ...descriptor, | | 438 | + options: descriptor.options.map((option) => { | | 439 | + const idRank = claudeEffortRank(option.id); | | 440 | + const sentRank = claudeEffortRank( | | 441 | + normalizeClaudeCatalogEffort(catalog, option.id, model.slug), | | 442 | + ); | | 443 | + const displayedRank = idRank >= 0 ? idRank : sentRank; | | 444 | + const runsRank = sentRank >= 0 ? sentRank : idRank; | | 445 | + return displayedRank > cap.rank && runsRank > cap.rank | | 446 | + ? { ...option, label: `${option.label} (capped to ${cap.label})` } | | 447 | + : option; | | 448 | + }), | | 449 | + }; | | 450 | + }), | | 451 | + }, | | 452 | + }; | | 453 | + }); | | 454 | +} | | 455 | + | 325 | 456 | export function resolveClaudeCatalogEffort( | 326 | 457 | catalog: ClaudeModelCatalog, | 327 | 458 | model: string | null | undefined, | 328 | 459 | raw: string | null | undefined, | 329 | 460 | ): string | undefined { | 330 | 461 | if (!resolveClaudeCatalogModel(catalog, model)) { | 331 | 462 | const effort = raw?.trim(); | 332 | | - return effort && ["low", "medium", "high", "xhigh", "max"].includes(effort) | | 463 | + return effort && (CLAUDE_EFFORT_LEVELS as ReadonlyArray<string>).includes(effort) | 333 | 464 | ? effort | 334 | 465 | : undefined; | 335 | 466 | } | |
|