Skip to content

Commit a3de446

Browse files
committed
feat(server): custom Claude models can pick reasoning, thinking, and 1M
A custom Claude model added by slug showed no composer controls, so a gateway route had no way to pick an effort, turn thinking off, or open 1M. The adapter also compiled turns against the bundled catalog, so options a custom model did declare never reached Claude Code. Bare custom entries now get Reasoning, Thinking, and a 200k/1M Context Window, and 1M maps to the `[1m]` suffix the built-ins already use. Turns compile against the instance's scoped catalog. The Settings editor no longer strips Context Window from Claude presets and copies.
1 parent 67c0fc0 commit a3de446

11 files changed

Lines changed: 315 additions & 99 deletions

File tree

‎apps/server/src/claudeModelOptions.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { describe, expect, it } from "@effect/vitest";
33
import { ProviderInstanceId, type ModelSelection } from "@t3tools/contracts";
44

55
import { compileClaudeModelSelection } from "./claudeModelOptions.ts";
6+
import {
7+
BUNDLED_CLAUDE_MODEL_CATALOG,
8+
scopeClaudeModelCatalog,
9+
} from "./provider/ClaudeModelCatalog.ts";
610

711
const selection = (
812
model: string,
@@ -74,4 +78,22 @@ describe("compileClaudeModelSelection", () => {
7478
.settings,
7579
).toEqual({ alwaysThinkingEnabled: false });
7680
});
81+
82+
it("compiles 1M, effort, and thinking for a bare custom model", () => {
83+
const catalog = scopeClaudeModelCatalog(BUNDLED_CLAUDE_MODEL_CATALOG, ["glm-flash"]);
84+
expect(
85+
compileClaudeModelSelection(
86+
selection("glm-flash", [
87+
{ id: "contextWindow", value: "1m" },
88+
{ id: "effort", value: "max" },
89+
{ id: "thinking", value: false },
90+
]),
91+
catalog,
92+
),
93+
).toMatchObject({
94+
apiModelId: "glm-flash[1m]",
95+
effort: "max",
96+
settings: { alwaysThinkingEnabled: false },
97+
});
98+
});
7799
});

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ import { WorktreeToolkit } from "../../mcp/toolkits/worktree/tools.ts";
8888
import { ThreadToolkit } from "../../mcp/toolkits/thread/tools.ts";
8989
import { OrchestratorToolkit } from "../../mcp/toolkits/orchestrator/tools.ts";
9090
import { ClaudeExecutableFileCheck } from "../../provider/Drivers/ClaudeExecutable.ts";
91+
import {
92+
BUNDLED_CLAUDE_MODEL_CATALOG,
93+
scopeClaudeModelCatalog,
94+
} from "../../provider/ClaudeModelCatalog.ts";
9195
import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
9296
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
9397
import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
@@ -126,6 +130,9 @@ const DEFAULT_CLAUDE_SETTINGS = Schema.decodeSync(ClaudeSettings)({});
126130
const AUTO_COMPACT_CLAUDE_SETTINGS = Schema.decodeSync(ClaudeSettings)({
127131
autoCompactWindow: "300000",
128132
});
133+
const BARE_CUSTOM_MODEL_CLAUDE_SETTINGS = Schema.decodeSync(ClaudeSettings)({
134+
customModels: [{ slug: "gateway-bare", name: "Gateway Bare" }],
135+
});
129136
const CUSTOM_MODEL_CLAUDE_SETTINGS = Schema.decodeSync(ClaudeSettings)({
130137
customModels: [
131138
"opus",
@@ -1026,6 +1033,38 @@ describe("ClaudeAdapterV2 context usage", () => {
10261033
);
10271034
assert.strictEqual(usage.promptCacheExpiresAt, "2026-08-29T00:05:00.000Z");
10281035
});
1036+
1037+
it("defaults a bare custom slug and opens 1M through the turn path", () => {
1038+
const settings = BARE_CUSTOM_MODEL_CLAUDE_SETTINGS;
1039+
const bareSelection = {
1040+
instanceId: ProviderInstanceId.make(ClaudeAdapterV2.CLAUDE_PROVIDER),
1041+
model: "gateway-bare",
1042+
options: [
1043+
{ id: "effort", value: "max" },
1044+
{ id: "contextWindow", value: "1m" },
1045+
],
1046+
} satisfies ModelSelection;
1047+
const options = ClaudeAdapterV2.makeClaudeQueryOptions({
1048+
modelSelection: bareSelection,
1049+
nativeThreadId: "bare-model-thread",
1050+
resume: false,
1051+
cwd: "/workspace",
1052+
settings,
1053+
});
1054+
assert.equal(options.model, "gateway-bare[1m]");
1055+
assert.equal(options.effort, "max");
1056+
assert.equal(
1057+
ClaudeAdapterV2.claudeProviderTurnTokenUsage(
1058+
{ input_tokens: 1, output_tokens: 1 },
1059+
bareSelection,
1060+
"2026-08-29T00:00:00.000Z",
1061+
"",
1062+
undefined,
1063+
scopeClaudeModelCatalog(BUNDLED_CLAUDE_MODEL_CATALOG, settings.customModels),
1064+
).maxTokens,
1065+
1_000_000,
1066+
);
1067+
});
10291068
});
10301069

