Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ClaudeModelCatalog.test.ts

534 lines18.9 KB

1import { assert, describe, it } from "@effect/vitest";
2import { type ModelSelection, ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
3​
4import { resolveProviderCatalog, type ModelManifestData } from "./ModelManifest.ts";
5import {
6 BUNDLED_CLAUDE_MODEL_CATALOG,
7 capClaudeCatalogEffort,
8 type ClaudeModelCatalog,
9 formatClaudeVersionUpgradeMessage,
10 getClaudeCatalogModelCapabilities,
11 normalizeClaudeCatalogEffort,
12 resolveClaudeCatalogApiModelId,
13 resolveClaudeCatalogContextWindowTokens,
14 resolveClaudeCatalogEffort,
15 resolveClaudeModelCatalog,
16 resolveClaudeModelsForVersion,
17 resolveClaudeModelSlug,
18 resolveClaudeUpdateRequiredModels,
19 scopeClaudeModelCatalog,
20 withClaudeCodeModelRoutes,
21} from "./ClaudeModelCatalog.ts";
22​
23const manifest = (): ModelManifestData => ({
24 version: 1,
25 currentModels: {},
26 providers: {
27 claudeAgent: {
28 profiles: {
29 synthetic: {
30 capabilities: {
31 optionDescriptors: [
32 {
33 id: "effort",
34 label: "Reasoning",
35 type: "select",
36 options: [{ id: "extreme", label: "Extreme", isDefault: true }],
37 },
38 {
39 id: "contextWindow",
40 label: "Context Window",
41 type: "select",
42 options: [
43 { id: "large", label: "Large", isDefault: true },
44 { id: "small", label: "Small" },
45 ],
46 },
47 ],
48 },
49 adapter: {
50 claudeCode: {
51 effortMap: { extreme: "high" },
52 modelSuffixes: { contextWindow: { large: "[large]" } },
53 },
54 },
55 },
56 },
57 models: [
58 {
59 slug: "claude-synthetic-next",
60 name: "Claude Synthetic Next",
61 aliases: ["synthetic"],
62 status: "current",
63 profile: "synthetic",
64 adapter: { claudeCode: { minVersion: "3.2.0" } },
65 },
66 ],
67 },
68 },
69});
70​
71const CLAUDE = ProviderDriverKind.make("claudeAgent");
72​
73const catalogFromManifest = (data: ModelManifestData) =>
74 resolveClaudeModelCatalog(resolveProviderCatalog(data, CLAUDE) ?? undefined);
75​
76describe("Claude model catalog", () => {
77 it("resolves capacity from selected options and fixed catalog windows without guessing custom models", () => {
78 const source = manifest();
79 const profile = source.providers!.claudeAgent!.profiles.synthetic!;
80 const catalog = catalogFromManifest({
81 ...source,
82 providers: {
83 claudeAgent: {
84 ...source.providers!.claudeAgent!,
85 profiles: {
86 fixed: {
87 capabilities: { optionDescriptors: [] },
88 adapter: { claudeCode: { fixedContextWindowTokens: 64_000 } },
89 },
90 synthetic: {
91 ...profile,
92 adapter: { claudeCode: { contextWindowTokens: { large: 1_000_000, small: 32_000 } } },
93 },
94 },
95 models: [
96 ...source.providers!.claudeAgent!.models,
97 {
98 slug: "fixed",
99 name: "Fixed",
100 status: "current",
101 profile: "fixed",
102 },
103 ],
104 },
105 },
106 });
107 const selection = { instanceId: ProviderInstanceId.make("claudeAgent"), model: "synthetic" };
108 assert.equal(resolveClaudeCatalogContextWindowTokens(catalog, selection), 1_000_000);
109 assert.equal(
110 resolveClaudeCatalogContextWindowTokens(catalog, {
111 ...selection,
112 options: [{ id: "contextWindow", value: "small" }],
113 }),
114 32_000,
115 );
116 assert.equal(
117 resolveClaudeCatalogContextWindowTokens(catalog, { ...selection, model: "fixed" }),
118 64_000,
119 );
120 assert.isUndefined(
121 resolveClaudeCatalogContextWindowTokens(catalog, { ...selection, model: "custom" }),
122 );
123 });
124​
125 it("filters models at runtime-version boundaries and derives the upgrade message", () => {
126 const catalog = catalogFromManifest(manifest());
127 assert.deepStrictEqual(resolveClaudeModelsForVersion(catalog, "3.1.9"), []);
128 assert.deepStrictEqual(
129 resolveClaudeModelsForVersion(catalog, "3.2.0").map((model) => model.slug),
130 ["claude-synthetic-next"],
131 );
132 assert.deepStrictEqual(resolveClaudeUpdateRequiredModels(catalog, "3.1.9"), [
133 { slug: "claude-synthetic-next", name: "Claude Synthetic Next", minVersion: "3.2.0" },
134 ]);
135 assert.deepStrictEqual(resolveClaudeUpdateRequiredModels(catalog, "3.2.0"), []);
136 assert.strictEqual(
137 formatClaudeVersionUpgradeMessage(catalog, "3.1.9"),
138 "Claude Code v3.1.9 is too old for Claude Synthetic Next. Upgrade to v3.2.0 or newer to access it.",
139 );
140 });
141​
142 it("resolves aliases and declarative adapter mappings", () => {
143 const base = manifest();
144 const input: ModelManifestData = {
145 ...base,
146 providers: {
147 ...base.providers,
148 claudeAgent: {
149 ...base.providers!.claudeAgent!,
150 models: [
151 {
152 slug: "claude-synthetic-collision",
153 name: "Claude Synthetic Collision",
154 aliases: ["claude-synthetic-next"],
155 status: "current",
156 },
157 ...base.providers!.claudeAgent!.models,
158 ],
159 },
160 },
161 };
162 const catalog = catalogFromManifest(input);
163 assert.strictEqual(resolveClaudeModelSlug(catalog, "synthetic"), "claude-synthetic-next");
164 assert.strictEqual(
165 resolveClaudeModelSlug(catalog, "claude-synthetic-next"),
166 "claude-synthetic-next",
167 );
168 assert.strictEqual(normalizeClaudeCatalogEffort(catalog, "extreme", "synthetic"), "high");
169 assert.strictEqual(
170 resolveClaudeCatalogApiModelId(catalog, {
171 instanceId: ProviderInstanceId.make("claudeAgent"),
172 model: "synthetic",
173 }),
174 "claude-synthetic-next[large]",
175 );
176 });
177​
178 it("appends custom models with their own descriptors and keeps bare slugs opaque", () => {
179 const catalog = scopeClaudeModelCatalog(catalogFromManifest(manifest()), [
180 "synthetic",
181 {
182 slug: "claude-custom-tuned",
183 name: "Tuned",
184 capabilities: {
185 optionDescriptors: [
186 {
187 id: "effort",
188 label: "Reasoning",
189 type: "select",
190 options: [
191 { id: "gentle", label: "Gentle", isDefault: true },
192 { id: "brutal", label: "Brutal" },
193 { id: "ultracode", label: "Ultracode" },
194 ],
195 },
196 ],
197 },
198 },
199 ]);
200​
201 assert.strictEqual(resolveClaudeModelSlug(catalog, "synthetic"), "synthetic");
202 assert.isUndefined(resolveClaudeCatalogEffort(catalog, "synthetic", "extreme"));
203 assert.strictEqual(resolveClaudeCatalogEffort(catalog, "synthetic", "max"), "max");
204 assert.strictEqual(normalizeClaudeCatalogEffort(catalog, "max", "synthetic"), "max");
205​
206 assert.strictEqual(
207 resolveClaudeCatalogEffort(catalog, "claude-custom-tuned", "brutal"),
208 "brutal",
209 );
210 assert.strictEqual(
211 resolveClaudeCatalogEffort(catalog, "claude-custom-tuned", "bogus"),
212 "gentle",
213 );
214 assert.deepStrictEqual(
215 getClaudeCatalogModelCapabilities(catalog, "claude-custom-tuned").optionDescriptors?.map(
216 ({ id, type }) => `${id}:${type}`,
217 ),
218 ["effort:select", "ultracode:boolean"],
219 );
220 assert.strictEqual(
221 normalizeClaudeCatalogEffort(catalog, "brutal", "claude-custom-tuned"),
222 "brutal",
223 );
224 assert.strictEqual(
225 resolveClaudeCatalogApiModelId(catalog, {
226 instanceId: ProviderInstanceId.make("claudeAgent"),
227 model: "claude-custom-tuned",
228 options: [{ id: "effort", value: "brutal" }],
229 }),
230 "claude-custom-tuned",
231 );
232 assert.deepStrictEqual(
233 resolveClaudeModelsForVersion(catalog, "3.2.0").map((model) => model.slug),
234 ["claude-synthetic-next", "claude-custom-tuned"],
235 );
236 });
237​
238 // Rows Claude Code 2.1.310 reports from initialization for each configuration.
239 it("sends the model id Claude Code reports when it serves a catalog model under another id", () => {
240 const apiModel = (
241 catalog: ClaudeModelCatalog,
242 model: string,
243 options?: ModelSelection["options"],
244 ) =>
245 resolveClaudeCatalogApiModelId(catalog, {
246 instanceId: ProviderInstanceId.make("claudeAgent"),
247 model,
248 ...(options ? { options } : {}),
249 });
250​
251 // Bedrock with ANTHROPIC_DEFAULT_*_MODEL and an availableModels allowlist of their ids.
252 const configuredBedrock = withClaudeCodeModelRoutes(BUNDLED_CLAUDE_MODEL_CATALOG, [
253 { value: "default", resolvedModel: "global.anthropic.claude-opus-5-5" },
254 { value: "sonnet", resolvedModel: "global.anthropic.claude-sonnet-5-5[1m]" },
255 { value: "opus", resolvedModel: "global.anthropic.claude-opus-5-5" },
256 { value: "haiku", resolvedModel: "global.anthropic.claude-haiku-5-5" },
257 ]);
258 assert.strictEqual(apiModel(configuredBedrock, "claude-haiku-5-5"), "haiku");
259 assert.strictEqual(apiModel(configuredBedrock, "haiku-5.5"), "haiku");
260 assert.strictEqual(apiModel(configuredBedrock, "claude-sonnet-5-5"), "sonnet");
261 assert.strictEqual(apiModel(configuredBedrock, "claude-opus-5-5"), "opus");
262 assert.strictEqual(apiModel(configuredBedrock, "claude-opus-5"), "claude-opus-5");
263​
264 // Bedrock with no model settings: Claude Code's `haiku` is Haiku 4.5 there.
265 const defaultBedrock = withClaudeCodeModelRoutes(BUNDLED_CLAUDE_MODEL_CATALOG, [
266 {
267 value: "us.anthropic.claude-opus-4-6-v1",
268 resolvedModel: "us.anthropic.claude-opus-4-6-v1",
269 },
270 {
271 value: "us.anthropic.claude-opus-4-6-v1[1m]",
272 resolvedModel: "us.anthropic.claude-opus-4-6-v1[1m]",
273 },
274 { value: "haiku", resolvedModel: "us.anthropic.claude-haiku-4-5-20251001-v1:0" },
275 ]);
276 assert.strictEqual(
277 apiModel(defaultBedrock, "claude-opus-4-6", [{ id: "contextWindow", value: "1m" }]),
278 "us.anthropic.claude-opus-4-6-v1[1m]",
279 );
280 assert.strictEqual(
281 apiModel(defaultBedrock, "claude-opus-4-6", [{ id: "contextWindow", value: "200k" }]),
282 "us.anthropic.claude-opus-4-6-v1",
283 );
284 assert.strictEqual(apiModel(defaultBedrock, "claude-haiku-4-5"), "haiku");
285 assert.strictEqual(apiModel(defaultBedrock, "claude-haiku-5-5"), "claude-haiku-5-5");
286​
287 // First-party ids already are what Claude Code runs, even when an alias points elsewhere.
288 const firstParty = withClaudeCodeModelRoutes(BUNDLED_CLAUDE_MODEL_CATALOG, [
289 { value: "default", resolvedModel: "claude-opus-5-5" },
290 { value: "opus", resolvedModel: "claude-opus-5-5" },
291 { value: "claude-fable-5-1[1m]", resolvedModel: "claude-fable-5-1[1m]" },
292 { value: "haiku", resolvedModel: "claude-haiku-4-5-20251001" },
293 ]);
294 assert.strictEqual(firstParty, BUNDLED_CLAUDE_MODEL_CATALOG);
295 });
296​
297 it("marks effort options that would run above the cap without changing ids or defaults", () => {
298 const effortModel = (slug: string) => ({
299 slug,
300 name: slug,
301 isCustom: false,
302 capabilities: {
303 optionDescriptors: [
304 {
305 id: "effort",
306 label: "Reasoning",
307 type: "select" as const,
308 options: [
309 { id: "medium", label: "Medium" },
310 { id: "high", label: "High", isDefault: true },
311 { id: "xhigh", label: "Extra High" },
312 { id: "max", label: "Max" },
313 { id: "ultracode", label: "Ultracode" },
314 { id: "ultrathink", label: "Ultrathink" },
315 ],
316 },
317 ],
318 },
319 });
320 const models = [effortModel("claude-plain"), effortModel("claude-remapped")];
321 const catalog: ClaudeModelCatalog = {
322 models: [
323 {
324 model: models[0]!,
325 runtime: { effortMap: { ultracode: "xhigh", ultrathink: null } },
326 compatibility: {},
327 },
328 {
329 model: models[1]!,
330 runtime: { effortMap: { max: "high", xhigh: "max", ultrathink: null } },
331 compatibility: {},
332 },
333 ],
334 };
335 const options = (cap: "high" | "xhigh") =>
336 capClaudeCatalogEffort(catalog, models, [{ maxEffortLevel: cap }]).map((model) => {
337 const effort = model.capabilities?.optionDescriptors?.[0];
338 return effort?.type === "select" ? effort.options : undefined;
339 });
340​
341 assert.deepStrictEqual(options("high"), [
342 [
343 { id: "medium", label: "Medium" },
344 { id: "high", label: "High", isDefault: true },
345 { id: "xhigh", label: "Extra High (capped to High)" },
346 { id: "max", label: "Max (capped to High)" },
347 { id: "ultracode", label: "Ultracode (capped to High)" },
348 { id: "ultrathink", label: "Ultrathink" },
349 ],
350 [
351 { id: "medium", label: "Medium" },
352 { id: "high", label: "High", isDefault: true },
353 { id: "xhigh", label: "Extra High (capped to High)" },
354 { id: "max", label: "Max" },
355 { id: "ultracode", label: "Ultracode" },
356 { id: "ultrathink", label: "Ultrathink" },
357 ],
358 ]);
359 // An effortMap that sends Extra High as max stays unmarked under an xhigh cap: the CLI runs
360 // it at xhigh, which is what the label says.
361 assert.deepStrictEqual(
362 options("xhigh").map((model) => model?.map((option) => option.label)),
363 [
364 ["Medium", "High", "Extra High", "Max (capped to Extra High)", "Ultracode", "Ultrathink"],
365 ["Medium", "High", "Extra High", "Max", "Ultracode", "Ultrathink"],
366 ],
367 );
368​
369 // A per-model cap applies without a top-level one and leaves other models uncapped.
370 assert.deepStrictEqual(
371 capClaudeCatalogEffort(catalog, models, [
372 { modelMaxEffortLevels: new Map([["claude-plain", "high"]]) },
373 ]).map((model) => {
374 const effort = model.capabilities?.optionDescriptors?.[0];
375 return effort?.type === "select" ? effort.options.map((option) => option.label) : [];
376 }),
377 [
378 [
379 "Medium",
380 "High",
381 "Extra High (capped to High)",
382 "Max (capped to High)",
383 "Ultracode (capped to High)",
384 "Ultrathink",
385 ],
386 ["Medium", "High", "Extra High", "Max", "Ultracode", "Ultrathink"],
387 ],
388 );
389​
390 // Without an Extra High option (Opus 4.6), the CLI clamps an xhigh cap down to High.
391 const noXhigh = {
392 slug: "claude-no-xhigh",
393 name: "claude-no-xhigh",
394 isCustom: false,
395 capabilities: {
396 optionDescriptors: [
397 {
398 id: "effort",
399 label: "Reasoning",
400 type: "select" as const,
401 options: [
402 { id: "low", label: "Low" },
403 { id: "medium", label: "Medium" },
404 { id: "high", label: "High", isDefault: true },
405 { id: "max", label: "Max" },
406 ],
407 },
408 ],
409 },
410 };
411 const [capped] = capClaudeCatalogEffort(
412 { models: [] },
413 [noXhigh],
414 [{ maxEffortLevel: "xhigh" }],
415 );
416 const cappedEffort = capped?.capabilities?.optionDescriptors?.[0];
417 assert.deepStrictEqual(
418 cappedEffort?.type === "select" ? cappedEffort.options.map((option) => option.label) : [],
419 ["Low", "Medium", "High", "Max (capped to High)"],
420 );
421 });
422​
423 it("leaves custom model effort labels alone under a cap", () => {
424 const custom = {
425 slug: "claude-custom-copy",
426 name: "Copy",
427 isCustom: true,
428 capabilities: {
429 optionDescriptors: [
430 {
431 id: "effort",
432 label: "Reasoning",
433 type: "select" as const,
434 options: [
435 { id: "high", label: "High", isDefault: true },
436 { id: "max", label: "Max (capped to High)" },
437 ],
438 },
439 ],
440 },
441 };
442​
443 assert.deepStrictEqual(
444 capClaudeCatalogEffort({ models: [] }, [custom], [{ maxEffortLevel: "high" }]),
445 [custom],
446 );
447 });
448​
449 it("resolves maxEffortLevel per settings source and per model key like Claude Code", () => {
450 const opus = {
451 slug: "claude-opus-5-5",
452 name: "Opus 5.5",
453 isCustom: false,
454 aliases: ["opus", "opus-5.5", "claude-opus-5.5"],
455 capabilities: {
456 optionDescriptors: [
457 {
458 id: "effort",
459 label: "Reasoning",
460 type: "select" as const,
461 options: [
462 { id: "low", label: "Low" },
463 { id: "medium", label: "Medium" },
464 { id: "high", label: "High", isDefault: true },
465 { id: "max", label: "Max" },
466 ],
467 },
468 ],
469 },
470 };
471 const catalog: ClaudeModelCatalog = {
472 models: [
473 { model: opus, runtime: {}, compatibility: {} },
474 {
475 model: {
476 slug: "claude-opus-5",
477 name: "Opus 5",
478 isCustom: false,
479 capabilities: { optionDescriptors: [] },
480 },
481 runtime: {},
482 compatibility: {},
483 },
484 ],
485 };
486 const maxLabel = (sources: Parameters<typeof capClaudeCatalogEffort>[2]) => {
487 const effort = capClaudeCatalogEffort(catalog, [opus], sources)[0]?.capabilities
488 ?.optionDescriptors?.[0];
489 return effort?.type === "select" ? effort.options.at(-1)?.label : undefined;
490 };
491 const perModel = (entries: Record<string, "low" | "medium" | "high" | "max">) => ({
492 modelMaxEffortLevels: new Map(Object.entries(entries)),
493 });
494​
495 // Claude Code matches family aliases and [1m] spellings case-insensitively.
496 assert.strictEqual(maxLabel([perModel({ Opus: "medium" })]), "Max (capped to Medium)");
497 assert.strictEqual(
498 maxLabel([perModel({ "claude-opus-5-5[1M]": "medium" })]),
499 "Max (capped to Medium)",
500 );
501 assert.strictEqual(maxLabel([perModel({ " OPUS[1m] ": "medium" })]), "Max (capped to Medium)");
502 // Other keys name the most specific model id they contain, so dated and provider-prefixed ids
503 // match while the catalog's dotted aliases and a less specific model id do not.
504 for (const key of [
505 "claude-opus-5-5-20260101",
506 "CLAUDE-OPUS-5-5-20260101[1m]",
507 "us.anthropic.claude-opus-5-5-v1:0",
508 "claude-opus-5-5-latest",
509 ]) {
510 assert.strictEqual(maxLabel([perModel({ [key]: "medium" })]), "Max (capped to Medium)", key);
511 }
512 for (const key of ["claude-opus-5.5", "opus-5.5", "opus-5-5", "opus [1m]", "claude-opus-5"]) {
513 assert.strictEqual(maxLabel([perModel({ [key]: "medium" })]), "Max", key);
514 }
515 // Keys naming the same model keep the lowest cap, and a `max` key does not exempt it.
516 assert.strictEqual(
517 maxLabel([perModel({ opus: "max", "claude-opus-5-5": "low" })]),
518 "Max (capped to Low)",
519 );
520 // A per-model `max` only exempts the model from its own source's top-level cap.
521 assert.strictEqual(
522 maxLabel([{ maxEffortLevel: "medium", ...perModel({ opus: "max" }) }]),
523 "Max",
524 );
525 assert.strictEqual(
526 maxLabel([perModel({ opus: "max" }), { maxEffortLevel: "high" }]),
527 "Max (capped to High)",
528 );
529 assert.strictEqual(
530 maxLabel([{ maxEffortLevel: "medium" }, perModel({ opus: "high" })]),
531 "Max (capped to Medium)",
532 );
533 });
534});