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
117 lines (97 loc) · 3.98 KB
/
Copy pathmiddleware.ts
File metadata and controls
117 lines (97 loc) · 3.98 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
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/persistence',
'/coagents/advanced': '/langgraph/advanced',
'/coagents/videos': '/langgraph/videos',
'/coagents/tutorials': '/langgraph/tutorials',
'/coagents/concepts': '/langgraph/concepts',
'/coagents/frontend-actions': '/langgraph/frontend-actions',
'/coagents/generative-ui': '/langgraph/generative-ui',
// Common typos and variations
'/direct-to-llm/guide': '/direct-to-llm/guides',
'/langgraph/guide': '/langgraph/guides',
'/mastra/guide': '/mastra/guides',
'/agno/guide': '/agno/guides',
'/llamaindex/guide': '/llamaindex/guides',
'/crewai-crews/guide': '/crewai-crews/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
'/quickstart': '/direct-to-llm/guides/quickstart',
'/getting-started': '/direct-to-llm/guides/quickstart',
'/start': '/direct-to-llm/guides/quickstart',
// Frontend tools variations
'/frontend-tools': '/direct-to-llm/guides/frontend-actions',
'/frontend-actions': '/direct-to-llm/guides/frontend-actions',
// 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));
}
// 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));
}
// Handle quickstart redirects for specific frameworks
if (pathname === '/quickstart') {
return NextResponse.redirect(new URL('/direct-to-llm/guides/quickstart', request.url));
}
// Check for partial matches and suggest alternatives
const suggestions = generateSuggestions(pathname);
if (suggestions.length > 0) {
// For now, we'll let the 404 page handle suggestions
// In the future, we could redirect to a special 404 page with suggestions
}
return NextResponse.next();
}
function generateSuggestions(pathname: string): string[] {
const suggestions: string[] = [];
// Common patterns that might be typos
if (pathname.includes('guide') && !pathname.includes('guides')) {
suggestions.push(pathname.replace('guide', 'guides'));
}
if (pathname.includes('coagents')) {
suggestions.push(pathname.replace('coagents', 'langgraph'));
}
if (pathname.includes('/api') && !pathname.includes('/reference')) {
suggestions.push('/reference');
}
return suggestions;
}
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|_next/static|_next/image|favicon.ico).*)',
],
};