From 053c0e3cd844034872dc60668418dd35180896b8 Mon Sep 17 00:00:00 2001 From: fucheng Date: Sun, 20 Sep 2026 19:29:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(ai):=20AI=20=E6=8E=A5=E5=8F=A3=E5=8D=8F?= =?UTF-8?q?=E8=AE=AE=E8=B7=9F=E9=9A=8F=E5=BD=93=E5=89=8D=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=86=99=E6=AD=BB=20https?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- client/src/lib/aiApi.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) 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}`; } // ============================================================