Spaces:
Runtime error
Runtime error
File size: 9,447 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | // Anthropic/Claude-format provider key validators (anthropic-like, claude-oauth-inline, anthropic-compatible, claude-code-compatible).
// Extracted from validation.ts (god-file decomposition) — top-level functions; behavior is
// byte-identical to the original inline defs.
import {
buildClaudeCodeCompatibleHeaders,
buildClaudeCodeCompatibleValidationPayload,
CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH,
CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH,
joinClaudeCodeCompatibleUrl,
joinBaseUrlAndPath,
} from "@omniroute/open-sse/services/claudeCodeCompatible.ts";
import {
normalizeAnthropicBaseUrl,
normalizeClaudeCodeCompatibleBaseUrl,
} from "./urlHelpers";
import { applyCustomUserAgent } from "./headers";
import {
toValidationErrorResult,
validationRead,
validationWrite,
} from "./transport";
export async function validateAnthropicLikeProvider({
apiKey,
baseUrl,
modelId = "claude-3-5-sonnet-20240620",
headers = {},
providerSpecificData = {},
isLocal = false,
}: any) {
try {
if (!baseUrl) {
return { valid: false, error: "Missing base URL" };
}
if (typeof apiKey === "string" && apiKey.startsWith("sk-ant-oat")) {
return validateClaudeOAuthInline({ apiKey, modelId, providerSpecificData });
}
const probeUrl =
typeof providerSpecificData?.modelsUrl === "string" &&
providerSpecificData.modelsUrl.trim() !== ""
? providerSpecificData.modelsUrl.trim()
: `${baseUrl}/models`;
// Best-effort /models probe. It must not fail validation: canonical Claude
// base URLs can already include a path/query (…/messages?beta=true).
try {
await validationRead(
probeUrl,
{
headers: {
"anthropic-version": "2023-06-01",
...headers,
},
},
isLocal
);
} catch {
// ignore probe failures
}
const requestUrl =
typeof providerSpecificData?.modelsUrl === "string" &&
providerSpecificData.modelsUrl.trim() !== ""
? providerSpecificData.modelsUrl.trim()
: "";
if (requestUrl) {
const response = await validationRead(
requestUrl,
{
headers: {
"anthropic-version": "2023-06-01",
...headers,
},
},
isLocal
);
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key" };
}
}
const requestHeaders = applyCustomUserAgent(
{
"Content-Type": "application/json",
...headers,
},
providerSpecificData
);
if (!requestHeaders["x-api-key"] && !requestHeaders["X-API-Key"]) {
requestHeaders["x-api-key"] = apiKey;
}
if (!requestHeaders["anthropic-version"] && !requestHeaders["Anthropic-Version"]) {
requestHeaders["anthropic-version"] = "2023-06-01";
}
const testModelId =
providerSpecificData?.validationModelId || modelId || "claude-3-5-sonnet-20241022";
const chatResponse = await validationWrite(
baseUrl,
{
method: "POST",
headers: requestHeaders,
body: JSON.stringify({
model: testModelId,
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
},
isLocal
);
if (chatResponse.status === 401 || chatResponse.status === 403) {
return { valid: false, error: "Invalid API key" };
}
return { valid: true, error: null };
} catch (error: any) {
return toValidationErrorResult(error);
}
}
export async function validateClaudeOAuthInline({
apiKey,
modelId,
providerSpecificData = {},
}: {
apiKey: string;
modelId: string | null | undefined;
providerSpecificData?: Record<string, unknown>;
}) {
const testModelId =
providerSpecificData?.validationModelId || modelId || "claude-haiku-4-5-20251001";
try {
const { getExecutor } = await import("@omniroute/open-sse/executors/index.ts");
const { response } = await getExecutor("claude").execute({
model: testModelId,
body: {
model: testModelId,
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
},
stream: false,
credentials: { accessToken: apiKey, providerSpecificData },
});
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid OAuth token" };
}
if (response.status >= 500) {
return { valid: false, error: `Provider unavailable (${response.status})` };
}
return { valid: true, error: null };
} catch (error: any) {
return toValidationErrorResult(error);
}
}
export async function validateAnthropicCompatibleProvider({
apiKey,
providerSpecificData = {},
isLocal = false,
}: any) {
let baseUrl = normalizeAnthropicBaseUrl(providerSpecificData.baseUrl);
if (!baseUrl) {
return { valid: false, error: "No base URL configured for Anthropic compatible provider" };
}
const headers = applyCustomUserAgent(
{
"Content-Type": "application/json",
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
Authorization: `Bearer ${apiKey}`,
},
providerSpecificData
);
// Step 1: Best-effort GET /models probe. /models is NOT part of the Anthropic API spec
// and many compatible proxies either 404, 401, or 403 on /models even with a valid key —
// so a 401/403 here must NOT mark the credentials invalid. Only a 2xx is a positive
// signal that the proxy DOES implement /models AND the key was accepted; everything else
// (including auth-shaped statuses) falls through to the authoritative POST /v1/messages
// probe below. Ported from decolua/9router 584cf66a.
try {
const modelsRes = await validationRead(
joinBaseUrlAndPath(baseUrl, providerSpecificData?.modelsPath || "/models"),
{
method: "GET",
headers,
},
isLocal
);
if (modelsRes.ok) {
return { valid: true, error: null };
}
} catch {
// /models fetch failed — fall through to messages test
}
// Step 2: Authoritative probe — POST /v1/messages with max_tokens=1.
const testModelId = providerSpecificData?.validationModelId || "claude-3-5-sonnet-20241022";
try {
const messagesRes = await validationWrite(
joinBaseUrlAndPath(baseUrl, providerSpecificData?.chatPath || "/messages"),
{
method: "POST",
headers,
body: JSON.stringify({
model: testModelId,
max_tokens: 1,
messages: [{ role: "user", content: "test" }],
}),
},
isLocal
);
if (messagesRes.status === 401 || messagesRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
// Any other response (200, 400, 422, etc.) means auth passed
return { valid: true, error: null };
} catch (error: any) {
return toValidationErrorResult(error);
}
}
export async function validateClaudeCodeCompatibleProvider({
apiKey,
providerSpecificData = {},
}: any) {
const baseUrl = normalizeClaudeCodeCompatibleBaseUrl(providerSpecificData.baseUrl);
if (!baseUrl) {
return { valid: false, error: "No base URL configured for CC Compatible provider" };
}
const modelsPath = providerSpecificData?.modelsPath || CLAUDE_CODE_COMPATIBLE_DEFAULT_MODELS_PATH;
const chatPath = providerSpecificData?.chatPath || CLAUDE_CODE_COMPATIBLE_DEFAULT_CHAT_PATH;
const defaultHeaders = applyCustomUserAgent(
buildClaudeCodeCompatibleHeaders(apiKey, false),
providerSpecificData
);
try {
const modelsRes = await validationRead(joinClaudeCodeCompatibleUrl(baseUrl, modelsPath), {
method: "GET",
headers: defaultHeaders,
});
if (modelsRes.ok) {
return { valid: true, error: null, method: "models_endpoint" };
}
if (modelsRes.status === 401 || modelsRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
} catch {
// Fall through to bridge request validation.
}
const payload = buildClaudeCodeCompatibleValidationPayload(
providerSpecificData?.validationModelId || "claude-sonnet-4-6"
);
const sessionId = JSON.parse(payload.metadata.user_id as string).session_id;
try {
const messagesRes = await validationWrite(joinClaudeCodeCompatibleUrl(baseUrl, chatPath), {
method: "POST",
headers: applyCustomUserAgent(
buildClaudeCodeCompatibleHeaders(apiKey, true, sessionId),
providerSpecificData
),
body: JSON.stringify(payload),
});
if (messagesRes.status === 401 || messagesRes.status === 403) {
return { valid: false, error: "Invalid API key" };
}
if (messagesRes.status === 429) {
return {
valid: true,
error: null,
method: "cc_bridge_request",
warning: "Rate limited, but credentials are valid",
};
}
if (messagesRes.status >= 400 && messagesRes.status < 500) {
return {
valid: true,
error: null,
method: "cc_bridge_request",
warning: "Bridge request reached upstream, but the model or payload was rejected",
};
}
return {
valid: messagesRes.ok,
error: messagesRes.ok ? null : `Validation failed: ${messagesRes.status}`,
method: "cc_bridge_request",
};
} catch (error: any) {
return toValidationErrorResult(error);
}
}
|