Spaces:
Runtime error
Runtime error
File size: 4,317 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 | import { getImageProvider } from "@omniroute/open-sse/config/imageRegistry";
import { getProviderOutboundGuard } from "@/shared/network/outboundUrlGuard";
import {
SAFE_OUTBOUND_FETCH_PRESETS,
SafeOutboundFetchError,
getSafeOutboundFetchErrorStatus,
safeOutboundFetch,
} from "@/shared/network/safeOutboundFetch";
const IMAGE_PROVIDER_VALIDATION_ENDPOINTS: Record<
string,
{ baseUrl?: string; path: string; method?: string }
> = {
"fal-ai": {
baseUrl: "https://api.fal.ai",
path: "/v1/models?limit=1",
},
"stability-ai": {
path: "/v1/user/account",
},
"black-forest-labs": {
path: "/v1/credits",
},
recraft: {
path: "/v1/users/me",
},
topaz: {
path: "/account/v1/credits/balance",
},
};
function normalizeBaseUrl(baseUrl: string) {
return (baseUrl || "").trim().replace(/\/$/, "");
}
function applyCustomUserAgent(headers: Record<string, string>, providerSpecificData: any = {}) {
const customUserAgent =
typeof providerSpecificData?.customUserAgent === "string"
? providerSpecificData.customUserAgent.trim()
: "";
if (customUserAgent) {
headers["user-agent"] = customUserAgent;
}
return headers;
}
function toValidationErrorResult(error: unknown) {
const message = error instanceof Error ? error.message : String(error || "Validation failed");
const statusCode = getSafeOutboundFetchErrorStatus(error);
return {
valid: false,
error: message || "Validation failed",
unsupported: false,
...(statusCode ? { statusCode } : {}),
...(error instanceof SafeOutboundFetchError && error.code === "TIMEOUT"
? { timeout: true }
: {}),
...(statusCode === 400 ? { securityBlocked: true } : {}),
};
}
function buildImageProviderValidationHeaders(
imageProvider: any,
apiKey: string,
providerSpecificData: any = {}
) {
const headers: Record<string, string> = {
Accept: "application/json",
};
if (apiKey) {
switch (String(imageProvider?.authHeader || "").toLowerCase()) {
case "bearer":
headers.Authorization = `Bearer ${apiKey}`;
break;
case "key":
headers.Authorization = `Key ${apiKey}`;
break;
case "x-key":
headers["x-key"] = apiKey;
break;
case "x-api-key":
headers["X-API-Key"] = apiKey;
break;
case "none":
break;
default:
headers.Authorization = `Bearer ${apiKey}`;
break;
}
}
return applyCustomUserAgent(headers, providerSpecificData);
}
async function validationRead(url: string, init: RequestInit) {
return safeOutboundFetch(url, {
...SAFE_OUTBOUND_FETCH_PRESETS.validationRead,
guard: getProviderOutboundGuard(),
...init,
});
}
export async function validateImageProviderApiKey({
provider,
apiKey,
providerSpecificData = {},
}: any) {
const imageProvider = getImageProvider(provider);
const validationConfig = IMAGE_PROVIDER_VALIDATION_ENDPOINTS[provider];
if (!imageProvider || !validationConfig) {
return { valid: false, error: "Provider validation not supported", unsupported: true };
}
try {
const baseUrl = normalizeBaseUrl(
providerSpecificData?.baseUrl || validationConfig.baseUrl || imageProvider.baseUrl
);
const url = `${baseUrl}${validationConfig.path}`;
const response = await validationRead(url, {
method: validationConfig.method || "GET",
headers: buildImageProviderValidationHeaders(imageProvider, apiKey, providerSpecificData),
});
if (response.ok) {
return { valid: true, error: null, method: "image-provider" };
}
if (response.status === 401 || response.status === 403) {
return { valid: false, error: "Invalid API key", method: "image-provider" };
}
if (response.status === 429) {
return {
valid: false,
error: "Validation rate limited (429)",
method: "image-provider",
};
}
if (response.status >= 500) {
return {
valid: false,
error: `Provider unavailable (${response.status})`,
method: "image-provider",
};
}
return {
valid: false,
error: `Validation failed: ${response.status}`,
method: "image-provider",
};
} catch (error: any) {
return toValidationErrorResult(error);
}
}
|