| 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 { KbSettings } from "./pages/KbSettings"; |
| 17 | import { MyKbs } from "./pages/MyKbs"; |
| 18 | import { NotFound } from "./pages/ServerDown"; |
| 19 | import { Ontology } from "./pages/Ontology"; |
| 20 | import { Mappings } from "./pages/Mappings"; |
| 21 | import { Review } from "./pages/Review"; |
| 22 | import { Search } from "./pages/Search"; |
| 23 | import { Settings } from "./pages/Settings"; |
| 24 | import { Shell } from "./pages/Shell"; |
| 25 | |
| 26 | const rootRoute = createRootRoute(); |
| 27 | |
| 28 | const loginRoute = createRoute({ |
| 29 | getParentRoute: () => rootRoute, |
| 30 | path: "/login", |
| 31 | component: Login, |
| 32 | }); |
| 33 | |
| 34 | // 公共法务页:登录前可达 |
| 35 | const privacyRoute = createRoute({ |
| 36 | getParentRoute: () => rootRoute, |
| 37 | path: "/privacy", |
| 38 | component: Privacy, |
| 39 | }); |
| 40 | |
| 41 | const termsRoute = createRoute({ |
| 42 | getParentRoute: () => rootRoute, |
| 43 | path: "/terms", |
| 44 | component: Terms, |
| 45 | }); |
| 46 | |
| 47 | const appRoute = createRoute({ |
| 48 | getParentRoute: () => rootRoute, |
| 49 | id: "app", |
| 50 | component: Shell, |
| 51 | }); |
| 52 | |
| 53 | const indexRoute = createRoute({ |
| 54 | getParentRoute: () => appRoute, |
| 55 | path: "/", |
| 56 | beforeLoad: () => { |
| 57 | // 首页 = 图谱:产品的差异化门面 |
| 58 | throw redirect({ to: "/graph" }); |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | const chatRoute = createRoute({ |
| 63 | getParentRoute: () => appRoute, |
| 64 | path: "/chat", |
| 65 | component: Chat, |
| 66 | }); |
| 67 | |
| 68 | // 会话即路由:/chat/$conversationId 只承载 URL(刷新/分享回到同一会话), |
| 69 | // 渲染仍由父级 Chat 负责——父级在 /chat ↔ /chat/$id 间保持挂载,流式不断 |
| 70 | const chatConversationRoute = createRoute({ |
| 71 | getParentRoute: () => chatRoute, |
| 72 | path: "$conversationId", |
| 73 | component: () => null, |
| 74 | }); |
| 75 | |
| 76 | const searchRoute = createRoute({ |
| 77 | getParentRoute: () => appRoute, |
| 78 | path: "/search", |
| 79 | component: Search, |
| 80 | }); |
| 81 | |
| 82 | const graphRoute = createRoute({ |
| 83 | getParentRoute: () => appRoute, |
| 84 | path: "/graph", |
| 85 | validateSearch: (search: Record<string, unknown>): { entity?: string } => ({ |
| 86 | entity: typeof search.entity === "string" ? search.entity : undefined, |
| 87 | }), |
| 88 | component: Graph, |
| 89 | }); |
| 90 | |
| 91 | const docRoute = createRoute({ |
| 92 | getParentRoute: () => appRoute, |
| 93 | path: "/doc/$docId", |
| 94 | validateSearch: (search: Record<string, unknown>): { chunk?: string } => ({ |
| 95 | chunk: typeof search.chunk === "string" ? search.chunk : undefined, |
| 96 | }), |
| 97 | component: DocViewer, |
| 98 | }); |
| 99 | |
| 100 | const libraryRoute = createRoute({ |
| 101 | getParentRoute: () => appRoute, |
| 102 | path: "/library", |
| 103 | validateSearch: (search: Record<string, unknown>): { src?: string } => ({ |
| 104 | src: typeof search.src === "string" ? search.src : undefined, |
| 105 | }), |
| 106 | component: Library, |
| 107 | }); |
| 108 | |
| 109 | const ontologyRoute = createRoute({ |
| 110 | getParentRoute: () => appRoute, |
| 111 | path: "/ontology", |
| 112 | component: Ontology, |
| 113 | }); |
| 114 | |
| 115 | const mappingsRoute = createRoute({ |
| 116 | getParentRoute: () => appRoute, |
| 117 | path: "/mappings", |
| 118 | component: Mappings, |
| 119 | }); |
| 120 | |
| 121 | const reviewRoute = createRoute({ |
| 122 | getParentRoute: () => appRoute, |
| 123 | path: "/review", |
| 124 | component: Review, |
| 125 | }); |
| 126 | |
| 127 | // 内置文档:公开路由(登录前可读;私有部署离线可用) |
| 128 | const docsIndexRoute = createRoute({ |
| 129 | getParentRoute: () => rootRoute, |
| 130 | path: "/docs", |
| 131 | beforeLoad: () => { |
| 132 | throw redirect({ to: "/docs/$slug", params: { slug: "ingest" } }); |
| 133 | }, |
| 134 | }); |
| 135 | |
| 136 | const docsRoute = createRoute({ |
| 137 | getParentRoute: () => rootRoute, |
| 138 | path: "/docs/$slug", |
| 139 | component: DocsPage, |
| 140 | }); |
| 141 | |
| 142 | // 账户层(Profile / Administration):与 KB 无关,用无 tab 导航的独立壳 |
| 143 | const accountShellRoute = createRoute({ |
| 144 | getParentRoute: () => rootRoute, |
| 145 | id: "account", |
| 146 | component: AccountShell, |
| 147 | }); |
| 148 | |
| 149 | const accountRoute = createRoute({ |
| 150 | getParentRoute: () => accountShellRoute, |
| 151 | path: "/account", |
| 152 | component: Account, |
| 153 | }); |
| 154 | |
| 155 | const myKbsRoute = createRoute({ |
| 156 | getParentRoute: () => accountShellRoute, |
| 157 | path: "/account/kbs", |
| 158 | component: MyKbs, |
| 159 | }); |
| 160 | |
| 161 | const adminRoute = createRoute({ |
| 162 | getParentRoute: () => accountShellRoute, |
| 163 | path: "/admin", |
| 164 | // 深链指定页签(如 KB 数据节的"注册新连接"直达 Data sources) |
| 165 | validateSearch: ( |
| 166 | search: Record<string, unknown>, |
| 167 | ): { tab?: "models" | "members" | "kbs" | "datasources" | "deployment" } => ({ |
| 168 | tab: |
| 169 | search.tab === "models" || |
| 170 | search.tab === "members" || |
| 171 | search.tab === "kbs" || |
| 172 | search.tab === "datasources" || |
| 173 | search.tab === "deployment" |
| 174 | ? search.tab |
| 175 | : undefined, |
| 176 | }), |
| 177 | component: Settings, |
| 178 | }); |
| 179 | |
| 180 | // 旧路径兼容:/settings → /admin |
| 181 | const settingsRoute = createRoute({ |
| 182 | getParentRoute: () => rootRoute, |
| 183 | path: "/settings", |
| 184 | beforeLoad: () => { |
| 185 | throw redirect({ to: "/admin" }); |
| 186 | }, |
| 187 | }); |
| 188 | |
| 189 | const kbSettingsRoute = createRoute({ |
| 190 | getParentRoute: () => appRoute, |
| 191 | path: "/kb-settings", |
| 192 | validateSearch: (search: Record<string, unknown>): { kb?: string } => ({ |
| 193 | kb: typeof search.kb === "string" ? search.kb : undefined, |
| 194 | }), |
| 195 | component: KbSettings, |
| 196 | }); |
| 197 | |
| 198 | const routeTree = rootRoute.addChildren([ |
| 199 | loginRoute, |
| 200 | privacyRoute, |
| 201 | termsRoute, |
| 202 | settingsRoute, |
| 203 | docsIndexRoute, |
| 204 | docsRoute, |
| 205 | accountShellRoute.addChildren([accountRoute, myKbsRoute, adminRoute]), |
| 206 | appRoute.addChildren([ |
| 207 | indexRoute, |
| 208 | chatRoute.addChildren([chatConversationRoute]), |
| 209 | searchRoute, |
| 210 | graphRoute, |
| 211 | docRoute, |
| 212 | libraryRoute, |
| 213 | reviewRoute, |
| 214 | ontologyRoute, |
| 215 | mappingsRoute, |
| 216 | kbSettingsRoute, |
| 217 | ]), |
| 218 | ]); |
| 219 | |
| 220 | export const router = createRouter({ |
| 221 | routeTree, |
| 222 | // 迷失之城:未知路径的惩戒页 |
| 223 | defaultNotFoundComponent: NotFound, |
| 224 | }); |
| 225 | |
| 226 | declare module "@tanstack/react-router" { |
| 227 | interface Register { |
| 228 | router: typeof router; |
| 229 | } |
| 230 | } |