diff --git a/apps/server/src/provider/Drivers/ClaudeDriver.ts b/apps/server/src/provider/Drivers/ClaudeDriver.ts index 5bedffe9242b..9712b55bcd46 100644 --- a/apps/server/src/provider/Drivers/ClaudeDriver.ts +++ b/apps/server/src/provider/Drivers/ClaudeDriver.ts @@ -37,8 +37,10 @@ import { type ClaudeCapabilitiesProbe, } from "../ClaudeProvider.ts"; import { makeManagedServerProvider } from "@t3tools/provider-core/server/managedProvider"; +import type { ServerProviderDraft } from "@t3tools/provider-core/server/snapshotProbe"; import * as ModelCatalog from "@t3tools/provider-core/server/ModelCatalog"; import { resolveClaudeModelCatalog } from "../ClaudeModelCatalog.ts"; +import { fetchMagpieGatewayCatalog, withMagpieGatewayModels } from "../MagpieGatewayModels.ts"; import { makeClaudeCodeModelRoutes, resolveClaudeModelSettingsKey, @@ -61,6 +63,7 @@ import { mergeClaudeProcessEnvironment, resolveClaudeHomePath, } from "./ClaudeHome.ts"; +import { customClaudeApiEndpointHost, readClaudeSettingsEnv } from "./ClaudeSkills.ts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { makeClaudeMaintenanceResolution } from "./ClaudeMaintenance.ts"; const decodeClaudeSettings = Schema.decodeSync(ClaudeSettings); @@ -270,6 +273,26 @@ export const ClaudeDriver: ProviderDriver result.capabilities), ); + // Claude Code's own gateway discovery keeps only Claude names, so T3 reads a Magpie catalog + // itself. Anything else, or no answer, leaves the built-in list. + const withGatewayModels = Effect.fn("ClaudeDriver.withGatewayModels")(function* ( + provider: ServerProviderDraft, + ) { + if (!provider.enabled) return provider; + const baseUrl = + (yield* readClaudeSettingsEnv(effectiveConfig, stateDir, processEnv)).ANTHROPIC_BASE_URL ?? + processEnv.ANTHROPIC_BASE_URL; + if (baseUrl === undefined || customClaudeApiEndpointHost(baseUrl) === undefined) { + return provider; + } + const gateway = yield* fetchMagpieGatewayCatalog(baseUrl, processEnv).pipe( + Effect.provideService(HttpClient.HttpClient, httpClient), + ); + return gateway === undefined + ? provider + : { ...provider, models: withMagpieGatewayModels(provider.models, gateway) }; + }); + const checkProvider = catalogService.refreshInBackground.pipe( Effect.andThen( modelCatalog.pipe( @@ -289,6 +312,7 @@ export const ClaudeDriver: ProviderDriver provider.auth.status === "unauthenticated" ? Cache.invalidateAll(capabilitiesProbeCache) diff --git a/apps/server/src/provider/MagpieGatewayModels.test.ts b/apps/server/src/provider/MagpieGatewayModels.test.ts new file mode 100644 index 000000000000..fecb8522667a --- /dev/null +++ b/apps/server/src/provider/MagpieGatewayModels.test.ts @@ -0,0 +1,138 @@ +import { assert, describe, it } from "@effect/vitest"; +import type { ServerProviderModel } from "@t3tools/contracts"; + +import { parseMagpieGatewayCatalog, withMagpieGatewayModels } from "./MagpieGatewayModels.ts"; + +const efforts = (...levels: ReadonlyArray) => levels.map((effort) => ({ effort })); + +// Trimmed from a live Magpie `/v1/models` response. +const MAGPIE_CATALOG = { + object: "list", + data: [ + { + id: "group/auto-gpt-5-6-luna", + display_name: "GPT-5.6 Luna", + magpie_label: "GPT-5.6 Luna · routing group", + owned_by: "magpie-go", + supported_reasoning_levels: efforts("low", "high"), + }, + { + id: "magpie-go/gpt-5.6-luna", + display_name: "GPT-5.6 Luna", + magpie_label: "GPT-5.6 Luna · Magpie Go", + owned_by: "magpie-go", + supported_reasoning_levels: efforts("none", "low", "max"), + }, + { + id: "magpie-go/mimo-v2.6-pro", + display_name: "MiMo-V2.6-Pro", + magpie_label: "MiMo-V2.6-Pro · Magpie Go", + owned_by: "magpie-go", + supported_reasoning_levels: [], + }, + { + id: "claude/claude-opus-5-5", + display_name: "Claude Opus 5.5", + magpie_label: "Claude Opus 5.5 · Claude Code", + owned_by: "claude", + }, + { + id: "claude/claude-haiku-4-5-20251001", + display_name: "Claude Haiku 4.5", + magpie_label: "Claude Haiku 4.5 · Claude Code", + owned_by: "claude", + }, + { + id: "claude/claude-sonnet-4-5", + display_name: "Claude Sonnet 4.5 (latest)", + magpie_label: "Claude Sonnet 4.5 (latest) · Claude Code", + owned_by: "claude", + supported_reasoning_levels: efforts("low", "high"), + }, + { + id: "claude/claude-sonnet-4-5-20250929", + display_name: "Claude Sonnet 4.5", + magpie_label: "Claude Sonnet 4.5 · Claude Code", + owned_by: "claude", + }, + { + id: "codex/gpt-6-luna", + display_name: "GPT-6-Luna", + magpie_label: "GPT-6-Luna · Codex", + owned_by: "codex", + supported_reasoning_levels: efforts("medium"), + }, + ], +}; + +const builtIn = (slug: string, aliases: ReadonlyArray = []): ServerProviderModel => ({ + slug, + name: slug, + aliases: [...aliases], + isCustom: false, + capabilities: { + optionDescriptors: [{ id: "fastMode", label: "Fast Mode", type: "boolean" }], + }, +}); + +describe("parseMagpieGatewayCatalog", () => { + it("recognises Magpie only by its labelled entries", () => { + assert.strictEqual(parseMagpieGatewayCatalog(MAGPIE_CATALOG)?.length, 8); + // A plain Anthropic-compatible gateway lists models without Magpie's labels. + assert.strictEqual( + parseMagpieGatewayCatalog({ data: [{ id: "claude-opus-5-5", display_name: "Opus" }] }), + undefined, + ); + assert.strictEqual(parseMagpieGatewayCatalog({ error: "nope" }), undefined); + }); +}); + +describe("withMagpieGatewayModels", () => { + const gateway = parseMagpieGatewayCatalog(MAGPIE_CATALOG) ?? []; + const custom: ServerProviderModel = { + slug: "codex/gpt-6-luna", + name: "My Luna", + isCustom: true, + capabilities: null, + }; + const models = withMagpieGatewayModels( + [ + builtIn("claude-opus-5-5", ["opus"]), + builtIn("claude-haiku-4-5", ["claude-haiku-4-5-20251001"]), + builtIn("claude-fable-5-2"), + custom, + ], + gateway, + ); + + it("keeps served built-in Claude models first, then groups the rest by route", () => { + assert.deepStrictEqual( + models.map((model) => [model.slug, model.subProvider ?? null]), + [ + ["claude-opus-5-5", null], + ["claude-haiku-4-5", null], + // One entry per Claude model: the dated duplicate is dropped. + ["claude/claude-sonnet-4-5", "Claude Code"], + ["magpie-go/gpt-5.6-luna", "Magpie Go · 200K context"], + ["magpie-go/mimo-v2.6-pro", "Magpie Go · 200K context"], + ["group/auto-gpt-5-6-luna", "Routing group · 200K context"], + // A custom model of the same slug wins over the gateway entry. + ["codex/gpt-6-luna", null], + ], + ); + // The gateway does not serve Fable 5.2, so it is not offered. + assert.isFalse(models.some((model) => model.slug === "claude-fable-5-2")); + assert.strictEqual(models[0]?.capabilities?.optionDescriptors?.[0]?.id, "fastMode"); + }); + + it("offers only the effort levels Claude Code can send", () => { + const effortChoices = (slug: string) => { + const descriptor = models + .find((model) => model.slug === slug) + ?.capabilities?.optionDescriptors?.find((option) => option.id === "effort"); + return descriptor?.type === "select" ? descriptor.options.map((option) => option.id) : []; + }; + assert.deepStrictEqual(effortChoices("magpie-go/gpt-5.6-luna"), ["default", "low", "max"]); + assert.deepStrictEqual(effortChoices("magpie-go/mimo-v2.6-pro"), []); + }); +}); diff --git a/apps/server/src/provider/MagpieGatewayModels.ts b/apps/server/src/provider/MagpieGatewayModels.ts new file mode 100644 index 000000000000..9664ce400b0e --- /dev/null +++ b/apps/server/src/provider/MagpieGatewayModels.ts @@ -0,0 +1,164 @@ +import type { ServerProviderModel } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Option from "effect/Option"; +import * as Schema from "effect/Schema"; +import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"; + +import { CLAUDE_EFFORT_LEVELS } from "./ClaudeModelCatalog.ts"; + +// `magpie_label` is what marks a Magpie catalog; plain Anthropic-compatible gateways omit it. +const MagpieGatewayModel = Schema.Struct({ + id: Schema.String, + display_name: Schema.optional(Schema.String), + owned_by: Schema.optional(Schema.String), + magpie_label: Schema.String, + supported_reasoning_levels: Schema.optional( + Schema.Array(Schema.Struct({ effort: Schema.String })), + ), +}); +export type MagpieGatewayModel = typeof MagpieGatewayModel.Type; + +const decodeCatalog = Schema.decodeUnknownOption( + Schema.Struct({ data: Schema.Array(Schema.Unknown) }), +); +const decodeModel = Schema.decodeUnknownOption(MagpieGatewayModel); + +/** The models a Magpie gateway serves, or undefined when the endpoint is not Magpie. */ +export function parseMagpieGatewayCatalog( + json: unknown, +): ReadonlyArray | undefined { + const catalog = Option.getOrUndefined(decodeCatalog(json)); + const models = (catalog?.data ?? []).flatMap((entry) => + Option.toArray(decodeModel(entry)).filter((model) => model.id.trim().length > 0), + ); + return models.length > 0 ? models : undefined; +} + +/** Reads the catalog the way Claude Code's gateway discovery does; any failure means "not Magpie". */ +export const fetchMagpieGatewayCatalog = Effect.fn("MagpieGatewayModels.fetchCatalog")(function* ( + baseUrl: string, + environment: NodeJS.ProcessEnv, +) { + const httpClient = yield* HttpClient.HttpClient; + const token = environment.ANTHROPIC_AUTH_TOKEN?.trim(); + const apiKey = environment.ANTHROPIC_API_KEY?.trim(); + return yield* httpClient + .execute( + HttpClientRequest.get(`${baseUrl.trim().replace(/\/+$/, "")}/v1/models?limit=1000`).pipe( + HttpClientRequest.setHeaders({ + "anthropic-version": "2023-06-01", + ...(token ? { authorization: `Bearer ${token}` } : {}), + ...(apiKey ? { "x-api-key": apiKey } : {}), + }), + ), + ) + .pipe( + // Like Claude Code's own discovery: a redirect must not carry the key to another host. + Effect.provideService(FetchHttpClient.RequestInit, { redirect: "error" }), + Effect.flatMap(HttpClientResponse.filterStatusOk), + Effect.flatMap((response) => response.json), + Effect.map(parseMagpieGatewayCatalog), + Effect.timeout("3 seconds"), + Effect.orElseSucceed(() => undefined), + ); +}); + +type MagpieRoute = "claude" | "codex" | "magpie-go" | "group" | "other"; +const ROUTE_ORDER: ReadonlyArray = ["claude", "codex", "magpie-go", "group", "other"]; + +function magpieRoute(model: MagpieGatewayModel): MagpieRoute { + const prefix = model.id.split("/", 1)[0]; + return prefix === "claude" || prefix === "codex" || prefix === "magpie-go" || prefix === "group" + ? prefix + : "other"; +} + +// Claude Code runs model names it does not recognise with a 200K window, whatever the model's own. +const UNRECOGNISED_MODEL_LIMIT = "200K context"; + +function magpieRouteLabel(model: MagpieGatewayModel, route: MagpieRoute): string { + const raw = model.magpie_label.split(" · ").at(-1)?.trim() || model.owned_by?.trim() || route; + const label = raw.charAt(0).toUpperCase() + raw.slice(1); + return route === "claude" ? label : `${label} · ${UNRECOGNISED_MODEL_LIMIT}`; +} + +/** Comparable Claude id: no `claude/` route, `[1m]` window or release date. */ +function claudeModelKey(id: string): string { + return id + .trim() + .toLowerCase() + .replace(/^claude\//, "") + .replace(/\[[^\]]*\]$/, "") + .replace(/-\d{8}$/, ""); +} + +const CLAUDE_EFFORT_LABELS: Readonly> = { + low: "Low", + medium: "Medium", + high: "High", + xhigh: "Extra High", + max: "Max", +}; + +function magpieServerModel(model: MagpieGatewayModel, route: MagpieRoute): ServerProviderModel { + // Claude Code sends only its own effort levels, so a gateway level such as `none` is unreachable. + const efforts = (model.supported_reasoning_levels ?? []).flatMap(({ effort }) => + (CLAUDE_EFFORT_LEVELS as ReadonlyArray).includes(effort) ? [effort] : [], + ); + return { + slug: model.id, + name: model.display_name?.trim() || model.id, + subProvider: magpieRouteLabel(model, route), + isCustom: false, + capabilities: { + optionDescriptors: efforts.length + ? [ + { + id: "effort", + label: "Reasoning", + type: "select", + // `default` sends no effort, so Claude Code picks one. + options: [ + { id: "default", label: "Default", isDefault: true }, + ...efforts.map((id) => ({ id, label: CLAUDE_EFFORT_LABELS[id] ?? id })), + ], + }, + ] + : [], + }, + }; +} + +/** + * The Claude models the gateway serves keep their built-in entries and options; every other + * gateway model is added after them, grouped by route. Custom models stay last. + */ +export function withMagpieGatewayModels( + models: ReadonlyArray, + gateway: ReadonlyArray, +): ReadonlyArray { + const servedClaude = new Set( + gateway.filter((model) => magpieRoute(model) === "claude").map((m) => claudeModelKey(m.id)), + ); + const modelKeys = (model: ServerProviderModel) => + [model.slug, ...(model.aliases ?? [])].map(claudeModelKey); + const builtIn = models.filter( + (model) => !model.isCustom && modelKeys(model).some((key) => servedClaude.has(key)), + ); + const custom = models.filter((model) => model.isCustom); + const coveredClaude = new Set(builtIn.flatMap(modelKeys)); + const takenSlugs = new Set([...builtIn, ...custom].map((model) => model.slug)); + const added = ROUTE_ORDER.flatMap((route) => + gateway.flatMap((model) => { + if (magpieRoute(model) !== route || takenSlugs.has(model.id)) return []; + if (route === "claude") { + const key = claudeModelKey(model.id); + if (coveredClaude.has(key)) return []; + coveredClaude.add(key); + } + takenSlugs.add(model.id); + return [magpieServerModel(model, route)]; + }), + ); + return [...builtIn, ...added, ...custom]; +}