Elffuss BraidHub

kikocis/utopia public

49653d3b6889 · 8.9 KiBRawPermalink
1import {
2 createRootRoute,
3 createRoute,
4 createRouter,
5 redirect,
6} from "@tanstack/react-router";
7import { Account } from "./pages/Account";
8import { AccountShell } from "./pages/AccountShell";
9import { Tokens } from "./pages/Tokens";
10import { Chat } from "./pages/Chat";
11import { DocViewer } from "./pages/DocViewer";
12import { DocsPage } from "./pages/Docs";
13import { Graph } from "./pages/Graph";
14import { Library } from "./pages/Library";
15import { Login } from "./pages/Login";
16import { Privacy, Terms } from "./pages/Legal";
17import { KbRedirect, KbScope } from "./pages/KbScope";
18import { KbSettings } from "./pages/KbSettings";
19import { MyKbs } from "./pages/MyKbs";
20import { NotFound } from "./pages/ServerDown";
21import { Ontology } from "./pages/Ontology";
22import { Mappings } from "./pages/Mappings";
23import { Review } from "./pages/Review";
24import { Search } from "./pages/Search";
25import { Settings } from "./pages/Settings";
26import { Shell } from "./pages/Shell";
27
28const rootRoute = createRootRoute();
29
30const loginRoute = createRoute({
31 getParentRoute: () => rootRoute,
32 path: "/login",
33 component: Login,
34});
35
36// 公共法务页:登录前可达
37const privacyRoute = createRoute({
38 getParentRoute: () => rootRoute,
39 path: "/privacy",
40 component: Privacy,
41});
42
43const termsRoute = createRoute({
44 getParentRoute: () => rootRoute,
45 path: "/terms",
46 component: Terms,
47});
48
49const appRoute = createRoute({
50 getParentRoute: () => rootRoute,
51 id: "app",
52 component: Shell,
53});
54
55const indexRoute = createRoute({
56 getParentRoute: () => appRoute,
57 path: "/",
58 beforeLoad: () => {
59 // 首页 = 图谱:产品的差异化门面
60 throw redirect({ to: "/graph" });
61 },
62});
63
64/* 知识库作用域。**库是容器不是筛选条件**——底下这些页面全都属于某一个库,
65 路径表达包含关系,路由器也就替我们兜住了「忘了带库」这类错误:
66 `/kb/$kbId/search` 没有 id 根本构造不出来。理由详见 pages/KbScope.tsx */
67const kbRoute = createRoute({
68 getParentRoute: () => appRoute,
69 path: "/kb/$kbId",
70 component: KbScope,
71});
72
73const chatRoute = createRoute({
74 getParentRoute: () => kbRoute,
75 path: "chat",
76 component: Chat,
77});
78
79// 会话即路由:/chat/$conversationId 只承载 URL(刷新/分享回到同一会话),
80// 渲染仍由父级 Chat 负责——父级在 /chat ↔ /chat/$id 间保持挂载,流式不断
81const chatConversationRoute = createRoute({
82 getParentRoute: () => chatRoute,
83 path: "$conversationId",
84 component: () => null,
85});
86
87const searchRoute = createRoute({
88 getParentRoute: () => kbRoute,
89 path: "search",
90 component: Search,
91});
92
93const graphRoute = createRoute({
94 getParentRoute: () => kbRoute,
95 path: "graph",
96 /* 图谱页的可分享状态。**三个都是"你在看什么",不是"你怎么看"**——
97 所以档位(画多少个)刻意不进 URL:那是本地观感,换台机器不该跟着走。
98
99 - entity:选中了谁
100 - focus:是否处在某个实体的邻域(与"在全图里选中"是两个画面)
101 - at:时间轴停在哪一刻。**这条最不能少**——这产品的卖点就是
102 "看某个时刻的世界",不带时刻的链接把最有意思的那部分丢了 */
103 validateSearch: (
104 search: Record<string, unknown>,
105 ): { entity?: string; focus?: string; at?: string } => ({
106 entity: typeof search.entity === "string" ? search.entity : undefined,
107 focus: typeof search.focus === "string" ? search.focus : undefined,
108 at: typeof search.at === "string" ? search.at : undefined,
109 }),
110 component: Graph,
111});
112
113const docRoute = createRoute({
114 getParentRoute: () => kbRoute,
115 path: "doc/$docId",
116 validateSearch: (search: Record<string, unknown>): { chunk?: string } => ({
117 chunk: typeof search.chunk === "string" ? search.chunk : undefined,
118 }),
119 component: DocViewer,
120});
121
122const libraryRoute = createRoute({
123 getParentRoute: () => kbRoute,
124 path: "library",
125 validateSearch: (search: Record<string, unknown>): { src?: string } => ({
126 src: typeof search.src === "string" ? search.src : undefined,
127 }),
128 component: Library,
129});
130
131const ontologyRoute = createRoute({
132 getParentRoute: () => kbRoute,
133 path: "ontology",
134 component: Ontology,
135});
136
137const mappingsRoute = createRoute({
138 getParentRoute: () => kbRoute,
139 path: "mappings",
140 component: Mappings,
141});
142
143const reviewRoute = createRoute({
144 getParentRoute: () => kbRoute,
145 path: "review",
146 component: Review,
147});
148
149// 内置文档:公开路由(登录前可读;私有部署离线可用)
150const docsIndexRoute = createRoute({
151 getParentRoute: () => rootRoute,
152 path: "/docs",
153 beforeLoad: () => {
154 throw redirect({ to: "/docs/$slug", params: { slug: "ingest" } });
155 },
156});
157
158const docsRoute = createRoute({
159 getParentRoute: () => rootRoute,
160 path: "/docs/$slug",
161 component: DocsPage,
162});
163
164// 账户层(Profile / Administration):与 KB 无关,用无 tab 导航的独立壳
165const accountShellRoute = createRoute({
166 getParentRoute: () => rootRoute,
167 id: "account",
168 component: AccountShell,
169});
170
171const accountRoute = createRoute({
172 getParentRoute: () => accountShellRoute,
173 path: "/account",
174 component: Account,
175});
176
177const myKbsRoute = createRoute({
178 getParentRoute: () => accountShellRoute,
179 path: "/account/kbs",
180 component: MyKbs,
181});
182
183// 个人令牌(0014):给 agent 的钥匙属于人,所以在账户层,不在库里
184const tokensRoute = createRoute({
185 getParentRoute: () => accountShellRoute,
186 path: "/account/tokens",
187 component: Tokens,
188});
189
190const adminRoute = createRoute({
191 getParentRoute: () => accountShellRoute,
192 path: "/admin",
193 // 深链指定页签(如 KB 数据节的"注册新连接"直达 Data sources)
194 validateSearch: (
195 search: Record<string, unknown>,
196 ): { tab?: "models" | "members" | "kbs" | "datasources" | "deployment" } => ({
197 tab:
198 search.tab === "models" ||
199 search.tab === "members" ||
200 search.tab === "kbs" ||
201 search.tab === "datasources" ||
202 search.tab === "deployment"
203 ? search.tab
204 : undefined,
205 }),
206 component: Settings,
207});
208
209// 旧路径兼容:/settings → /admin
210const settingsRoute = createRoute({
211 getParentRoute: () => rootRoute,
212 path: "/settings",
213 beforeLoad: () => {
214 throw redirect({ to: "/admin" });
215 },
216});
217
218const kbSettingsRoute = createRoute({
219 getParentRoute: () => kbRoute,
220 path: "settings",
221 component: KbSettings,
222});
223
224/* 旧路径兼容:`/graph` 这类不带库的地址仍然可用,解析出该去哪个库再跳。
225 **不做成 beforeLoad 重定向**——那时候库列表还没取回来,localStorage 里
226 也可能什么都没有(新设备、清过缓存),只能等 useKb 解析出来 */
227/* **逐条写出来,不用工厂函数**:工厂里的 path 是 string,类型系统认不出
228 字面量,别处 `redirect({ to: "/graph" })` 就通不过。啰嗦换类型安全 */
229const legacyGraphRoute = createRoute({
230 getParentRoute: () => appRoute,
231 path: "/graph",
232 component: () => <KbRedirect page="graph" />,
233});
234const legacySearchRoute = createRoute({
235 getParentRoute: () => appRoute,
236 path: "/search",
237 component: () => <KbRedirect page="search" />,
238});
239const legacyChatRoute = createRoute({
240 getParentRoute: () => appRoute,
241 path: "/chat",
242 component: () => <KbRedirect page="chat" />,
243});
244const legacyLibraryRoute = createRoute({
245 getParentRoute: () => appRoute,
246 path: "/library",
247 component: () => <KbRedirect page="library" />,
248});
249const legacyOntologyRoute = createRoute({
250 getParentRoute: () => appRoute,
251 path: "/ontology",
252 component: () => <KbRedirect page="ontology" />,
253});
254const legacyMappingsRoute = createRoute({
255 getParentRoute: () => appRoute,
256 path: "/mappings",
257 component: () => <KbRedirect page="mappings" />,
258});
259const legacyReviewRoute = createRoute({
260 getParentRoute: () => appRoute,
261 path: "/review",
262 component: () => <KbRedirect page="review" />,
263});
264const legacyKbSettingsRoute = createRoute({
265 getParentRoute: () => appRoute,
266 path: "/kb-settings",
267 component: () => <KbRedirect page="settings" />,
268});
269
270const routeTree = rootRoute.addChildren([
271 loginRoute,
272 privacyRoute,
273 termsRoute,
274 settingsRoute,
275 docsIndexRoute,
276 docsRoute,
277 accountShellRoute.addChildren([accountRoute, myKbsRoute, tokensRoute, adminRoute]),
278 appRoute.addChildren([
279 indexRoute,
280 legacyGraphRoute,
281 legacySearchRoute,
282 legacyChatRoute,
283 legacyLibraryRoute,
284 legacyOntologyRoute,
285 legacyMappingsRoute,
286 legacyReviewRoute,
287 legacyKbSettingsRoute,
288 kbRoute.addChildren([
289 chatRoute.addChildren([chatConversationRoute]),
290 searchRoute,
291 graphRoute,
292 docRoute,
293 libraryRoute,
294 reviewRoute,
295 ontologyRoute,
296 mappingsRoute,
297 kbSettingsRoute,
298 ]),
299 ]),
300]);
301
302export const router = createRouter({
303 routeTree,
304 // 迷失之城:未知路径的惩戒页
305 defaultNotFoundComponent: NotFound,
306});
307
308declare module "@tanstack/react-router" {
309 interface Register {
310 router: typeof router;
311 }
312}