Skip to content
TypeScript

fix(server): custom Claude endpoint no longer reads as logged out (#21169)

Arta51d1f6f623dd
diff --git a/apps/server/src/provider/ClaudeProvider.ts b/apps/server/src/provider/ClaudeProvider.ts
index a6614cd6680..2ea79160b05 100644
--- a/apps/server/src/provider/ClaudeProvider.ts
+++ b/apps/server/src/provider/ClaudeProvider.ts
@@ -622,7 +622,14 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
});
}
- if (claudeAuthStatus(capabilities) === "unauthenticated") {
+ // First-party account fields cannot verify custom endpoint credentials.
+ const endpoint = URL.parse(resolvedEnvironment.ANTHROPIC_BASE_URL?.trim() ?? "");
+ const customApiEndpoint =
+ endpoint !== null &&
+ (endpoint.protocol === "http:" || endpoint.protocol === "https:") &&
+ endpoint.hostname !== "" &&
+ endpoint.hostname !== "api.anthropic.com";
+ if (!customApiEndpoint && claudeAuthStatus(capabilities) === "unauthenticated") {
return buildServerProvider({
presentation: CLAUDE_PRESENTATION,
enabled: claudeSettings.enabled,
@@ -644,7 +651,9 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
claudeAuthMetadata({
subscriptionType: capabilities.subscriptionType,
authMethod: capabilities.tokenSource,
- }) ?? apiProviderAuthMetadata(capabilities.apiProvider);
+ }) ??
+ apiProviderAuthMetadata(capabilities.apiProvider) ??
+ (customApiEndpoint ? { type: "customEndpoint", label: "Custom API endpoint" } : undefined);
const usageLimits = !capabilities.usage
? makeUnavailableUsageLimits({ checkedAt, reason: "probeFailed" })
: scopedLimitNames
diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.ts
index d827aebbc5b..5c06f16f3db 100644
--- a/apps/server/src/provider/ProviderRegistry.test.ts
+++ b/apps/server/src/provider/ProviderRegistry.test.ts
@@ -70,6 +70,7 @@ import type {
import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";
import * as ProviderRegistry from "./ProviderRegistry.ts";
import { ProviderDriverError } from "./Errors.ts";
+import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
import {
makeManualOnlyProviderMaintenanceCapabilities,
makeProviderMaintenanceCapabilities,
@@ -4105,6 +4106,7 @@ it.layer(
apiKeySource: "none",
apiProvider: "firstParty",
}),
+ { ANTHROPIC_BASE_URL: "" },
);
assert.strictEqual(status.status, "error");
assert.strictEqual(status.auth.status, "unauthenticated");
@@ -4119,6 +4121,80 @@ it.layer(
),
);
+ it.effect.each([
+ { source: "inherited HTTPS endpoint", baseUrl: "https://proxy.example/api", overrides: [] },
+ {
+ source: "instance HTTP endpoint overriding Anthropic",
+ baseUrl: "https://api.anthropic.com",
+ overrides: [
+ { name: "ANTHROPIC_BASE_URL", value: "http://localhost:8080", sensitive: false },
+ ],
+ },
+ ])("keeps Claude ready with an empty first-party account: $source", ({ baseUrl, overrides }) =>
+ Effect.gen(function* () {
+ const environment = yield* mergeProviderInstanceEnvironment(overrides, {
+ ANTHROPIC_BASE_URL: baseUrl,
+ });
+ const status = yield* checkClaudeProviderStatus(
+ defaultClaudeSettings,
+ claudeCapabilities({
+ tokenSource: "none",
+ apiKeySource: "none",
+ apiProvider: "firstParty",
+ }),
+ environment,
+ );
+ assert.strictEqual(status.status, "ready");
+ assert.strictEqual(status.auth.status, "authenticated");
+ assert.strictEqual(status.auth.type, "customEndpoint");
+ assert.strictEqual(status.auth.label, "Custom API endpoint");
+ }).pipe(
+ Effect.provide(
+ layerMockSpawner((args) => {
+ const joined = args.join(" ");
+ if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
+ throw new Error(`Unexpected args: ${joined}`);
+ }),
+ ),
+ ),
+ );
+
+ it.effect.each([
+ "https://api.anthropic.com",
+ "https://api.anthropic.com:443",
+ "https://api.anthropic.com:8443",
+ "openrouter.ai/api",
+ "localhost:8080",
+ "ftp://proxy.example",
+ ])("keeps Claude logged out when the instance endpoint is %s", (baseUrl) =>
+ Effect.gen(function* () {
+ const environment = yield* mergeProviderInstanceEnvironment(
+ [{ name: "ANTHROPIC_BASE_URL", value: baseUrl, sensitive: false }],
+ { ANTHROPIC_BASE_URL: "https://proxy.example/api" },
+ );
+ const status = yield* checkClaudeProviderStatus(
+ defaultClaudeSettings,
+ claudeCapabilities({
+ tokenSource: "none",
+ apiKeySource: "none",
+ apiProvider: "firstParty",
+ }),
+ environment,
+ );
+ assert.strictEqual(status.status, "error");
+ assert.strictEqual(status.auth.status, "unauthenticated");
+ assert.strictEqual(status.auth.type, undefined);
+ }).pipe(
+ Effect.provide(
+ layerMockSpawner((args) => {
+ const joined = args.join(" ");
+ if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
+ throw new Error(`Unexpected args: ${joined}`);
+ }),
+ ),
+ ),
+ );
+
it.effect("keeps an API key install authenticated when it reports no token source", () =>
Effect.gen(function* () {
// `ANTHROPIC_API_KEY` never populates `tokenSource`, so reading that