Fix Ask AI proxy to use internal service URL (#519)

This commit is contained in:
shenlan 2025-10-14 19:40:01 +08:00 committed by GitHub
parent 186d0adb07
commit 808aed7cef
3 changed files with 31 additions and 4 deletions

View File

@ -1,8 +1,8 @@
import { getServerServiceBaseUrl } from '@lib/serviceConfig'
import { getInternalServerServiceBaseUrl } from '@lib/serviceConfig'
export async function POST(req: Request) {
const { question, history } = await req.json()
const apiBase = getServerServiceBaseUrl()
const apiBase = getInternalServerServiceBaseUrl()
const res = await fetch(`${apiBase}/api/askai`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },

View File

@ -1,9 +1,9 @@
import { getServerServiceBaseUrl } from '@lib/serviceConfig'
import { getInternalServerServiceBaseUrl } from '@lib/serviceConfig'
export async function POST(req: Request) {
try {
const { question, history } = await req.json()
const apiBase = getServerServiceBaseUrl()
const apiBase = getInternalServerServiceBaseUrl()
const response = await fetch(`${apiBase}/api/rag/query`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },

View File

@ -69,6 +69,7 @@ type ServiceKey = keyof EnvironmentRuntimeConfig
const FALLBACK_ACCOUNT_SERVICE_URL = 'http://localhost:8080'
const FALLBACK_SERVER_SERVICE_URL = 'http://localhost:8090'
const FALLBACK_SERVER_SERVICE_INTERNAL_URL = 'http://127.0.0.1:8090'
const LOCAL_HOSTNAMES = new Set(['localhost', '127.0.0.1', '[::1]'])
@ -199,6 +200,32 @@ export function getServerServiceBaseUrl(): string {
return normalizeBaseUrl(configured ?? DEFAULT_SERVER_SERVICE_URL)
}
const SERVER_INTERNAL_URL_ENV_KEYS = [
'SERVER_SERVICE_INTERNAL_URL',
'SERVER_INTERNAL_URL',
'INTERNAL_SERVER_SERVICE_URL',
] as const
export function getInternalServerServiceBaseUrl(): string {
const configured = readEnvValue(...SERVER_INTERNAL_URL_ENV_KEYS)
if (configured) {
return normalizeBaseUrl(configured)
}
const external = getServerServiceBaseUrl()
try {
const parsed = new URL(external)
if (LOCAL_HOSTNAMES.has(parsed.hostname)) {
return normalizeBaseUrl(external)
}
} catch {
// Ignore parsing errors and fall back to the internal default below.
}
return normalizeBaseUrl(FALLBACK_SERVER_SERVICE_INTERNAL_URL)
}
export const serviceConfig = {
account: {
baseUrl: getAccountServiceBaseUrl(),