diff --git a/client/src/lib/aiApi.ts b/client/src/lib/aiApi.ts index eaa1412..effcba1 100644 --- a/client/src/lib/aiApi.ts +++ b/client/src/lib/aiApi.ts @@ -13,13 +13,31 @@ import { } from "./luxsinApi"; import { AI_NETWORK_ERROR_CODE, isLikelyNetworkFetchError } from "./networkError"; -// Allow override via Vite env / localStorage; default to same origin in prod. +const AI_HOST = "v2ai.luxsin.com.cn"; + +/** Match the page protocol so http://device-ip does not call https://v2ai (and vice versa). */ +function currentPageProtocol(): "http:" | "https:" { + if (typeof window !== "undefined" && window.location.protocol === "http:") return "http:"; + return "https:"; +} + +function withPageProtocol(url: string): string { + const trimmed = url.replace(/\/$/, ""); + const protocol = currentPageProtocol(); + if (/^https?:\/\//i.test(trimmed)) { + return trimmed.replace(/^https?:\/\//i, `${protocol}//`); + } + if (trimmed.startsWith("//")) return `${protocol}${trimmed}`; + return trimmed; +} + +// Allow override via Vite env / localStorage; protocol follows the current page. export function getAIBaseUrl(): string { const fromLs = typeof window !== "undefined" ? window.localStorage.getItem("luxsin_ai_url") : null; - if (fromLs) return fromLs.replace(/\/$/, ""); + if (fromLs) return withPageProtocol(fromLs); const fromEnv = (import.meta as any).env?.VITE_AI_API_URL as string | undefined; - if (fromEnv) return fromEnv.replace(/\/$/, ""); - return "https://v2ai.luxsin.com.cn"; + if (fromEnv) return withPageProtocol(fromEnv); + return `${currentPageProtocol()}//${AI_HOST}`; } // ============================================================