mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 04:29:27 +08:00
The interface ships in French and Ukrainian alongside English and Chinese, and /language pins a reply language for one conversation. Immersive reading organizes collections as colored folders, adds a fullscreen learning mode, and turns Explain / Translate / Guide me on a selection into ordinary messages in the reading chat; "Quiz me on this page" runs one quiz turn without changing the conversation's mode, which retires the reading-openers endpoint and its task model. Images embedded in DOCX, PPTX and PDF are extracted in place and reach vision models. Office files preview as paged PDFs through LibreOffice (now in the Docker image), falling back to the in-browser renderers without it. Web sources re-sync on a durable schedule, linked folders sync on demand, .bib files index, and a zotero_search tool queries Zotero. Settings gains Learning progress and credential-free presets; admins import users from CSV; split deployments can hand a session to a public device. Grok CLI joins the subagents, Unifically the providers, ChatGPT exports the importers. Gate cleanup: mypy/bandit findings in the new launcher, audio-overview, web-source, reading-store and textbook modules; the task-model copy test follows the retired reading_openers kind. See assets/releases/ver1-6-11.md for the full notes, and the README Releases row plus its eleven translations.
109 lines
4.0 KiB
TypeScript
109 lines
4.0 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { parseAuthEnabled } from "./lib/api";
|
|
import { resolveBackendApiBase } from "./lib/backend-runtime-config";
|
|
import { prepareBackendForwardHeaders } from "./lib/backend-forward";
|
|
import {
|
|
CODEX_CALLBACK_API_PATH,
|
|
COOKIE_NAME,
|
|
LOGIN_PATH,
|
|
classifyToken,
|
|
isAuthExempt,
|
|
isBackendPath,
|
|
isCodexCallbackPath,
|
|
isRetiredPagePath,
|
|
isWebSocketPath,
|
|
} from "./lib/proxy-policy";
|
|
|
|
// Backend base URL for `/api/*` and `/ws/*` rewrites. The container entrypoint
|
|
// exports `DEEPTUTOR_API_BASE_URL` from `data/user/settings/system.json`
|
|
// (preferring `next_public_api_base`, then `next_public_api_base_external`,
|
|
// then `http://127.0.0.1:${BACKEND_PORT}`). This last-resort default applies
|
|
// only when nothing exported the variable at all.
|
|
//
|
|
// The loopback is spelled as the IPv4 literal, not `localhost`: on a dual-stack
|
|
// host that name resolves to ::1 first, while uvicorn binds 0.0.0.0 (IPv4
|
|
// only), so every rewrite would fail to connect.
|
|
const API_BASE_URL = resolveBackendApiBase();
|
|
|
|
const AUTH_ENABLED = parseAuthEnabled(
|
|
process.env.DEEPTUTOR_AUTH_ENABLED ?? process.env.NEXT_PUBLIC_AUTH_ENABLED,
|
|
);
|
|
|
|
// Redirect to the login page, preserving the intended destination in `next`.
|
|
// A present-but-invalid cookie is cleared so the browser stops resending it;
|
|
// when no cookie was sent there is nothing to clear.
|
|
function redirectToLogin(
|
|
req: NextRequest,
|
|
{ clearCookie }: { clearCookie: boolean },
|
|
): NextResponse {
|
|
const loginUrl = req.nextUrl.clone();
|
|
loginUrl.pathname = LOGIN_PATH;
|
|
loginUrl.search = "";
|
|
loginUrl.searchParams.set(
|
|
"next",
|
|
`${req.nextUrl.pathname}${req.nextUrl.search}`,
|
|
);
|
|
const response = NextResponse.redirect(loginUrl);
|
|
if (clearCookie) response.cookies.delete(COOKIE_NAME);
|
|
return response;
|
|
}
|
|
|
|
export function proxy(req: NextRequest): NextResponse {
|
|
const { pathname, search } = req.nextUrl;
|
|
|
|
if (isCodexCallbackPath(pathname)) {
|
|
return NextResponse.rewrite(
|
|
new URL(CODEX_CALLBACK_API_PATH + search, API_BASE_URL),
|
|
);
|
|
}
|
|
|
|
if (isRetiredPagePath(pathname)) {
|
|
return new NextResponse(null, { status: 404 });
|
|
}
|
|
|
|
// 1. Bridge the origin gap: forward backend-relative paths to the API server.
|
|
// This keeps the URL knowledge in one place (the entrypoint + system.json)
|
|
// rather than baked into the frontend bundle.
|
|
if (isBackendPath(pathname)) {
|
|
return NextResponse.rewrite(new URL(pathname + search, API_BASE_URL), {
|
|
request: {
|
|
headers: prepareBackendForwardHeaders(req.headers, {
|
|
allowWebSocketUpgrade:
|
|
req.method === "GET" && isWebSocketPath(pathname),
|
|
}),
|
|
},
|
|
});
|
|
}
|
|
|
|
// 2. Auth gate — multi-user mode only. Disabled by default, and never blocks
|
|
// auth pages, Next.js internals, or public static assets (see
|
|
// isAuthExempt: that exemption is what keeps the logo/banner images
|
|
// loading once login is enabled — issue #599).
|
|
if (!AUTH_ENABLED || isAuthExempt(pathname)) {
|
|
return NextResponse.next();
|
|
}
|
|
|
|
const token = req.cookies.get(COOKIE_NAME)?.value;
|
|
if (classifyToken(token, Date.now()) !== "valid") {
|
|
return redirectToLogin(req, { clearCookie: Boolean(token) });
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
// Run on every request except Next.js internals and the favicon. The /api/*
|
|
// and /ws/* paths are explicitly handled above (rewritten to the backend);
|
|
// large knowledge create/upload requests are handled by dedicated App Router
|
|
// endpoints that stream directly to FastAPI. The collection handler also
|
|
// forwards GET because a route module owns every method at that pathname.
|
|
// Excluding these endpoints here is crucial:
|
|
// merely entering Proxy makes Next clone and cap the multipart body.
|
|
// the browser's /_next/image optimizer requests are excluded here, while the
|
|
// optimizer's loopback fetch for the source image (e.g. /logo.png) is let
|
|
// through the auth gate by isAuthExempt.
|
|
matcher: [
|
|
"/((?!_next/static|_next/image|favicon.ico|api/knowledge-bases(?:$|/[^/]+/upload(?:/|$))).*)",
|
|
],
|
|
};
|