forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.tsx
More file actions
55 lines (48 loc) · 1.6 KB
/
Copy pathlayout.tsx
File metadata and controls
55 lines (48 loc) · 1.6 KB
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
45
46
47
48
49
50
51
52
53
54
55
import fs from "node:fs";
import path from "node:path";
import { headers } from "next/headers";
import { parse } from "yaml";
import type { Metadata } from "next";
type Demo = { id: string; name: string; route?: string };
type Manifest = { demos: Demo[] };
let demoIndex: Map<string, string> | null = null;
function loadDemoIndex(): Map<string, string> {
if (demoIndex) return demoIndex;
const raw = fs.readFileSync(
path.join(process.cwd(), "manifest.yaml"),
"utf8",
);
const manifest = parse(raw) as Manifest;
const map = new Map<string, string>();
for (const demo of manifest.demos ?? []) {
if (demo.route) {
const slug = demo.route.replace(/^\/demos\//, "");
// Last writer wins; manifest has duplicate routes (e.g., hitl-in-chat
// shared by hitl-in-chat and hitl-in-chat-booking). Prefer the first
// entry to keep titles stable.
if (!map.has(slug)) map.set(slug, demo.name);
}
}
demoIndex = map;
return map;
}
function titleCase(slug: string): string {
return slug.replace(/[-_]/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
}
export async function generateMetadata(): Promise<Metadata> {
const h = await headers();
const pathname = h.get("x-pathname") ?? "";
const match = pathname.match(/^\/demos\/([^/]+)/);
const slug = match?.[1];
if (!slug) return { title: "LangChain - Python" };
const index = loadDemoIndex();
const demoName = index.get(slug) ?? titleCase(slug);
return { title: `LangChain - Python - ${demoName}` };
}
export default function DemosLayout({
children,
}: {
children: React.ReactNode;
}) {
return children;
}