Elffuss BraidHub

kikocis/utopia public

dev · 9.2 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 // 从实体面板的争议 chip 跳过来:落到对应那一档,并点亮那一张卡(0017 §3)
147 validateSearch: (
148 search: Record<string, unknown>,
149 ): { queue?: string; item?: string } => ({
150 queue: typeof search.queue === "string" ? search.queue : undefined,
151 item: typeof search.item === "string" ? search.item : undefined,
152 }),
153 component: Review,
154});
155
156// 内置文档:公开路由(登录前可读;私有部署离线可用)
157const docsIndexRoute = createRoute({
158 getParentRoute: () => rootRoute,
159 path: "/docs",
160 beforeLoad: () => {
161 throw redirect({ to: "/docs/$slug", params: { slug: "ingest" } });
162 },
163});
164
165const docsRoute = createRoute({
166 getParentRoute: () => rootRoute,
167 path: "/docs/$slug",
168 component: DocsPage,
169});
170
171// 账户层(Profile / Administration):与 KB 无关,用无 tab 导航的独立壳
172const accountShellRoute = createRoute({
173 getParentRoute: () => rootRoute,
174 id: "account",
175 component: AccountShell,
176});
177
178const accountRoute = createRoute({
179 getParentRoute: () => accountShellRoute,
180 path: "/account",
181 component: Account,
182});
183
184const myKbsRoute = createRoute({
185 getParentRoute: () => accountShellRoute,
186 path: "/account/kbs",
187 component: MyKbs,
188});
189
190// 个人令牌(0014):给 agent 的钥匙属于人,所以在账户层,不在库里
191const tokensRoute = createRoute({
192 getParentRoute: () => accountShellRoute,
193 path: "/account/tokens",
194 component: Tokens,
195});
196
197const adminRoute = createRoute({
198 getParentRoute: () => accountShellRoute,
199 path: "/admin",
200 // 深链指定页签(如 KB 数据节的"注册新连接"直达 Data sources)
201 validateSearch: (
202 search: Record<string, unknown>,
203 ): { tab?: "models" | "members" | "kbs" | "datasources" | "deployment" } => ({
204 tab:
205 search.tab === "models" ||
206 search.tab === "members" ||
207 search.tab === "kbs" ||
208 search.tab === "datasources" ||
209 search.tab === "deployment"
210 ? search.tab
211 : undefined,
212 }),
213 component: Settings,
214});
215
216// 旧路径兼容:/settings → /admin
217const settingsRoute = createRoute({
218 getParentRoute: () => rootRoute,
219 path: "/settings",
220 beforeLoad: () => {
221 throw redirect({ to: "/admin" });
222 },
223});
224
225const kbSettingsRoute = createRoute({
226 getParentRoute: () => kbRoute,
227 path: "settings",
228 component: KbSettings,
229});
230
231/* 旧路径兼容:`/graph` 这类不带库的地址仍然可用,解析出该去哪个库再跳。
232 **不做成 beforeLoad 重定向**——那时候库列表还没取回来,localStorage 里
233 也可能什么都没有(新设备、清过缓存),只能等 useKb 解析出来 */
234/* **逐条写出来,不用工厂函数**:工厂里的 path 是 string,类型系统认不出
235 字面量,别处 `redirect({ to: "/graph" })` 就通不过。啰嗦换类型安全 */
236const legacyGraphRoute = createRoute({
237 getParentRoute: () => appRoute,
238 path: "/graph",
239 component: () => <KbRedirect page="graph" />,
240});
241const legacySearchRoute = createRoute({
242 getParentRoute: () => appRoute,
243 path: "/search",
244 component: () => <KbRedirect page="search" />,
245});
246const legacyChatRoute = createRoute({
247 getParentRoute: () => appRoute,
248 path: "/chat",
249 component: () => <KbRedirect page="chat" />,
250});
251const legacyLibraryRoute = createRoute({
252 getParentRoute: () => appRoute,
253 path: "/library",
254 component: () => <KbRedirect page="library" />,
255});
256const legacyOntologyRoute = createRoute({
257 getParentRoute: () => appRoute,
258 path: "/ontology",
259 component: () => <KbRedirect page="ontology" />,
260});
261const legacyMappingsRoute = createRoute({
262 getParentRoute: () => appRoute,
263 path: "/mappings",
264 component: () => <KbRedirect page="mappings" />,
265});
266const legacyReviewRoute = createRoute({
267 getParentRoute: () => appRoute,
268 path: "/review",
269 component: () => <KbRedirect page="review" />,
270});
271const legacyKbSettingsRoute = createRoute({
272 getParentRoute: () => appRoute,
273 path: "/kb-settings",
274 component: () => <KbRedirect page="settings" />,
275});
276
277const routeTree = rootRoute.addChildren([
278 loginRoute,
279 privacyRoute,
280 termsRoute,
281 settingsRoute,
282 docsIndexRoute,
283 docsRoute,
284 accountShellRoute.addChildren([accountRoute, myKbsRoute, tokensRoute, adminRoute]),
285 appRoute.addChildren([
286 indexRoute,
287 legacyGraphRoute,
288 legacySearchRoute,
289 legacyChatRoute,
290 legacyLibraryRoute,
291 legacyOntologyRoute,
292 legacyMappingsRoute,
293 legacyReviewRoute,
294 legacyKbSettingsRoute,
295 kbRoute.addChildren([
296 chatRoute.addChildren([chatConversationRoute]),
297 searchRoute,
298 graphRoute,
299 docRoute,
300 libraryRoute,
301 reviewRoute,
302 ontologyRoute,
303 mappingsRoute,
304 kbSettingsRoute,
305 ]),
306 ]),
307]);
308
309export const router = createRouter({
310 routeTree,
311 // 迷失之城:未知路径的惩戒页
312 defaultNotFoundComponent: NotFound,
313});
314
315declare module "@tanstack/react-router" {
316 interface Register {
317 router: typeof router;
318 }
319}