forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink-validation.ts
More file actions
166 lines (149 loc) · 3.82 KB
/
Copy pathlink-validation.ts
File metadata and controls
166 lines (149 loc) · 3.82 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
155
156
157
158
159
160
161
162
163
164
165
166
import { source } from "@/app/source";
export interface LinkSuggestion {
href: string;
title: string;
description?: string;
confidence: number;
}
export interface BrokenLinkInfo {
originalHref: string;
suggestions: LinkSuggestion[];
isExternal: boolean;
}
/**
* Validates if a link is broken and provides suggestions
*/
export function validateLink(href: string): BrokenLinkInfo {
const isExternal =
href.startsWith("http") ||
href.startsWith("mailto:") ||
href.startsWith("tel:");
if (isExternal) {
return {
originalHref: href,
suggestions: [],
isExternal: true,
};
}
// For internal links, check if the page exists
const normalizedHref = href.startsWith("/") ? href.slice(1) : href;
const slug = normalizedHref.split("/").filter(Boolean);
try {
const page = source.getPage(slug);
if (page) {
return {
originalHref: href,
suggestions: [],
isExternal: false,
};
}
} catch (error) {
// Page doesn't exist
}
// Generate suggestions based on the broken link
const suggestions = generateSuggestions(href, slug);
return {
originalHref: href,
suggestions,
isExternal: false,
};
}
/**
* Generates suggestions for a broken link
*/
function generateSuggestions(
originalHref: string,
slug: string[],
): LinkSuggestion[] {
const suggestions: LinkSuggestion[] = [];
// Get all available pages for fuzzy matching
const allPages = getAllPages();
if (slug.length === 0) {
// If it's just a root link, suggest main sections
suggestions.push(
{
href: "/direct-to-llm",
title: "Direct to LLM",
description: "Build copilots with any LLM",
confidence: 0.8,
},
{
href: "/langgraph",
title: "LangChain",
description: "Agentic workflows and state machines",
confidence: 0.8,
},
{
href: "/mastra",
title: "Mastra",
description: "Multi-agent orchestration",
confidence: 0.8,
},
{
href: "/reference",
title: "API Reference",
description: "Complete API documentation",
confidence: 0.8,
},
);
return suggestions;
}
// Try to find similar pages
const lastSegment = slug[slug.length - 1];
const similarPages = allPages.filter((page) => {
const pageSlug = page.slugs.join("/");
return (
pageSlug.includes(lastSegment) ||
page.data.title?.toLowerCase().includes(lastSegment.toLowerCase())
);
});
similarPages.slice(0, 3).forEach((page) => {
suggestions.push({
href: `/${page.slugs.join("/")}`,
title: page.data.title || "Untitled",
description: page.data.description,
confidence: 0.7,
});
});
// If no similar pages found, suggest main sections
if (suggestions.length === 0) {
const firstSegment = slug[0];
if (firstSegment) {
suggestions.push(
{
href: `/${firstSegment}`,
title: `${firstSegment} Documentation`,
confidence: 0.6,
},
{ href: "/", title: "Home", confidence: 0.5 },
);
}
}
return suggestions;
}
/**
* Gets all available pages (this would need to be implemented based on your source structure)
*/
function getAllPages(): Array<{
slugs: string[];
data: { title?: string; description?: string };
}> {
// This is a simplified version - you'd need to implement this based on your source structure
try {
// This would need to be implemented to get all pages from your source
return [];
} catch (error) {
return [];
}
}
/**
* Creates a link suggestion component
*/
export function createLinkSuggestion(suggestion: LinkSuggestion) {
return {
href: suggestion.href,
title: suggestion.title,
description: suggestion.description,
confidence: suggestion.confidence,
};
}