File size: 1,476 Bytes
87337b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// middleware.js
import { NextRequest, NextResponse } from 'next/server';
const { AGENT_SERVER_URL } = process.env;
// Check if environment variables are available
if (!AGENT_SERVER_URL) {
throw "Environment variables AGENT_SERVER_URL are not available";
}
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
if (pathname.startsWith('/api/agents/')) {
if (!pathname.startsWith('/api/agents/start')) {
// Proxy all other agents API requests
const url = req.nextUrl.clone();
url.href = `${AGENT_SERVER_URL}${pathname.replace('/api/agents/', '/')}`;
// console.log(`Rewriting request to ${url.href}`);
return NextResponse.rewrite(url);
}
} else if (pathname.startsWith('/api/vector/')) {
// Proxy all other documents requests
const url = req.nextUrl.clone();
url.href = `${AGENT_SERVER_URL}${pathname.replace('/api/vector/', '/vector/')}`;
// console.log(`Rewriting request to ${url.href}`);
return NextResponse.rewrite(url);
} else if (pathname.startsWith('/api/token/')) {
// Proxy all other documents requests
const url = req.nextUrl.clone();
url.href = `${AGENT_SERVER_URL}${pathname.replace('/api/token/', '/token/')}`;
// console.log(`Rewriting request to ${url.href}`);
return NextResponse.rewrite(url);
} else {
return NextResponse.next();
}
} |