10311070
describe("ClaudeAdapterV2 session permissions", () => {
@@ -12630,7 +12669,11 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1263012669
model: "claude-custom-tuned",
1263112670
options: [{ id: "effort", value: "brutal" }],
1263212671
},
12633-
[`task-${SHADOW_TOOL_USE_ID}`]: { instanceId, model: "opus" },
12672+
[`task-${SHADOW_TOOL_USE_ID}`]: {
12673+
instanceId,
12674+
model: "opus",
12675+
options: [{ id: "effort", value: "high" }],
12676+
},
1263412677
});
1263512678
}).pipe(
1263612679
Effect.provide(

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,11 @@ import {
191191
export const CLAUDE_PROVIDER = ProviderDriverKind.make("claudeAgent");
192192
export const CLAUDE_AGENT_SDK_QUERY_PROTOCOL = "claude-agent-sdk.query" as const;
193193

194-
function claudeContextWindow(modelSelection: ModelSelection): number | null {
195-
return (
196-
resolveClaudeCatalogContextWindowTokens(BUNDLED_CLAUDE_MODEL_CATALOG, modelSelection) ?? null
197-
);
194+
function claudeContextWindow(
195+
catalog: ClaudeModelCatalog,
196+
modelSelection: ModelSelection,
197+
): number | null {
198+
return resolveClaudeCatalogContextWindowTokens(catalog, modelSelection) ?? null;
198199
}
199200

200201
interface ClaudeCacheCreation {
@@ -243,11 +244,12 @@ export function claudeProviderTurnTokenUsage(
243244
updatedAt: string,
244245
autoCompactWindow = "",
245246
requestedAt?: DateTime.Utc,
247+
catalog: ClaudeModelCatalog = BUNDLED_CLAUDE_MODEL_CATALOG,
246248
) {
247249
const inputTokens = claudeMessageInputTokens(usage);
248250
const outputTokens = usage.output_tokens;
249251
const promptCacheTtlMs = claudePromptCacheTtlMs(usage.cache_creation);
250-
const maxTokens = claudeContextWindow(modelSelection);
252+
const maxTokens = claudeContextWindow(catalog, modelSelection);
251253
// The cache lifetime starts with the request, not when its response ends.
252254
const promptCacheExpiresAt =
253255
promptCacheTtlMs === undefined || requestedAt === undefined
@@ -1261,7 +1263,10 @@ export function makeClaudeQueryOptions(input: {
12611263
readonly allowDangerouslySkipPermissions?: boolean;
12621264
readonly appendSystemPromptFileText?: string;
12631265
}): ClaudeAgentSdkQueryOptions {
1264-
const compiledSelection = compileClaudeModelSelection(input.modelSelection);
1266+
const compiledSelection = compileClaudeModelSelection(
1267+
input.modelSelection,
1268+
scopeClaudeModelCatalog(BUNDLED_CLAUDE_MODEL_CATALOG, input.settings?.customModels ?? []),
1269+
);
12651270
const {
12661271
"permission-mode": launchArgPermissionMode,
12671272
"dangerously-skip-permissions": launchArgSkipPermissions,
@@ -4215,6 +4220,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
42154220
DateTime.formatIso(now),
42164221
adapterOptions.settings.autoCompactWindow,
42174222
context.streamUsage?.requestedAt,
4223+
subagentModelCatalog,
42184224
);
42194225
if (tokenUsage.inputTokens <= 0) return;
42204226
yield* emitProviderEvent({
@@ -5534,7 +5540,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
55345540
existingSubagent === undefined
55355541
? (input.effort ??
55365542
input.owner?.effort ??
5537-
compileClaudeModelSelection(input.context.input.modelSelection).effort)
5543+
compileClaudeModelSelection(
5544+
input.context.input.modelSelection,
5545+
subagentModelCatalog,
5546+
).effort)
55385547
: existingSubagent.effort;
55395548
const model = input.model ?? priorTask?.model ?? null;
55405549
const task = {
@@ -7691,7 +7700,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
76917700
? Math.round(postTokens)
76927701
: undefined;
76937702
if (afterTokenCount !== undefined) {
7694-
const maxTokens = claudeContextWindow(context.input.modelSelection);
7703+
const maxTokens = claudeContextWindow(
7704+
subagentModelCatalog,
7705+
context.input.modelSelection,
7706+
);
76957707
yield* emitProviderEvent({
76967708
type: "provider_turn.updated",
76977709
driver: CLAUDE_PROVIDER,
@@ -9043,7 +9055,10 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90439055
: { allowedTools: queryPolicy.allowedTools }),
90449056
});
90459057
const queryPolicyKey = claudeEffectiveQueryPolicyKey(queryPolicy, mcpOverrides);
9046-
const compiledSelection = compileClaudeModelSelection(turnInput.modelSelection);
9058+
const compiledSelection = compileClaudeModelSelection(
9059+
turnInput.modelSelection,
9060+
subagentModelCatalog,
9061+
);
90479062
const resumeSessionAt = yield* getNativeConversationHeadId(turnInput.providerThread);
90489063
const existing = yield* Ref.get(queryContext);
90499064
// A continuation prompts nothing: it drains output the live process
@@ -9331,7 +9346,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93319346
: yield* makeClaudeUserMessageWithAttachments({
93329347
text: applyClaudePromptEffortPrefix(
93339348
turnInput.message.text,
9334-
compileClaudeModelSelection(turnInput.modelSelection).promptEffort,
9349+
compileClaudeModelSelection(turnInput.modelSelection, subagentModelCatalog)
9350+
.promptEffort,
93359351
),
93369352
attachments: turnInput.message.attachments,
93379353
attachmentsDir,
@@ -9761,7 +9777,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
97619777
const userMessage = yield* makeClaudeUserMessageWithAttachments({
97629778
text: applyClaudePromptEffortPrefix(
97639779
turnInput.message.text,
9764-
compileClaudeModelSelection(currentTurn.input.modelSelection).promptEffort,
9780+
compileClaudeModelSelection(currentTurn.input.modelSelection, subagentModelCatalog)
9781+
.promptEffort,
97659782
),
97669783
attachments: turnInput.message.attachments,
97679784
priority: "now",

‎apps/server/src/provider/ClaudeModelCatalog.test.ts‎

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,29 @@ describe("Claude model catalog", () => {
116116
assert.isUndefined(
117117
resolveClaudeCatalogContextWindowTokens(catalog, { ...selection, model: "custom" }),
118118
);
119+
// A route the user marked `[1m]` by hand runs at 1M, so it reports 1M even
120+
// with no Context Window control of its own.
121+
assert.equal(
122+
resolveClaudeCatalogContextWindowTokens(
123+
scopeClaudeModelCatalog(catalog, [
124+
{
125+
slug: "route/pro[1m]",
126+
capabilities: {
127+
optionDescriptors: [
128+
{
129+
id: "effort",
130+
label: "Reasoning",
131+
type: "select",
132+
options: [{ id: "high", label: "High", isDefault: true }],
133+
},
134+
],
135+
},
136+
},
137+
]),
138+
{ ...selection, model: "route/pro[1m]" },
139+
),
140+
1_000_000,
141+
);
119142
});
120143

121144
it("filters models at runtime-version boundaries and derives the upgrade message", () => {
@@ -202,7 +225,7 @@ describe("Claude model catalog", () => {
202225
}
203226
});
204227

205-
it("appends custom models with their own descriptors and keeps bare slugs opaque", () => {
228+
it("appends custom models, defaulting a bare slug and keeping declared descriptors", () => {
206229
const catalog = scopeClaudeModelCatalog(catalogFromManifest(manifest()), [
207230
"synthetic",
208231
{
@@ -225,9 +248,65 @@ describe("Claude model catalog", () => {
225248
]);
226249

227250
assert.strictEqual(resolveClaudeModelSlug(catalog, "synthetic"), "synthetic");
228-
assert.isUndefined(resolveClaudeCatalogEffort(catalog, "synthetic", "extreme"));
229-
assert.strictEqual(resolveClaudeCatalogEffort(catalog, "synthetic", "max"), "max");
230-
assert.strictEqual(normalizeClaudeCatalogEffort(catalog, "max", "synthetic"), "max");
251+
252+
// With no descriptors of its own it gets the default Reasoning and Context
253+
// Window controls, and 1M appends the suffix Claude Code needs to open 1M.
254+
assert.strictEqual(resolveClaudeCatalogEffort(catalog, "synthetic", "extreme"), "high");
255+
assert.strictEqual(
256+
resolveClaudeCatalogApiModelId(catalog, {
257+
instanceId: ProviderInstanceId.make("claudeAgent"),
258+
model: "synthetic",
259+
options: [{ id: "contextWindow", value: "1m" }],
260+
}),
261+
"synthetic[1m]",
262+
);
263+
assert.strictEqual(
264+
resolveClaudeCatalogContextWindowTokens(catalog, {
265+
instanceId: ProviderInstanceId.make("claudeAgent"),
266+
model: "synthetic",
267+
options: [{ id: "contextWindow", value: "1m" }],
268+
}),
269+
1_000_000,
270+
);
271+
272+
// A slug a user already marked `[1m]` by hand keeps the marker once.
273+
assert.strictEqual(
274+
resolveClaudeCatalogApiModelId(scopeClaudeModelCatalog(catalog, ["route/deepseek[1m]"]), {
275+
instanceId: ProviderInstanceId.make("claudeAgent"),
276+
model: "route/deepseek[1m]",
277+
options: [{ id: "contextWindow", value: "1m" }],
278+
}),
279+
"route/deepseek[1m]",
280+
);
281+
282+
// A custom entry that declares its own Context Window still gets the suffix.
283+
const declared = scopeClaudeModelCatalog(catalog, [
284+
{
285+
slug: "route/glm",
286+
name: "GLM",
287+
capabilities: {
288+
optionDescriptors: [
289+
{
290+
id: "contextWindow",
291+
label: "Context Window",
292+
type: "select",
293+
options: [
294+
{ id: "200k", label: "200k", isDefault: true },
295+
{ id: "1m", label: "1M" },
296+
],
297+
},
298+
],
299+
},
300+
},
301+
]);
302+
assert.strictEqual(
303+
resolveClaudeCatalogApiModelId(declared, {
304+
instanceId: ProviderInstanceId.make("claudeAgent"),
305+
model: "route/glm",
306+
options: [{ id: "contextWindow", value: "1m" }],
307+
}),
308+
"route/glm[1m]",
309+
);
231310

232311
assert.strictEqual(
233312
resolveClaudeCatalogEffort(catalog, "claude-custom-tuned", "brutal"),
@@ -245,13 +324,16 @@ describe("Claude model catalog", () => {
245324
resolveClaudeCatalogApiModelId(catalog, {
246325
instanceId: ProviderInstanceId.make("claudeAgent"),
247326
model: "claude-custom-tuned",
248-
options: [{ id: "effort", value: "brutal" }],
327+
options: [
328+
{ id: "effort", value: "brutal" },
329+
{ id: "contextWindow", value: "1m" },
330+
],
249331
}),
250332
"claude-custom-tuned",
251333
);
252334
assert.deepStrictEqual(
253335
resolveClaudeModelsForVersion(catalog, "3.2.0").map((model) => model.slug),
254-
["claude-synthetic-next", "claude-custom-tuned"],
336+
["claude-synthetic-next", "synthetic", "claude-custom-tuned"],
255337
);
256338
});
257339
});

0 commit comments

Comments
 (0)