Skip to content

Commit 5922408

Browse files
fix(server): custom Claude models named like a Claude Code alias reach the API unchanged (pingdotgg#34938)
1 parent f423a4d commit 5922408

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import { ClaudeExecutableFileCheck } from "../../provider/Drivers/ClaudeExecutab
9999
import {
100100
BUNDLED_CLAUDE_MODEL_CATALOG,
101101
type ClaudeModelCatalog,
102+
scopeClaudeModelCatalog,
102103
} from "../../provider/ClaudeModelCatalog.ts";
103104
import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers";
104105
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
@@ -394,6 +395,30 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
394395
});
395396
});
396397

398+
it("sends a custom model named like a Claude Code alias to the API unchanged", () => {
399+
const options = ClaudeAdapterV2.makeClaudeQueryOptions({
400+
modelSelection: { instanceId: CLAUDE_TEST_MODEL_SELECTION.instanceId, model: "opus" },
401+
modelCatalog: scopeClaudeModelCatalog(
402+
BUNDLED_CLAUDE_MODEL_CATALOG,
403+
CUSTOM_MODEL_CLAUDE_SETTINGS.customModels,
404+
),
405+
nativeThreadId: "native-custom-alias",
406+
resume: false,
407+
cwd: "/workspace",
408+
settings: CUSTOM_MODEL_CLAUDE_SETTINGS,
409+
});
410+
// Claude Code expands a bare alias to its own model unless ANTHROPIC_DEFAULT_<ALIAS>_MODEL overrides it.
411+
const settingsEnv =
412+
typeof options.settings === "object" && options.settings !== null
413+
? (options.settings as { env?: Record<string, string> }).env
414+
: undefined;
415+
const aliasOverride =
416+
settingsEnv?.ANTHROPIC_DEFAULT_OPUS_MODEL ?? options.env?.ANTHROPIC_DEFAULT_OPUS_MODEL;
417+
const requestedModel =
418+
options.model === "opus" ? (aliasOverride ?? "claude-opus-5-5") : options.model;
419+
assert.equal(requestedModel, "opus");
420+
});
421+
397422
it("projects AskUserQuestion input with question text as the answer key", () => {
398423
const preview = "\n **Proposed change**\n\n```ts\n\tconst enabled = true;\n```\n ";
399424
assert.deepEqual(

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,8 @@ const subagentDeviceInstructions: HookCallback = () =>
19391939
},
19401940
});
19411941

1942+
const CLAUDE_CODE_MODEL_ALIASES = new Set(["opus", "sonnet", "haiku", "fable"]);
1943+
19421944
export function makeClaudeQueryOptions(input: {
19431945
readonly modelSelection: ModelSelection;
19441946
readonly modelCatalog?: ClaudeModelCatalog;
@@ -2014,13 +2016,28 @@ export function makeClaudeQueryOptions(input: {
20142016
...(typeof querySettings === "object" && querySettings !== null ? querySettings : {}),
20152017
autoCompactWindow: Number(input.settings.autoCompactWindow),
20162018
} as ClaudeSdkSettings);
2019+
// Claude Code expands a bare alias to its own model, so a custom model with an alias's name
2020+
// pins the alias to itself.
2021+
const aliasOverrideEnv = CLAUDE_CODE_MODEL_ALIASES.has(compiledSelection.apiModelId)
2022+
? {
2023+
[`ANTHROPIC_DEFAULT_${compiledSelection.apiModelId.toUpperCase()}_MODEL`]:
2024+
compiledSelection.apiModelId,
2025+
}
2026+
: undefined;
20172027
// A settings file path cannot take extra keys, so it goes through unchanged.
20182028
const effectiveQuerySettings =
2019-
input.toolSearchOverride === undefined || typeof compactQuerySettings === "string"
2029+
(input.toolSearchOverride === undefined && aliasOverrideEnv === undefined) ||
2030+
typeof compactQuerySettings === "string"
20202031
? compactQuerySettings
20212032
: ({
20222033
...compactQuerySettings,
2023-
env: { ...compactQuerySettings?.env, ENABLE_TOOL_SEARCH: input.toolSearchOverride },
2034+
env: {
2035+
...compactQuerySettings?.env,
2036+
...aliasOverrideEnv,
2037+
...(input.toolSearchOverride === undefined
2038+
? {}
2039+
: { ENABLE_TOOL_SEARCH: input.toolSearchOverride }),
2040+
},
20242041
} as ClaudeSdkSettings);
20252042
const deviceInstructions = input.mcpServers !== undefined && input.deviceToolsAvailable === true;
20262043
const options: ClaudeAgentSdkQueryOptions = {

0 commit comments

Comments
 (0)