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