Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

refactor(advisor): move Claude alias resolution into a leaf module

Jack Rudenko2ffa959b83ff
diff --git a/packages/cli/src/handlers/advisor-claude-route.ts b/packages/cli/src/handlers/advisor-claude-route.ts
new file mode 100644
index 00000000..f1b56c84
--- /dev/null
+++ b/packages/cli/src/handlers/advisor-claude-route.ts
@@ -0,0 +1,63 @@
+/**
+ * Where an advisor call to Claude goes, and which Anthropic model id a Claude
+ * name sent there stands for.
+ *
+ * A leaf: it reads the catalog lookups and Claude Code's alias vocabulary
+ * (`claude-code-aliases.ts`) and nothing from the advisor or the proxy, so the
+ * startup planner, the advisor call path and `NativeHandler` can all import it
+ * without a cycle.
+ *
+ * No model id is pinned here. A tier alias (`haiku`) resolves through two live
+ * sources: the catalog's own alias table, then the newest `claude-<tier>-*` id
+ * the catalog lists (the rule `--probe` uses). A concrete `claude-*` id is
+ * passed through as written, because the API may serve a model newer than the
+ * catalog.
+ */
+
+import { latestAnthropicTierModelId } from "../providers/catalog-client.js";
+import { findEntryByAlias } from "../providers/catalog-query.js";
+
+/** The Anthropic API origin. `NativeHandler` forwards to it; the advisor calls it. */
+export const ANTHROPIC_API_ORIGIN = "https://api.anthropic.com";
+
+/** The Messages endpoint every advisor call to Claude is POSTed to. */
+export const ANTHROPIC_MESSAGES_URL = `${ANTHROPIC_API_ORIGIN}/v1/messages`;
+
+/** The host of {@link ANTHROPIC_API_ORIGIN}, for notices and records. */
+export const ANTHROPIC_API_HOST = new URL(ANTHROPIC_API_ORIGIN).host;
+
+/** Why a Claude name cannot be sent to the API as a model id. */
+export type ClaudeAdvisorModelRefusal = "unresolved-alias";
+
+/** The Anthropic model id a Claude name stands for, or why there is none. */
+export type ClaudeAdvisorModel =
+ | { refused: false; id: string }
+ | { refused: true; cause: ClaudeAdvisorModelRefusal; alias: string };
+
+/**
+ * The Claude Code TIER an advisor spec names as a bare alias (`haiku`), or null
+ * for anything already spelled as a model id (`claude-haiku-4-5`). Only an
+ * alias needs resolving; an id is passed through as the user wrote it.
+ */
+function anthropicTierAlias(model: string): "opus" | "sonnet" | "haiku" | null {
+ const m = model.toLowerCase();
+ return m === "opus" || m === "sonnet" || m === "haiku" ? m : null;
+}
+
+/**
+ * The Anthropic model id for a Claude name (the model part of the spec, with
+ * any `anthropic@` already removed).
+ *
+ * The alias itself is never the answer. It used to fall through as `?? model`,
+ * so the default collector POSTed the literal `haiku` to api.anthropic.com,
+ * which rejects it, after the notice had told the user
+ * `haiku -> api.anthropic.com (ANTHROPIC_API_KEY, billed per token)`.
+ */
+export function claudeAdvisorModelFor(model: string): ClaudeAdvisorModel {
+ const tier = anthropicTierAlias(model);
+ if (!tier) return { refused: false, id: model };
+ const resolved = findEntryByAlias(model)?.modelId ?? latestAnthropicTierModelId(tier);
+ return resolved
+ ? { refused: false, id: resolved }
+ : { refused: true, cause: "unresolved-alias", alias: model };
+}
diff --git a/packages/cli/src/handlers/native-handler-advisor.ts b/packages/cli/src/handlers/native-handler-advisor.ts
index 5bad9527..164e1425 100644
--- a/packages/cli/src/handlers/native-handler-advisor.ts
+++ b/packages/cli/src/handlers/native-handler-advisor.ts
@@ -38,12 +38,12 @@ import { appendFileSync } from "node:fs";
import { credentials } from "../auth/credentials/authority.js";
import { getAlwaysOnLogPath, getLogFilePath, log, logStderr } from "../logger.js";
import { type SlimModelEntry, readAllModelsCache } from "../providers/all-models-cache.js";
-import { getCatalogEntries, latestAnthropicTierModelId } from "../providers/catalog-client.js";
-import { findEntryByAlias } from "../providers/catalog-query.js";
+import { getCatalogEntries } from "../providers/catalog-client.js";
import { resolveConnection, resolveRouteProfile } from "../providers/connection-resolver.js";
import { type KnownWire, type WireApi, wireRequestUrl } from "../providers/connection-wire.js";
import { parseModelSpec } from "../providers/model-parser.js";
import { mergeWireHeaders, shapeRequestFromWire } from "../providers/wire-request.js";
+import { claudeAdvisorModelFor } from "./advisor-claude-route.js";
import { extractProviderMessage, extractUpstreamStatus } from "./shared/anthropic-error.js";
const ADVISOR_SERVER_TOOL_TYPE = "advisor_20260301";
@@ -1602,18 +1602,13 @@ export function advisorRouteFor(modelSpec: string, role: "panel" | "collector"):
const parsed = parseModelSpec(modelSpec);
if (role === "collector" && isAnthropicModel(parsed)) {
- const model = parsed.model;
- const tier = anthropicTierAlias(model);
- if (!tier) return routeOf("anthropic", model);
- // Two LIVE sources, no pinned id: the catalog's own alias table, then the
- // "newest `claude-<tier>-*` the catalog lists" rule `--probe` already uses.
- // The alias itself is never the answer — it used to fall through as
- // `?? model`, so the default collector POSTed the literal `haiku` to
- // api.anthropic.com, which rejects it, after the notice had told the user
- // `haiku -> api.anthropic.com (ANTHROPIC_API_KEY, billed per token)`.
- const resolved = findEntryByAlias(model)?.modelId ?? latestAnthropicTierModelId(tier);
- if (!resolved) return { ...routeOf("anthropic", model), unresolvedAlias: model };
- return routeOf("anthropic", resolved);
+ // The Anthropic id comes from `claudeAdvisorModelFor` (the leaf shared with
+ // the startup planner); an alias the catalog cannot resolve is marked, never
+ // POSTed.
+ const claude = claudeAdvisorModelFor(parsed.model);
+ if (claude.refused)
+ return { ...routeOf("anthropic", claude.alias), unresolvedAlias: claude.alias };
+ return routeOf("anthropic", claude.id);
}
const provider = parsed.provider;
@@ -2319,17 +2314,6 @@ async function callAdvisorModel(
return executeAdvisorFetch(plan, fetchImpl);
}
-/**
- * The Claude Code TIER an advisor spec names as a bare alias (`haiku`), or null
- * for anything already spelled as a model id (`claude-haiku-4-5`). Only an
- * alias needs resolving; an id is passed through as the user wrote it, because
- * the API may serve a model newer than the catalog.
- */
-function anthropicTierAlias(model: string): "opus" | "sonnet" | "haiku" | null {
- const m = model.toLowerCase();
- return m === "opus" || m === "sonnet" || m === "haiku" ? m : null;
-}
-
function isAnthropicModel(parsed: ReturnType<typeof parseModelSpec>): boolean {
const m = parsed.model.toLowerCase();
return (
diff --git a/packages/cli/src/handlers/native-handler.ts b/packages/cli/src/handlers/native-handler.ts
index 88af45aa..94017672 100644
--- a/packages/cli/src/handlers/native-handler.ts
+++ b/packages/cli/src/handlers/native-handler.ts
@@ -1,6 +1,7 @@
import type { Context } from "hono";
import { credentials } from "../auth/credentials/authority.js";
import { log, maskCredential } from "../logger.js";
+import { ANTHROPIC_API_ORIGIN } from "./advisor-claude-route.js";
import { ADVISOR_SWAPPED_CONTEXT_KEY } from "./advisor-decorator.js";
import {
loadAdvisorSwapConfig,
@@ -19,8 +20,8 @@ export class NativeHandler implements ModelHandler {
constructor(apiKey?: string, advisorModels?: string[], advisorCollector?: string | null) {
this.apiKey = apiKey;
- // Always forward to real Anthropic API
- this.baseUrl = "https://api.anthropic.com";
+ // Always forward to real Anthropic API (the origin the advisor's Claude call uses too)
+ this.baseUrl = ANTHROPIC_API_ORIGIN;
this.advisorModels = advisorModels;
this.advisorCollector = advisorCollector;
}