mirror of https://github.com/openclaw/openclaw.git
29 lines
867 B
TypeScript
29 lines
867 B
TypeScript
const DEFAULT_GOOGLE_API_HOST = "generativelanguage.googleapis.com";
|
|
|
|
export const DEFAULT_GOOGLE_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
|
|
function trimTrailingSlashes(value: string): string {
|
|
return value.replace(/\/+$/, "");
|
|
}
|
|
|
|
export function normalizeGoogleApiBaseUrl(baseUrl?: string): string {
|
|
const raw = trimTrailingSlashes(baseUrl?.trim() || DEFAULT_GOOGLE_API_BASE_URL);
|
|
try {
|
|
const url = new URL(raw);
|
|
url.hash = "";
|
|
url.search = "";
|
|
if (
|
|
url.hostname.toLowerCase() === DEFAULT_GOOGLE_API_HOST &&
|
|
trimTrailingSlashes(url.pathname || "") === ""
|
|
) {
|
|
url.pathname = "/v1beta";
|
|
}
|
|
return trimTrailingSlashes(url.toString());
|
|
} catch {
|
|
if (/^https:\/\/generativelanguage\.googleapis\.com\/?$/i.test(raw)) {
|
|
return DEFAULT_GOOGLE_API_BASE_URL;
|
|
}
|
|
return raw;
|
|
}
|
|
}
|