Resolve ai-service directly from the RAG routes, never the gateway

This commit is contained in:
2026-08-19 10:16:44 +07:00
parent f50ccfc5f7
commit 24c55d1627
13 changed files with 470 additions and 22 deletions
+11 -5
View File
@@ -4,7 +4,13 @@ import type { AnswerBlock, AnswerPlan, Citation, SendMessageResponse } from "@du
export const runtime = "nodejs";
const API_GATEWAY_URL = process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8000";
// RAG never goes through api-gateway. The gateway proxies `/auth/*` only --
// apps/api-gateway/src/proxy/ holds a single AuthProxyController -- so
// preferring AI_SERVICE_URL here pointed every RAG call at a service with no
// such route the moment apiGateway was enabled, breaking chat, suggest,
// history, sections, section-text and feedback at once. Resolve ai-service
// directly and let AI_SERVICE_URL mean what its name says: auth only.
const AI_SERVICE_URL = process.env.AI_SERVICE_URL ?? "http://localhost:8000";
interface RagCitation {
chunk_id: string;
@@ -257,9 +263,9 @@ export async function POST(request: Request) {
let rag: RagResponse;
try {
const targetUrl = API_GATEWAY_URL.includes("/v1/rag")
? API_GATEWAY_URL
: `${API_GATEWAY_URL}/v1/rag/query`;
const targetUrl = AI_SERVICE_URL.includes("/v1/rag")
? AI_SERVICE_URL
: `${AI_SERVICE_URL}/v1/rag/query`;
const upstreamHeaders: Record<string, string> = {
"Content-Type": "application/json",
@@ -306,7 +312,7 @@ export async function POST(request: Request) {
trace_id: `fallback-${Date.now()}`,
decision: "abstain",
reason: "upstream_unreachable",
answer: `Không thể kết nối đến AI Service (${API_GATEWAY_URL}). Vui lòng đảm bảo AI Service đã được bật.`,
answer: `Không thể kết nối đến AI Service (${AI_SERVICE_URL}). Vui lòng đảm bảo AI Service đã được bật.`,
resolved_drug_id: null,
citations: [],
};
+8 -3
View File
@@ -2,8 +2,13 @@ import { NextResponse } from "next/server";
export const runtime = "nodejs";
const API_GATEWAY_URL =
process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8000";
// RAG never goes through api-gateway. The gateway proxies `/auth/*` only --
// apps/api-gateway/src/proxy/ holds a single AuthProxyController -- so
// preferring AI_SERVICE_URL here pointed every RAG call at a service with no
// such route the moment apiGateway was enabled, breaking chat, suggest,
// history, sections, section-text and feedback at once. Resolve ai-service
// directly and let AI_SERVICE_URL mean what its name says: auth only.
const AI_SERVICE_URL = process.env.AI_SERVICE_URL ?? "http://localhost:8000";
export async function POST(request: Request) {
let traceId: string;
@@ -35,7 +40,7 @@ export async function POST(request: Request) {
return NextResponse.json({ error: "conversation_id_too_long" }, { status: 400 });
}
const base = API_GATEWAY_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
const base = AI_SERVICE_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
try {
const upstream = await fetch(`${base}/v1/rag/feedback`, {
method: "POST",
+8 -3
View File
@@ -2,8 +2,13 @@ import { NextResponse } from "next/server";
export const runtime = "nodejs";
const API_GATEWAY_URL =
process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8000";
// RAG never goes through api-gateway. The gateway proxies `/auth/*` only --
// apps/api-gateway/src/proxy/ holds a single AuthProxyController -- so
// preferring AI_SERVICE_URL here pointed every RAG call at a service with no
// such route the moment apiGateway was enabled, breaking chat, suggest,
// history, sections, section-text and feedback at once. Resolve ai-service
// directly and let AI_SERVICE_URL mean what its name says: auth only.
const AI_SERVICE_URL = process.env.AI_SERVICE_URL ?? "http://localhost:8000";
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
@@ -14,7 +19,7 @@ export async function GET(request: Request) {
}
try {
const base = API_GATEWAY_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
const base = AI_SERVICE_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
const targetUrl = `${base}/v1/rag/history?conversation_id=${encodeURIComponent(conversationId)}`;
const upstream = await fetch(targetUrl, {
+8 -3
View File
@@ -2,11 +2,16 @@ import { NextResponse } from "next/server";
export const runtime = "nodejs";
const API_GATEWAY_URL =
process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8000";
// RAG never goes through api-gateway. The gateway proxies `/auth/*` only --
// apps/api-gateway/src/proxy/ holds a single AuthProxyController -- so
// preferring AI_SERVICE_URL here pointed every RAG call at a service with no
// such route the moment apiGateway was enabled, breaking chat, suggest,
// history, sections, section-text and feedback at once. Resolve ai-service
// directly and let AI_SERVICE_URL mean what its name says: auth only.
const AI_SERVICE_URL = process.env.AI_SERVICE_URL ?? "http://localhost:8000";
function ragBaseUrl() {
return API_GATEWAY_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
return AI_SERVICE_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
}
export async function GET(request: Request) {
+8 -3
View File
@@ -2,11 +2,16 @@ import { NextResponse } from "next/server";
export const runtime = "nodejs";
const API_GATEWAY_URL =
process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8000";
// RAG never goes through api-gateway. The gateway proxies `/auth/*` only --
// apps/api-gateway/src/proxy/ holds a single AuthProxyController -- so
// preferring AI_SERVICE_URL here pointed every RAG call at a service with no
// such route the moment apiGateway was enabled, breaking chat, suggest,
// history, sections, section-text and feedback at once. Resolve ai-service
// directly and let AI_SERVICE_URL mean what its name says: auth only.
const AI_SERVICE_URL = process.env.AI_SERVICE_URL ?? "http://localhost:8000";
function ragBaseUrl() {
return API_GATEWAY_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
return AI_SERVICE_URL.replace(/\/v1\/rag(?:\/query)?\/?$/, "");
}
export async function GET(request: Request) {
+10 -5
View File
@@ -2,8 +2,13 @@ import { NextResponse } from "next/server";
export const runtime = "nodejs";
const API_GATEWAY_URL =
process.env.API_GATEWAY_URL ?? process.env.AI_SERVICE_URL ?? "http://localhost:8000";
// RAG never goes through api-gateway. The gateway proxies `/auth/*` only --
// apps/api-gateway/src/proxy/ holds a single AuthProxyController -- so
// preferring AI_SERVICE_URL here pointed every RAG call at a service with no
// such route the moment apiGateway was enabled, breaking chat, suggest,
// history, sections, section-text and feedback at once. Resolve ai-service
// directly and let AI_SERVICE_URL mean what its name says: auth only.
const AI_SERVICE_URL = process.env.AI_SERVICE_URL ?? "http://localhost:8000";
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
@@ -14,9 +19,9 @@ export async function GET(request: Request) {
}
try {
const targetUrl = API_GATEWAY_URL.includes("/v1/rag")
? `${API_GATEWAY_URL.replace(/\/query$/, "/suggest")}?q=${encodeURIComponent(q)}`
: `${API_GATEWAY_URL}/v1/rag/suggest?q=${encodeURIComponent(q)}`;
const targetUrl = AI_SERVICE_URL.includes("/v1/rag")
? `${AI_SERVICE_URL.replace(/\/query$/, "/suggest")}?q=${encodeURIComponent(q)}`
: `${AI_SERVICE_URL}/v1/rag/suggest?q=${encodeURIComponent(q)}`;
const upstream = await fetch(targetUrl, {
method: "GET",