ktx/docs-site/proxy.ts
Andrey Avtomonov 911cfdc741 docs: rewrite Semantic Querying concept with imperative-vs-declarative diagram
Reframe semantic-layer-internals.mdx around the contract the semantic
layer offers an agent: declare what you want (a Semantic Query), KTX
figures out how to compute it. Replaces the old "Context-Aware SQL"
framing with a clear imperative-vs-declarative narrative.

Adds a React Flow component (semantic-layer-flow.tsx) that contrasts a
buggy 4-table agent-authored SQL (chasm trap, LEFT-JOIN-in-WHERE,
hardcoded DATE_TRUNC) against the chasm-safe per-fact CTE SQL the
planner actually emits, including the outer GROUP BY over the requested
dimensions. Both lanes converge into a shared warehouse node and each
SQL card now has parallel bullet notes (failures on the left, KTX
behavior on the right).

Side fixes bundled in:
- include the /ktx basePath in the favicon metadata so the icon resolves
  under the production prefix
- migrate docs-site/middleware.ts to docs-site/proxy.ts (Next 16 rename)
- redirect / to /ktx/docs/getting-started/introduction so the apex docs
  URL works
- add tests covering the apex redirect, the favicon basePath, and the
  middleware-to-proxy rename
- propagate the Semantic Query terminology across the ktx-sl CLI
  reference, the context-layer concept page, and the agent-clients /
  primary-sources integration pages
2026-05-19 19:34:04 +02:00

52 lines
1.5 KiB
TypeScript

import { NextResponse, type NextRequest } from "next/server";
const markdownMimeTypes = new Set([
"text/markdown",
"text/x-markdown",
"application/markdown",
]);
export function proxy(request: NextRequest) {
if (!isMarkdownPreferred(request.headers.get("accept"))) {
return NextResponse.next();
}
const { pathname } = request.nextUrl;
const docsIndex = pathname.indexOf("/docs/");
if (docsIndex < 0 || pathname.endsWith(".md")) {
return NextResponse.next();
}
const rewriteUrl = request.nextUrl.clone();
rewriteUrl.pathname = `${pathname.slice(0, docsIndex)}/llms.mdx${pathname.slice(docsIndex)}`;
return NextResponse.rewrite(rewriteUrl);
}
export const config = {
matcher: ["/docs/:path*"],
};
function isMarkdownPreferred(acceptHeader: string | null) {
if (!acceptHeader) return false;
const accepted = acceptHeader
.split(",")
.map((entry, index) => {
const [type = "", ...parameters] = entry.trim().split(";");
const quality = parameters
.map((parameter) => parameter.trim())
.find((parameter) => parameter.startsWith("q="));
return {
type: type.trim().toLowerCase(),
quality: quality ? Number.parseFloat(quality.slice(2)) : 1,
index,
};
})
.filter((entry) => Number.isFinite(entry.quality) && entry.quality > 0)
.sort((a, b) => b.quality - a.quality || a.index - b.index);
const preferred = accepted[0]?.type;
return preferred ? markdownMimeTypes.has(preferred) : false;
}