diff --git a/chatdesk-ui/lib/generate-bgem3-embedding.ts b/chatdesk-ui/lib/generate-bgem3-embedding.ts index 8407a5c..ac4eab1 100644 --- a/chatdesk-ui/lib/generate-bgem3-embedding.ts +++ b/chatdesk-ui/lib/generate-bgem3-embedding.ts @@ -1,14 +1,19 @@ //import { getRuntimeEnv } from "@/lib/ipconfig" -import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 +//import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 +import { getServerRuntimeUrl } from "@/lib/runtime-url" export async function generateBgeM3Embedding(text: string): Promise { try { // 取 Supabase URL 或本地默认 - const supaUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" - // 构造 Embedding 服务地址:同 host + 8001 端口 - const urlObj = new URL(supaUrl) - urlObj.port = "8001" // 强制改成 8001 - const apiUrl = `${urlObj.origin}/v1/embeddings` + // const supaUrl = getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000" + // // 构造 Embedding 服务地址:同 host + 8001 端口 + // const urlObj = new URL(supaUrl) + // urlObj.port = "8001" // 强制改成 8001 + // const apiUrl = `${urlObj.origin}/v1/embeddings` + // console.debug("......[generateBgeM3Embedding] apiUrl =", apiUrl) + + // ✅ 使用服务端真实 URL(自动取协议/host)→ 端口强制为 8001 + const apiUrl = `${getServerRuntimeUrl("8001")}/v1/embeddings` console.debug("......[generateBgeM3Embedding] apiUrl =", apiUrl) const response = await fetch(apiUrl, { diff --git a/chatdesk-ui/lib/get-runtime-env.ts b/chatdesk-ui/lib/get-runtime-env.ts index 77af75d..db3bc21 100644 --- a/chatdesk-ui/lib/get-runtime-env.ts +++ b/chatdesk-ui/lib/get-runtime-env.ts @@ -1,16 +1,24 @@ // lib/get-runtime-env.ts +import { headers } from "next/headers" + export function getRuntimeEnv(key: string): string | undefined { - /* ---------- 服务器端 (SSR) ---------- */ + // 包装成闭包 → 防止构建期静态优化 + const getFromEnv = () => process.env[key] + if (typeof window === "undefined") { - const val = process.env[key] - console.log(`[getRuntimeEnv][SSR] key=${key} -> ${val ?? "undefined"} (process.env)`) - return val + // SSR 逻辑 + if (key === "SUPABASE_URL") { + const h = headers() + const proto = h.get("x-forwarded-proto") ?? (h.get("host")?.includes(":443") ? "https" : "http") + const rawHost = h.get("x-forwarded-host") ?? h.get("host")! + const hostname = rawHost.split(",")[0].split(":")[0].trim() + return `${proto}://${hostname}:8000` + } + + // 默认 fallback 到 env + return getFromEnv() } - /* ---------- 客户端 (CSR) ---------- */ - const val = (window as any).RUNTIME_ENV?.[key] - console.log( - `[getRuntimeEnv][CSR] key=${key} -> ${val ?? "undefined"} (window.RUNTIME_ENV)` - ) - return val -} \ No newline at end of file + // 客户端 + return (window as any).RUNTIME_ENV?.[key] +} diff --git a/chatdesk-ui/lib/lib/runtime-url.ts b/chatdesk-ui/lib/lib/runtime-url.ts new file mode 100644 index 0000000..8583c63 --- /dev/null +++ b/chatdesk-ui/lib/lib/runtime-url.ts @@ -0,0 +1,14 @@ +// lib/runtime-url.ts +import { headers } from "next/headers" + +/** + * 返回服务端当前请求的 base url,支持自定义端口 + * 示例: getServerRuntimeUrl("8001") => http://host:8001 + */ +export function getServerRuntimeUrl(port = "8000"): string { + const h = headers() + const proto = h.get("x-forwarded-proto") ?? (h.get("host")?.includes(":443") ? "https" : "http") + const rawHost = h.get("x-forwarded-host") ?? h.get("host")! + const hostname = rawHost.split(",")[0].split(":")[0].trim() + return `${proto}://${hostname}:${port}` +} diff --git a/chatdesk-ui/lib/supabase/middleware.ts b/chatdesk-ui/lib/supabase/middleware.ts index 714763e..d3f9e9f 100644 --- a/chatdesk-ui/lib/supabase/middleware.ts +++ b/chatdesk-ui/lib/supabase/middleware.ts @@ -1,7 +1,84 @@ +// import { createServerClient, type CookieOptions } from "@supabase/ssr" +// import { NextResponse, type NextRequest } from "next/server" +// //import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入 +// import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 + +// export const createClient = (request: NextRequest) => { +// // Create an unmodified response +// let response = NextResponse.next({ +// request: { +// headers: request.headers +// } +// }) + +// const supabase = createServerClient( +// getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", +// process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, +// { +// cookies: { +// get(name: string) { +// return request.cookies.get(name)?.value +// }, +// set(name: string, value: string, options: CookieOptions) { +// // If the cookie is updated, update the cookies for the request and response +// request.cookies.set({ +// name, +// value, +// ...options +// }) +// response = NextResponse.next({ +// request: { +// headers: request.headers +// } +// }) +// response.cookies.set({ +// name, +// value, +// ...options +// }) +// }, +// remove(name: string, options: CookieOptions) { +// // If the cookie is removed, update the cookies for the request and response +// request.cookies.set({ +// name, +// value: "", +// ...options +// }) +// response = NextResponse.next({ +// request: { +// headers: request.headers +// } +// }) +// response.cookies.set({ +// name, +// value: "", +// ...options +// }) +// } +// } +// } +// ) + +// return { supabase, response } +// } + + + + + import { createServerClient, type CookieOptions } from "@supabase/ssr" import { NextResponse, type NextRequest } from "next/server" -//import { getRuntimeEnv } from "@/lib/ipconfig" // 新增引入 -import { getRuntimeEnv } from "@/lib/get-runtime-env" // ✅ 新路径 + +// ✅ 用 headers 构造 host,不再使用 getRuntimeEnv +function getSupabaseUrlFromRequest(req: NextRequest, port = "8000"): string { + const proto = + req.headers.get("x-forwarded-proto") ?? + (req.headers.get("host")?.includes(":443") ? "https" : "http") + + const rawHost = req.headers.get("x-forwarded-host") ?? req.headers.get("host")! + const hostname = rawHost.split(",")[0].split(":")[0].trim() + return `${proto}://${hostname}:${port}` +} export const createClient = (request: NextRequest) => { // Create an unmodified response @@ -12,7 +89,7 @@ export const createClient = (request: NextRequest) => { }) const supabase = createServerClient( - getRuntimeEnv("SUPABASE_URL") ?? "http://localhost:8000", + getSupabaseUrlFromRequest(request), // ✅ 这里改了,其他全保留 process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, { cookies: { @@ -20,40 +97,14 @@ export const createClient = (request: NextRequest) => { return request.cookies.get(name)?.value }, set(name: string, value: string, options: CookieOptions) { - // If the cookie is updated, update the cookies for the request and response - request.cookies.set({ - name, - value, - ...options - }) - response = NextResponse.next({ - request: { - headers: request.headers - } - }) - response.cookies.set({ - name, - value, - ...options - }) + request.cookies.set({ name, value, ...options }) + response = NextResponse.next({ request: { headers: request.headers } }) + response.cookies.set({ name, value, ...options }) }, remove(name: string, options: CookieOptions) { - // If the cookie is removed, update the cookies for the request and response - request.cookies.set({ - name, - value: "", - ...options - }) - response = NextResponse.next({ - request: { - headers: request.headers - } - }) - response.cookies.set({ - name, - value: "", - ...options - }) + request.cookies.set({ name, value: "", ...options }) + response = NextResponse.next({ request: { headers: request.headers } }) + response.cookies.set({ name, value: "", ...options }) } } }