| 1 | import { |
| 2 | createRootRoute, |
| 3 | createRoute, |
| 4 | createRouter, |
| 5 | redirect, |
| 6 | } from "@tanstack/react-router"; |
| 7 | import { Account } from "./pages/Account"; |
| 8 | import { AccountShell } from "./pages/AccountShell"; |
| 9 | import { Tokens } from "./pages/Tokens"; |
| 10 | import { Chat } from "./pages/Chat"; |
| 11 | import { DocViewer } from "./pages/DocViewer"; |
| 12 | import { DocsPage } from "./pages/Docs"; |
| 13 | import { Graph } from "./pages/Graph"; |
| 14 | import { Library } from "./pages/Library"; |
| 15 | import { Login } from "./pages/Login"; |
| 16 | import { Privacy, Terms } from "./pages/Legal"; |
| 17 | import { KbRedirect, KbScope } from "./pages/KbScope"; |
| 18 | import { KbSettings } from "./pages/KbSettings"; |
| 19 | import { MyKbs } from "./pages/MyKbs"; |
| 20 | import { NotFound } from "./pages/ServerDown"; |
| 21 | import { Ontology } from "./pages/Ontology"; |
| 22 | import { Mappings } from "./pages/Mappings"; |
| 23 | import { Review } from "./pages/Review"; |
| 24 | import { Search } from "./pages/Search"; |
| 25 | import { Settings } from "./pages/Settings"; |
| 26 | import { Shell } from "./pages/Shell"; |
| 27 | |
| 28 | const rootRoute = createRootRoute(); |
| 29 | |
| 30 | const loginRoute = createRoute({ |
| 31 | getParentRoute: () => rootRoute, |
| 32 | path: "/login", |
| 33 | component: Login, |
| 34 | }); |
| 35 | |
| 36 | // 公共法务页:登录前可达 |
| 37 | const privacyRoute = createRoute({ |
| 38 | getParentRoute: () => rootRoute, |
| 39 | path: "/privacy", |
| 40 | component: Privacy, |
| 41 | }); |
| 42 | |
| 43 | const termsRoute = createRoute({ |
| 44 | getParentRoute: () => rootRoute, |
| 45 | path: "/terms", |
| 46 | component: Terms, |
| 47 | }); |
| 48 | |
| 49 | const appRoute = createRoute({ |
| 50 | getParentRoute: () => rootRoute, |
| 51 | id: "app", |
| 52 | component: Shell, |
| 53 | }); |
| 54 | |
| 55 | const 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 */ |
| 67 | const kbRoute = createRoute({ |
| 68 | getParentRoute: () => appRoute, |
| 69 | path: "/kb/$kbId", |
| 70 | component: KbScope, |
| 71 | }); |
| 72 | |
| 73 | const chatRoute = createRoute({ |
| 74 | getParentRoute: () => kbRoute, |
| 75 | path: "chat", |
| 76 | component: Chat, |
| 77 | }); |
| 78 | |
| 79 | // 会话即路由:/chat/$conversationId 只承载 URL(刷新/分享回到同一会话), |
| 80 | // 渲染仍由父级 Chat 负责——父级在 /chat ↔ /chat/$id 间保持挂载,流式不断 |
| 81 | const chatConversationRoute = createRoute({ |
| 82 | getParentRoute: () => chatRoute, |
| 83 | path: "$conversationId", |
| 84 | component: () => null, |
| 85 | }); |
| 86 | |
| 87 | const searchRoute = createRoute({ |
| 88 | getParentRoute: () => kbRoute, |
| 89 | path: "search", |
| 90 | component: Search, |
| 91 | }); |
| 92 | |
| 93 | const 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 | |
| 113 | const 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 | |
| 122 | const 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 | |
| 131 | const ontologyRoute = createRoute({ |
| 132 | getParentRoute: () => kbRoute, |
| 133 | path: "ontology", |
| 134 | component: Ontology, |
| 135 | }); |
| 136 | |
| 137 | const mappingsRoute = createRoute({ |
| 138 | getParentRoute: () => kbRoute, |
| 139 | path: "mappings", |
| 140 | component: Mappings, |
| 141 | }); |
| 142 | |
| 143 | const 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 | // 内置文档:公开路由(登录前可读;私有部署离线可用) |
| 157 | const docsIndexRoute = createRoute({ |
| 158 | getParentRoute: () => rootRoute, |
| 159 | path: "/docs", |
| 160 | beforeLoad: () => { |
| 161 | throw redirect({ to: "/docs/$slug", params: { slug: "ingest" } }); |
| 162 | }, |
| 163 | }); |
| 164 | |
| 165 | const docsRoute = createRoute({ |
| 166 | getParentRoute: () => rootRoute, |
| 167 | path: "/docs/$slug", |
| 168 | component: DocsPage, |
| 169 | }); |
| 170 | |
| 171 | // 账户层(Profile / Administration):与 KB 无关,用无 tab 导航的独立壳 |
| 172 | const accountShellRoute = createRoute({ |
| 173 | getParentRoute: () => rootRoute, |
| 174 | id: "account", |
| 175 | component: AccountShell, |
| 176 | }); |
| 177 | |
| 178 | const accountRoute = createRoute({ |
| 179 | getParentRoute: () => accountShellRoute, |
| 180 | path: "/account", |
| 181 | component: Account, |
| 182 | }); |
| 183 | |
| 184 | const myKbsRoute = createRoute({ |
| 185 | getParentRoute: () => accountShellRoute, |
| 186 | path: "/account/kbs", |
| 187 | component: MyKbs, |
| 188 | }); |
| 189 | |
| 190 | // 个人令牌(0014):给 agent 的钥匙属于人,所以在账户层,不在库里 |
| 191 | const tokensRoute = createRoute({ |
| 192 | getParentRoute: () => accountShellRoute, |
| 193 | path: "/account/tokens", |
| 194 | component: Tokens, |
| 195 | }); |
| 196 | |
| 197 | const 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 |
| 217 | const settingsRoute = createRoute({ |
| 218 | getParentRoute: () => rootRoute, |
| 219 | path: "/settings", |
| 220 | beforeLoad: () => { |
| 221 | throw redirect({ to: "/admin" }); |
| 222 | }, |
| 223 | }); |
| 224 | |
| 225 | const 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" })` 就通不过。啰嗦换类型安全 */ |
| 236 | const legacyGraphRoute = createRoute({ |
| 237 | getParentRoute: () => appRoute, |
| 238 | path: "/graph", |
| 239 | component: () => <KbRedirect page="graph" />, |
| 240 | }); |
| 241 | const legacySearchRoute = createRoute({ |
| 242 | getParentRoute: () => appRoute, |
| 243 | path: "/search", |
| 244 | component: () => <KbRedirect page="search" />, |
| 245 | }); |
| 246 | const legacyChatRoute = createRoute({ |
| 247 | getParentRoute: () => appRoute, |
| 248 | path: "/chat", |
| 249 | component: () => <KbRedirect page="chat" />, |
| 250 | }); |
| 251 | const legacyLibraryRoute = createRoute({ |
| 252 | getParentRoute: () => appRoute, |
| 253 | path: "/library", |
| 254 | component: () => <KbRedirect page="library" />, |
| 255 | }); |
| 256 | const legacyOntologyRoute = createRoute({ |
| 257 | getParentRoute: () => appRoute, |
| 258 | path: "/ontology", |
| 259 | component: () => <KbRedirect page="ontology" />, |
| 260 | }); |
| 261 | const legacyMappingsRoute = createRoute({ |
| 262 | getParentRoute: () => appRoute, |
| 263 | path: "/mappings", |
| 264 | component: () => <KbRedirect page="mappings" />, |
| 265 | }); |
| 266 | const legacyReviewRoute = createRoute({ |
| 267 | getParentRoute: () => appRoute, |
| 268 | path: "/review", |
| 269 | component: () => <KbRedirect page="review" />, |
| 270 | }); |
| 271 | const legacyKbSettingsRoute = createRoute({ |
| 272 | getParentRoute: () => appRoute, |
| 273 | path: "/kb-settings", |
| 274 | component: () => <KbRedirect page="settings" />, |
| 275 | }); |
| 276 | |
| 277 | const 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 | |
| 309 | export const router = createRouter({ |
| 310 | routeTree, |
| 311 | // 迷失之城:未知路径的惩戒页 |
| 312 | defaultNotFoundComponent: NotFound, |
| 313 | }); |
| 314 | |
| 315 | declare module "@tanstack/react-router" { |
| 316 | interface Register { |
| 317 | router: typeof router; |
| 318 | } |
| 319 | } |