forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
154 lines (134 loc) · 5.19 KB
/
Copy pathmiddleware.ts
File metadata and controls
154 lines (134 loc) · 5.19 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
// All framework slugs for pattern-based redirects
const FRAMEWORKS = [
"langgraph",
"deepagents",
"adk",
"agno",
"crewai-flows",
"pydantic-ai",
"llamaindex",
"mastra",
"agent-spec",
"ag2",
"microsoft-agent-framework",
"aws-strands",
"a2a",
"built-in-agent",
];
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Common redirects for broken links
const redirects: Record<string, string> = {
// Old coagents paths
"/coagents": "/langgraph",
"/coagents/quickstart": "/langgraph/quickstart",
"/coagents/guides": "/langgraph/guides",
"/coagents/shared-state": "/langgraph/shared-state",
"/coagents/human-in-the-loop": "/langgraph/human-in-the-loop",
"/coagents/multi-agent-flows": "/langgraph/multi-agent-flows",
"/coagents/persistence": "/langgraph/threads",
"/coagents/advanced": "/langgraph/advanced",
"/coagents/videos": "/langgraph/videos",
"/coagents/tutorials": "/langgraph/tutorials",
"/coagents/concepts": "/langgraph/concepts",
"/coagents/frontend-actions": "/langgraph/frontend-tools",
"/coagents/generative-ui": "/langgraph/generative-ui",
// Common typos and variations
"/direct-to-llm/guide": "/built-in-agent/guides",
"/langgraph/guide": "/langgraph/guides",
"/mastra/guide": "/mastra/guides",
"/agno/guide": "/agno/guides",
"/llamaindex/guide": "/llamaindex/guides",
"/crewai-flows/guide": "/crewai-flows/guides",
"/ag2/guide": "/ag2/guides",
"/pydantic-ai/guide": "/pydantic-ai/guides",
"/adk/guide": "/adk/guides",
// API reference variations
"/api": "/reference",
"/docs/api": "/reference",
"/api-reference": "/reference",
// Quickstart variations
"/getting-started": "/quickstart",
"/start": "/quickstart",
// Frontend actions → frontend tools (renamed in restructure)
"/frontend-actions": "/frontend-tools",
// Generative UI directory → first page
"/generative-ui": "/generative-ui/your-components/display-only",
"/generative-ui/display": "/generative-ui/your-components/display-only",
"/generative-ui/interactive": "/generative-ui/your-components/interactive",
// Old root page names → new names
"/agentic-chat-ui": "/prebuilt-components",
"/headless": "/custom-look-and-feel/headless-ui",
"/coding-agent-setup": "/build-with-agents",
"/copilot-suggestions": "/prebuilt-components",
"/direct-to-llm": "/built-in-agent",
"/builtin-agent": "/built-in-agent",
// Contributing paths
"/contributing/code-contributions/package-linking":
"/shared/contributing/code-contributions/package-linking",
};
// Check for exact matches
if (redirects[pathname]) {
return NextResponse.redirect(new URL(redirects[pathname], request.url));
}
// Check for pattern-based redirects
if (pathname.startsWith("/coagents/")) {
const newPath = pathname.replace("/coagents/", "/langgraph/");
return NextResponse.redirect(new URL(newPath, request.url));
}
// Per-framework pattern redirects (docs restructure 2026-02)
for (const fw of FRAMEWORKS) {
const prefix = `/${fw}/`;
if (!pathname.startsWith(prefix)) continue;
const rest = pathname.slice(prefix.length);
// Renamed pages
const renames: Record<string, string> = {
"agentic-chat-ui": "prebuilt-components",
"use-agent-hook": "programmatic-control",
"frontend-actions": "frontend-tools",
"vibe-coding-mcp": "build-with-agents",
// Old generative-ui pages → new locations
"generative-ui/agentic": "generative-ui/your-components/display-only",
"generative-ui/backend-tools": "generative-ui/tool-rendering",
"generative-ui/frontend-tools": "frontend-tools",
"generative-ui/render-only": "generative-ui/your-components/display-only",
"generative-ui/tool-based": "generative-ui/tool-rendering",
// Old custom-look-and-feel pages
"custom-look-and-feel/bring-your-own-components":
"custom-look-and-feel/slots",
"custom-look-and-feel/customize-built-in-ui-components":
"custom-look-and-feel/slots",
"custom-look-and-feel/markdown-rendering": "custom-look-and-feel/slots",
};
if (renames[rest]) {
return NextResponse.redirect(
new URL(`/${fw}/${renames[rest]}`, request.url),
);
}
// Concepts directory → framework root
if (rest.startsWith("concepts/") || rest === "concepts") {
return NextResponse.redirect(new URL(`/${fw}`, request.url));
}
break; // Only match one framework
}
// Handle guide -> guides redirects
if (pathname.includes("/guide") && !pathname.includes("/guides")) {
const newPath = pathname.replace("/guide", "/guides");
return NextResponse.redirect(new URL(newPath, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api|ingest|_next/static|_next/image|favicon.ico).*)",
],
};