60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
const GET_PROXY_PREFIXES = Object.freeze(["/v1/"]);
|
|
const GET_PROXY_ROUTES = new Set(["/v1"]);
|
|
const POST_PROXY_ROUTES = new Set([
|
|
"/json-rpc",
|
|
"/v1/agent/chat",
|
|
"/v1/agent/chat/cancel",
|
|
"/v1/m3/io"
|
|
]);
|
|
const PUBLIC_PROXY_ROUTES = new Set([
|
|
"POST /v1/agent/chat",
|
|
"POST /v1/agent/chat/cancel"
|
|
]);
|
|
|
|
export function cloudWebProxyRoutePolicy(method, pathname) {
|
|
const normalizedMethod = normalizeMethod(method);
|
|
const normalizedPath = normalizePathname(pathname);
|
|
const routeKey = `${normalizedMethod} ${normalizedPath}`;
|
|
const proxy = isCloudApiProxyRoute(normalizedMethod, normalizedPath);
|
|
const publicRoute = proxy && (
|
|
PUBLIC_PROXY_ROUTES.has(routeKey) ||
|
|
isPublicCodeAgentPollRoute(normalizedMethod, normalizedPath)
|
|
);
|
|
return {
|
|
proxy,
|
|
authRequired: proxy && !publicRoute,
|
|
publicRoute,
|
|
routeKey
|
|
};
|
|
}
|
|
|
|
export function isCloudApiProxyRoute(method, pathname) {
|
|
const normalizedMethod = normalizeMethod(method);
|
|
const normalizedPath = normalizePathname(pathname);
|
|
if (normalizedMethod === "GET") {
|
|
return GET_PROXY_ROUTES.has(normalizedPath) ||
|
|
GET_PROXY_PREFIXES.some((prefix) => normalizedPath.startsWith(prefix));
|
|
}
|
|
if (normalizedMethod === "POST") {
|
|
return POST_PROXY_ROUTES.has(normalizedPath);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function isPublicCodeAgentPollRoute(method, pathname) {
|
|
return method === "GET" && (
|
|
pathname.startsWith("/v1/agent/chat/result/") ||
|
|
pathname.startsWith("/v1/agent/chat/trace/")
|
|
);
|
|
}
|
|
|
|
function normalizeMethod(method) {
|
|
return String(method || "GET").trim().toUpperCase() || "GET";
|
|
}
|
|
|
|
function normalizePathname(pathname) {
|
|
const value = String(pathname || "/").trim();
|
|
if (!value || value[0] !== "/") return "/";
|
|
return value.replace(/\/+$/u, "") || "/";
|
|
}
|