index.js 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { build } = require('esbuild');
  5. const { convertV8ToGraph } = require('./convert_v8_to_graph');
  6. const { convertV8ToGraphV2, convertV8ToGraphSimplified } = require('./convert_v8_to_graph_v2');
  7. // 读取命令行参数
  8. const args = process.argv.slice(2);
  9. if (args.length === 0) {
  10. console.error('Usage: node index.js <path-to-run_context.json> [output.html] [--simplified]');
  11. process.exit(1);
  12. }
  13. const inputFile = args[0];
  14. const outputFile = args[1] || 'query_graph_output.html';
  15. const useSimplified = args.includes('--simplified');
  16. // 读取输入数据
  17. const inputData = JSON.parse(fs.readFileSync(inputFile, 'utf-8'));
  18. // 检测数据格式并转换
  19. let data;
  20. if (inputData.rounds && inputData.o) {
  21. // v6.1.2.8 格式,需要转换
  22. console.log('✨ 检测到 v6.1.2.8 格式,正在转换为图结构...');
  23. // 尝试读取 search_results.json(兼容旧版本)
  24. let searchResults = null;
  25. const searchResultsPath = path.join(path.dirname(inputFile), 'search_results.json');
  26. if (fs.existsSync(searchResultsPath)) {
  27. console.log('📄 读取外部搜索结果数据(兼容模式)...');
  28. searchResults = JSON.parse(fs.readFileSync(searchResultsPath, 'utf-8'));
  29. } else {
  30. console.log('✅ 使用 run_context.json 中的内嵌搜索结果');
  31. }
  32. // 选择转换函数
  33. let graphData;
  34. let fullData = null; // 用于目录的完整数据
  35. if (useSimplified) {
  36. console.log('🎨 使用简化视图(合并query节点)');
  37. // 生成简化版用于画布
  38. graphData = convertV8ToGraphSimplified(inputData, searchResults);
  39. // 生成完整版用于目录
  40. const fullGraphData = convertV8ToGraphV2(inputData, searchResults);
  41. fullData = {
  42. nodes: fullGraphData.nodes,
  43. edges: fullGraphData.edges,
  44. iterations: fullGraphData.iterations
  45. };
  46. console.log(`✅ 简化版: ${Object.keys(graphData.nodes).length} 个节点, ${graphData.edges.length} 条边`);
  47. console.log(`📋 完整版(用于目录): ${Object.keys(fullData.nodes).length} 个节点`);
  48. } else {
  49. console.log('📊 使用详细视图(完整流程)');
  50. graphData = convertV8ToGraphV2(inputData, searchResults);
  51. console.log(`✅ 转换完成: ${Object.keys(graphData.nodes).length} 个节点, ${graphData.edges.length} 条边`);
  52. }
  53. data = {
  54. nodes: graphData.nodes,
  55. edges: graphData.edges,
  56. iterations: graphData.iterations,
  57. fullData: fullData // 传递完整数据
  58. };
  59. } else if (inputData.nodes && inputData.edges) {
  60. // v6.1.2.5 格式,直接使用
  61. console.log('✨ 检测到 v6.1.2.5 格式,直接使用');
  62. data = inputData;
  63. } else {
  64. console.error('❌ 无法识别的数据格式');
  65. process.exit(1);
  66. }
  67. // 创建临时 React 组件文件
  68. const reactComponentPath = path.join(__dirname, 'temp_flow_component_v2.jsx');
  69. const reactComponent = `
  70. import React, { useState, useCallback, useMemo, useEffect } from 'react';
  71. import { createRoot } from 'react-dom/client';
  72. import {
  73. ReactFlow,
  74. Controls,
  75. Background,
  76. useNodesState,
  77. useEdgesState,
  78. Handle,
  79. Position,
  80. useReactFlow,
  81. ReactFlowProvider,
  82. } from '@xyflow/react';
  83. import '@xyflow/react/dist/style.css';
  84. const data = ${JSON.stringify(data, null, 2)};
  85. // 根据节点类型获取边框颜色
  86. function getNodeTypeColor(type) {
  87. const typeColors = {
  88. 'root': '#6b21a8', // 紫色 - 根节点
  89. 'round': '#7c3aed', // 深紫 - Round节点
  90. 'step': '#f59e0b', // 橙色 - 步骤节点
  91. 'seg': '#10b981', // 绿色 - 分词
  92. 'q': '#3b82f6', // 蓝色 - Query
  93. 'sug': '#06b6d4', // 青色 - Sug建议词
  94. 'seed': '#84cc16', // 黄绿 - Seed
  95. 'add_word': '#22c55e', // 绿色 - 加词生成
  96. 'search_word': '#8b5cf6', // 紫色 - 搜索词
  97. 'post': '#ec4899', // 粉色 - 帖子
  98. 'filtered_sug': '#14b8a6',// 青绿 - 筛选的sug
  99. 'next_q': '#2563eb', // 深蓝 - 下轮查询
  100. 'next_seed': '#65a30d', // 深黄绿 - 下轮种子
  101. 'search': '#8b5cf6', // 深紫 - 搜索(兼容旧版)
  102. 'operation': '#f59e0b', // 橙色 - 操作节点(兼容旧版)
  103. 'query': '#3b82f6', // 蓝色 - 查询(兼容旧版)
  104. 'note': '#ec4899', // 粉色 - 帖子(兼容旧版)
  105. };
  106. return typeColors[type] || '#9ca3af';
  107. }
  108. // 查询节点组件 - 卡片样式
  109. function QueryNode({ id, data, sourcePosition, targetPosition }) {
  110. // 所有节点默认展开
  111. const expanded = true;
  112. // 获取节点类型颜色
  113. const typeColor = getNodeTypeColor(data.nodeType || 'query');
  114. return (
  115. <div>
  116. <Handle
  117. type="target"
  118. position={targetPosition || Position.Left}
  119. style={{ background: typeColor, width: 8, height: 8 }}
  120. />
  121. <div
  122. style={{
  123. padding: '12px',
  124. borderRadius: '8px',
  125. border: data.isHighlighted ? \`3px solid \${typeColor}\` :
  126. data.isCollapsed ? \`2px solid \${typeColor}\` :
  127. data.isSelected === false ? '2px dashed #d1d5db' :
  128. \`2px solid \${typeColor}\`,
  129. background: data.isHighlighted ? '#eef2ff' :
  130. data.isSelected === false ? '#f9fafb' : 'white',
  131. minWidth: '200px',
  132. maxWidth: '280px',
  133. boxShadow: data.isHighlighted ? '0 0 0 4px rgba(102, 126, 234, 0.25), 0 4px 16px rgba(102, 126, 234, 0.4)' :
  134. data.isCollapsed ? '0 4px 12px rgba(102, 126, 234, 0.15)' :
  135. data.level === 0 ? '0 4px 12px rgba(139, 92, 246, 0.15)' : '0 2px 6px rgba(0, 0, 0, 0.06)',
  136. transition: 'all 0.3s ease',
  137. cursor: 'pointer',
  138. position: 'relative',
  139. opacity: data.isSelected === false ? 0.6 : 1,
  140. }}
  141. >
  142. {/* 折叠当前节点按钮 - 左边 */}
  143. <div
  144. style={{
  145. position: 'absolute',
  146. top: '6px',
  147. left: '6px',
  148. width: '20px',
  149. height: '20px',
  150. borderRadius: '50%',
  151. background: '#f59e0b',
  152. color: 'white',
  153. display: 'flex',
  154. alignItems: 'center',
  155. justifyContent: 'center',
  156. fontSize: '11px',
  157. fontWeight: 'bold',
  158. cursor: 'pointer',
  159. transition: 'all 0.2s ease',
  160. zIndex: 10,
  161. }}
  162. onClick={(e) => {
  163. e.stopPropagation();
  164. if (data.onHideSelf) {
  165. data.onHideSelf();
  166. }
  167. }}
  168. onMouseEnter={(e) => {
  169. e.currentTarget.style.background = '#d97706';
  170. }}
  171. onMouseLeave={(e) => {
  172. e.currentTarget.style.background = '#f59e0b';
  173. }}
  174. title="隐藏当前节点"
  175. >
  176. ×
  177. </div>
  178. {/* 聚焦按钮 - 右上角 */}
  179. <div
  180. style={{
  181. position: 'absolute',
  182. top: '6px',
  183. right: '6px',
  184. width: '20px',
  185. height: '20px',
  186. borderRadius: '50%',
  187. background: data.isFocused ? '#10b981' : '#e5e7eb',
  188. color: data.isFocused ? 'white' : '#6b7280',
  189. display: 'flex',
  190. alignItems: 'center',
  191. justifyContent: 'center',
  192. fontSize: '11px',
  193. fontWeight: 'bold',
  194. cursor: 'pointer',
  195. transition: 'all 0.2s ease',
  196. zIndex: 10,
  197. }}
  198. onClick={(e) => {
  199. e.stopPropagation();
  200. if (data.onFocus) {
  201. data.onFocus();
  202. }
  203. }}
  204. onMouseEnter={(e) => {
  205. if (!data.isFocused) {
  206. e.currentTarget.style.background = '#d1d5db';
  207. }
  208. }}
  209. onMouseLeave={(e) => {
  210. if (!data.isFocused) {
  211. e.currentTarget.style.background = '#e5e7eb';
  212. }
  213. }}
  214. title={data.isFocused ? '取消聚焦' : '聚焦到此节点'}
  215. >
  216. 🎯
  217. </div>
  218. {/* 折叠/展开子节点按钮 - 右边第二个位置 */}
  219. {data.hasChildren && (
  220. <div
  221. style={{
  222. position: 'absolute',
  223. top: '6px',
  224. right: '30px',
  225. width: '20px',
  226. height: '20px',
  227. borderRadius: '50%',
  228. background: data.isCollapsed ? '#667eea' : '#e5e7eb',
  229. color: data.isCollapsed ? 'white' : '#6b7280',
  230. display: 'flex',
  231. alignItems: 'center',
  232. justifyContent: 'center',
  233. fontSize: '11px',
  234. fontWeight: 'bold',
  235. cursor: 'pointer',
  236. transition: 'all 0.2s ease',
  237. zIndex: 10,
  238. }}
  239. onClick={(e) => {
  240. e.stopPropagation();
  241. data.onToggleCollapse();
  242. }}
  243. title={data.isCollapsed ? '展开子节点' : '折叠子节点'}
  244. >
  245. {data.isCollapsed ? '+' : '−'}
  246. </div>
  247. )}
  248. {/* 卡片内容 */}
  249. <div>
  250. {/* 标题行 */}
  251. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '8px', paddingLeft: '24px', paddingRight: data.hasChildren ? '54px' : '28px' }}>
  252. <div style={{ flex: 1 }}>
  253. <div style={{ display: 'flex', alignItems: 'center', gap: '4px', marginBottom: '3px' }}>
  254. <div style={{
  255. fontSize: '13px',
  256. fontWeight: data.level === 0 ? '700' : '600',
  257. color: data.level === 0 ? '#6b21a8' : '#1f2937',
  258. lineHeight: '1.3',
  259. flex: 1,
  260. }}>
  261. {data.title}
  262. </div>
  263. {data.isSelected === false && (
  264. <div style={{
  265. fontSize: '9px',
  266. padding: '1px 4px',
  267. borderRadius: '3px',
  268. background: '#fee2e2',
  269. color: '#991b1b',
  270. fontWeight: '500',
  271. flexShrink: 0,
  272. }}>
  273. 未选中
  274. </div>
  275. )}
  276. </div>
  277. </div>
  278. </div>
  279. {/* 展开的详细信息 - 始终显示 */}
  280. <div style={{ fontSize: '11px', lineHeight: 1.4 }}>
  281. <div style={{ display: 'flex', gap: '4px', marginBottom: '6px', flexWrap: 'wrap' }}>
  282. <span style={{
  283. display: 'inline-block',
  284. padding: '1px 6px',
  285. borderRadius: '10px',
  286. background: '#eff6ff',
  287. color: '#3b82f6',
  288. fontSize: '10px',
  289. fontWeight: '500',
  290. }}>
  291. Lv.{data.level}
  292. </span>
  293. <span style={{
  294. display: 'inline-block',
  295. padding: '1px 6px',
  296. borderRadius: '10px',
  297. background: '#f0fdf4',
  298. color: '#16a34a',
  299. fontSize: '10px',
  300. fontWeight: '500',
  301. }}>
  302. {data.score}
  303. </span>
  304. {data.strategy && data.strategy !== 'root' && (
  305. <span style={{
  306. display: 'inline-block',
  307. padding: '1px 6px',
  308. borderRadius: '10px',
  309. background: '#fef3c7',
  310. color: '#92400e',
  311. fontSize: '10px',
  312. fontWeight: '500',
  313. }}>
  314. {data.strategy}
  315. </span>
  316. )}
  317. </div>
  318. {data.parent && (
  319. <div style={{ color: '#6b7280', fontSize: '10px', marginTop: '4px', paddingTop: '4px', borderTop: '1px solid #f3f4f6' }}>
  320. <strong>Parent:</strong> {data.parent}
  321. </div>
  322. )}
  323. {data.selectedWord && (
  324. <div style={{
  325. marginTop: '6px',
  326. paddingTop: '6px',
  327. borderTop: '1px solid #f3f4f6',
  328. fontSize: '10px',
  329. color: '#6b7280',
  330. lineHeight: '1.5',
  331. }}>
  332. <strong style={{ color: '#4b5563' }}>选择词:</strong>
  333. <span style={{ marginLeft: '4px', color: '#3b82f6', fontWeight: '500' }}>{data.selectedWord}</span>
  334. </div>
  335. )}
  336. {data.evaluationReason && (
  337. <div style={{
  338. marginTop: '6px',
  339. paddingTop: '6px',
  340. borderTop: '1px solid #f3f4f6',
  341. fontSize: '10px',
  342. color: '#6b7280',
  343. lineHeight: '1.5',
  344. }}>
  345. <strong style={{ color: '#4b5563' }}>评估:</strong>
  346. <div style={{ marginTop: '2px' }}>{data.evaluationReason}</div>
  347. </div>
  348. )}
  349. {data.occurrences && data.occurrences.length > 1 && (
  350. <div style={{
  351. marginTop: '6px',
  352. paddingTop: '6px',
  353. borderTop: '1px solid #f3f4f6',
  354. fontSize: '10px',
  355. color: '#6b7280',
  356. }}>
  357. <strong style={{ color: '#4b5563' }}>演化历史 ({data.occurrences.length}次):</strong>
  358. <div style={{ marginTop: '4px' }}>
  359. {data.occurrences.map((occ, idx) => (
  360. <div key={idx} style={{ marginTop: '2px', paddingLeft: '8px' }}>
  361. <span style={{ color: '#3b82f6', fontWeight: '500' }}>R{occ.round}</span>
  362. {' · '}
  363. <span>{occ.strategy}</span>
  364. {occ.score !== undefined && (
  365. <span style={{ color: '#16a34a', marginLeft: '4px' }}>
  366. ({typeof occ.score === 'number' ? occ.score.toFixed(2) : occ.score})
  367. </span>
  368. )}
  369. </div>
  370. ))}
  371. </div>
  372. </div>
  373. )}
  374. {data.hasSearchResults && (
  375. <div style={{
  376. marginTop: '6px',
  377. paddingTop: '6px',
  378. borderTop: '1px solid #f3f4f6',
  379. fontSize: '10px',
  380. background: '#fef3c7',
  381. padding: '4px 6px',
  382. borderRadius: '4px',
  383. color: '#92400e',
  384. fontWeight: '500',
  385. }}>
  386. 🔍 找到 {data.postCount} 个帖子
  387. </div>
  388. )}
  389. </div>
  390. </div>
  391. </div>
  392. <Handle
  393. type="source"
  394. position={sourcePosition || Position.Right}
  395. style={{ background: '#667eea', width: 8, height: 8 }}
  396. />
  397. </div>
  398. );
  399. }
  400. // 笔记节点组件 - 卡片样式,带轮播图
  401. function NoteNode({ id, data, sourcePosition, targetPosition }) {
  402. const [currentImageIndex, setCurrentImageIndex] = useState(0);
  403. const expanded = true;
  404. const hasImages = data.imageList && data.imageList.length > 0;
  405. const nextImage = (e) => {
  406. e.stopPropagation();
  407. if (hasImages) {
  408. setCurrentImageIndex((prev) => (prev + 1) % data.imageList.length);
  409. }
  410. };
  411. const prevImage = (e) => {
  412. e.stopPropagation();
  413. if (hasImages) {
  414. setCurrentImageIndex((prev) => (prev - 1 + data.imageList.length) % data.imageList.length);
  415. }
  416. };
  417. return (
  418. <div>
  419. <Handle
  420. type="target"
  421. position={targetPosition || Position.Left}
  422. style={{ background: '#ec4899', width: 8, height: 8 }}
  423. />
  424. <div
  425. style={{
  426. padding: '14px',
  427. borderRadius: '20px',
  428. border: data.isHighlighted ? '3px solid #ec4899' : '2px solid #fce7f3',
  429. background: data.isHighlighted ? '#eef2ff' : 'white',
  430. minWidth: '220px',
  431. maxWidth: '300px',
  432. boxShadow: data.isHighlighted ? '0 0 0 4px rgba(236, 72, 153, 0.25), 0 4px 16px rgba(236, 72, 153, 0.4)' : '0 4px 12px rgba(236, 72, 153, 0.15)',
  433. transition: 'all 0.3s ease',
  434. cursor: 'pointer',
  435. }}
  436. >
  437. {/* 笔记标题 */}
  438. <div style={{ display: 'flex', alignItems: 'flex-start', marginBottom: '8px' }}>
  439. <div style={{ flex: 1 }}>
  440. <div style={{
  441. fontSize: '13px',
  442. fontWeight: '600',
  443. color: '#831843',
  444. lineHeight: '1.4',
  445. marginBottom: '4px',
  446. }}>
  447. {data.title}
  448. </div>
  449. </div>
  450. </div>
  451. {/* 轮播图 */}
  452. {hasImages && (
  453. <div style={{
  454. position: 'relative',
  455. marginBottom: '8px',
  456. borderRadius: '12px',
  457. overflow: 'hidden',
  458. }}>
  459. <img
  460. src={data.imageList[currentImageIndex].image_url}
  461. alt={\`Image \${currentImageIndex + 1}\`}
  462. style={{
  463. width: '100%',
  464. height: '160px',
  465. objectFit: 'cover',
  466. display: 'block',
  467. }}
  468. onError={(e) => {
  469. e.target.style.display = 'none';
  470. }}
  471. />
  472. {data.imageList.length > 1 && (
  473. <>
  474. {/* 左右切换按钮 */}
  475. <button
  476. onClick={prevImage}
  477. style={{
  478. position: 'absolute',
  479. left: '4px',
  480. top: '50%',
  481. transform: 'translateY(-50%)',
  482. background: 'rgba(0, 0, 0, 0.5)',
  483. color: 'white',
  484. border: 'none',
  485. borderRadius: '50%',
  486. width: '24px',
  487. height: '24px',
  488. cursor: 'pointer',
  489. display: 'flex',
  490. alignItems: 'center',
  491. justifyContent: 'center',
  492. fontSize: '14px',
  493. }}
  494. >
  495. </button>
  496. <button
  497. onClick={nextImage}
  498. style={{
  499. position: 'absolute',
  500. right: '4px',
  501. top: '50%',
  502. transform: 'translateY(-50%)',
  503. background: 'rgba(0, 0, 0, 0.5)',
  504. color: 'white',
  505. border: 'none',
  506. borderRadius: '50%',
  507. width: '24px',
  508. height: '24px',
  509. cursor: 'pointer',
  510. display: 'flex',
  511. alignItems: 'center',
  512. justifyContent: 'center',
  513. fontSize: '14px',
  514. }}
  515. >
  516. </button>
  517. {/* 图片计数 */}
  518. <div style={{
  519. position: 'absolute',
  520. bottom: '4px',
  521. right: '4px',
  522. background: 'rgba(0, 0, 0, 0.6)',
  523. color: 'white',
  524. padding: '2px 6px',
  525. borderRadius: '10px',
  526. fontSize: '10px',
  527. }}>
  528. {currentImageIndex + 1}/{data.imageList.length}
  529. </div>
  530. </>
  531. )}
  532. </div>
  533. )}
  534. {/* 互动数据 */}
  535. {data.interact_info && (
  536. <div style={{
  537. display: 'flex',
  538. gap: '8px',
  539. marginBottom: '8px',
  540. flexWrap: 'wrap',
  541. fontSize: '11px',
  542. color: '#9f1239',
  543. }}>
  544. {data.interact_info.liked_count > 0 && (
  545. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  546. ❤️ {data.interact_info.liked_count}
  547. </span>
  548. )}
  549. {data.interact_info.collected_count > 0 && (
  550. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  551. ⭐ {data.interact_info.collected_count}
  552. </span>
  553. )}
  554. {data.interact_info.comment_count > 0 && (
  555. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  556. 💬 {data.interact_info.comment_count}
  557. </span>
  558. )}
  559. {data.interact_info.shared_count > 0 && (
  560. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  561. 🔗 {data.interact_info.shared_count}
  562. </span>
  563. )}
  564. </div>
  565. )}
  566. {/* 被哪些query找到 */}
  567. {data.foundByQueries && data.foundByQueries.length > 0 && (
  568. <div style={{
  569. marginBottom: '8px',
  570. padding: '6px 8px',
  571. background: '#f0fdf4',
  572. borderRadius: '6px',
  573. fontSize: '10px',
  574. }}>
  575. <strong style={{ color: '#16a34a' }}>🔍 被找到:</strong>
  576. <div style={{ marginTop: '4px', display: 'flex', flexWrap: 'wrap', gap: '4px' }}>
  577. {data.foundByQueries.map((query, idx) => (
  578. <span key={idx} style={{
  579. display: 'inline-block',
  580. padding: '2px 6px',
  581. background: '#dcfce7',
  582. color: '#166534',
  583. borderRadius: '4px',
  584. fontSize: '9px',
  585. }}>
  586. {query}
  587. </span>
  588. ))}
  589. </div>
  590. {data.foundInRounds && data.foundInRounds.length > 0 && (
  591. <div style={{ marginTop: '4px', color: '#6b7280' }}>
  592. 出现在: Round {data.foundInRounds.join(', ')}
  593. </div>
  594. )}
  595. </div>
  596. )}
  597. {/* 标签 */}
  598. {(data.matchLevel || data.score) && (
  599. <div style={{ display: 'flex', gap: '6px', marginBottom: '8px', flexWrap: 'wrap' }}>
  600. {data.matchLevel && (
  601. <span style={{
  602. display: 'inline-block',
  603. padding: '2px 8px',
  604. borderRadius: '12px',
  605. background: '#fff1f2',
  606. color: '#be123c',
  607. fontSize: '10px',
  608. fontWeight: '500',
  609. }}>
  610. {data.matchLevel}
  611. </span>
  612. )}
  613. {data.score && (
  614. <span style={{
  615. display: 'inline-block',
  616. padding: '2px 8px',
  617. borderRadius: '12px',
  618. background: '#fff7ed',
  619. color: '#c2410c',
  620. fontSize: '10px',
  621. fontWeight: '500',
  622. }}>
  623. Score: {data.score}
  624. </span>
  625. )}
  626. </div>
  627. )}
  628. {/* 描述 */}
  629. {expanded && data.description && (
  630. <div style={{
  631. fontSize: '11px',
  632. color: '#9f1239',
  633. lineHeight: '1.5',
  634. paddingTop: '8px',
  635. borderTop: '1px solid #fbcfe8',
  636. }}>
  637. {data.description}
  638. </div>
  639. )}
  640. {/* 评估理由 */}
  641. {expanded && data.evaluationReason && (
  642. <div style={{
  643. fontSize: '10px',
  644. color: '#831843',
  645. lineHeight: '1.5',
  646. paddingTop: '8px',
  647. marginTop: '8px',
  648. borderTop: '1px solid #fbcfe8',
  649. }}>
  650. <strong style={{ color: '#9f1239' }}>评估:</strong>
  651. <div style={{ marginTop: '2px' }}>{data.evaluationReason}</div>
  652. </div>
  653. )}
  654. </div>
  655. <Handle
  656. type="source"
  657. position={sourcePosition || Position.Right}
  658. style={{ background: '#ec4899', width: 8, height: 8 }}
  659. />
  660. </div>
  661. );
  662. }
  663. const nodeTypes = {
  664. query: QueryNode,
  665. note: NoteNode,
  666. };
  667. // 根据 score 获取颜色
  668. function getScoreColor(score) {
  669. if (score >= 0.7) return '#10b981'; // 绿色 - 高分
  670. if (score >= 0.4) return '#f59e0b'; // 橙色 - 中分
  671. return '#ef4444'; // 红色 - 低分
  672. }
  673. // 截断文本,保留头尾,中间显示省略号
  674. function truncateMiddle(text, maxLength = 20) {
  675. if (!text || text.length <= maxLength) return text;
  676. const headLength = Math.ceil(maxLength * 0.4);
  677. const tailLength = Math.floor(maxLength * 0.4);
  678. const head = text.substring(0, headLength);
  679. const tail = text.substring(text.length - tailLength);
  680. return \`\${head}...\${tail}\`;
  681. }
  682. // 根据策略获取颜色
  683. function getStrategyColor(strategy) {
  684. const strategyColors = {
  685. '初始分词': '#10b981',
  686. '调用sug': '#06b6d4',
  687. '同义改写': '#f59e0b',
  688. '加词': '#3b82f6',
  689. '抽象改写': '#8b5cf6',
  690. '基于部分匹配改进': '#ec4899',
  691. '结果分支-抽象改写': '#a855f7',
  692. '结果分支-同义改写': '#fb923c',
  693. // v6.1.2.8 新增策略
  694. '原始问题': '#6b21a8',
  695. '来自分词': '#10b981',
  696. '加词生成': '#ef4444',
  697. '建议词': '#06b6d4',
  698. '执行搜索': '#8b5cf6',
  699. };
  700. return strategyColors[strategy] || '#9ca3af';
  701. }
  702. // 树节点组件
  703. function TreeNode({ node, level, children, isCollapsed, onToggle, isSelected, onSelect }) {
  704. const hasChildren = children && children.length > 0;
  705. const score = node.data.score ? parseFloat(node.data.score) : 0;
  706. const strategy = node.data.strategy || '';
  707. const strategyColor = getStrategyColor(strategy);
  708. const nodeActualType = node.data.nodeType || node.type; // 获取实际节点类型
  709. return (
  710. <div style={{ marginLeft: level * 12 + 'px' }}>
  711. <div
  712. style={{
  713. padding: '6px 8px',
  714. borderRadius: '4px',
  715. cursor: 'pointer',
  716. background: 'transparent',
  717. border: isSelected ? '1px solid #3b82f6' : '1px solid transparent',
  718. display: 'flex',
  719. alignItems: 'center',
  720. gap: '6px',
  721. transition: 'all 0.2s ease',
  722. position: 'relative',
  723. overflow: 'visible',
  724. }}
  725. onMouseEnter={(e) => {
  726. if (!isSelected) e.currentTarget.style.background = '#f9fafb';
  727. }}
  728. onMouseLeave={(e) => {
  729. if (!isSelected) e.currentTarget.style.background = 'transparent';
  730. }}
  731. >
  732. {/* 策略类型竖线 */}
  733. <div style={{
  734. width: '3px',
  735. height: '20px',
  736. background: strategyColor,
  737. borderRadius: '2px',
  738. flexShrink: 0,
  739. position: 'relative',
  740. zIndex: 1,
  741. }} />
  742. {hasChildren && (
  743. <span
  744. style={{
  745. fontSize: '10px',
  746. color: '#6b7280',
  747. cursor: 'pointer',
  748. width: '16px',
  749. textAlign: 'center',
  750. position: 'relative',
  751. zIndex: 1,
  752. }}
  753. onClick={(e) => {
  754. e.stopPropagation();
  755. onToggle();
  756. }}
  757. >
  758. {isCollapsed ? '▶' : '▼'}
  759. </span>
  760. )}
  761. {!hasChildren && <span style={{ width: '16px', position: 'relative', zIndex: 1 }}></span>}
  762. <div
  763. style={{
  764. flex: 1,
  765. fontSize: '12px',
  766. color: '#374151',
  767. position: 'relative',
  768. zIndex: 1,
  769. minWidth: 0,
  770. display: 'flex',
  771. flexDirection: 'column',
  772. gap: '4px',
  773. }}
  774. onClick={onSelect}
  775. >
  776. <div style={{
  777. display: 'flex',
  778. alignItems: 'center',
  779. gap: '8px',
  780. }}>
  781. <div style={{
  782. fontWeight: level === 0 ? '600' : '400',
  783. maxWidth: '180px',
  784. flex: 1,
  785. minWidth: 0,
  786. color: (node.type === 'note' ? node.data.matchLevel === 'unsatisfied' : node.data.isSelected === false) ? '#ef4444' : '#374151',
  787. }}
  788. title={node.data.title || node.id}
  789. >
  790. {truncateMiddle(node.data.title || node.id, 18)}
  791. </div>
  792. {/* 分数显示 - 步骤和轮次节点不显示分数 */}
  793. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  794. <span style={{
  795. fontSize: '11px',
  796. color: '#6b7280',
  797. fontWeight: '500',
  798. flexShrink: 0,
  799. }}>
  800. {score.toFixed(2)}
  801. </span>
  802. )}
  803. </div>
  804. {/* 分数下划线 - 步骤和轮次节点不显示 */}
  805. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  806. <div style={{
  807. width: (score * 100) + '%',
  808. height: '2px',
  809. background: getScoreColor(score),
  810. borderRadius: '1px',
  811. }} />
  812. )}
  813. </div>
  814. </div>
  815. {hasChildren && !isCollapsed && (
  816. <div>
  817. {children}
  818. </div>
  819. )}
  820. </div>
  821. );
  822. }
  823. // 使用 dagre 自动布局
  824. function getLayoutedElements(nodes, edges, direction = 'LR') {
  825. console.log('🎯 Starting layout with dagre...');
  826. console.log('Input:', nodes.length, 'nodes,', edges.length, 'edges');
  827. // 检查 dagre 是否加载
  828. if (typeof window === 'undefined' || typeof window.dagre === 'undefined') {
  829. console.warn('⚠️ Dagre not loaded, using fallback layout');
  830. // 降级到简单布局
  831. const levelGroups = {};
  832. nodes.forEach(node => {
  833. const level = node.data.level || 0;
  834. if (!levelGroups[level]) levelGroups[level] = [];
  835. levelGroups[level].push(node);
  836. });
  837. Object.entries(levelGroups).forEach(([level, nodeList]) => {
  838. const x = parseInt(level) * 350;
  839. nodeList.forEach((node, index) => {
  840. node.position = { x, y: index * 150 };
  841. node.targetPosition = 'left';
  842. node.sourcePosition = 'right';
  843. });
  844. });
  845. return { nodes, edges };
  846. }
  847. try {
  848. const dagreGraph = new window.dagre.graphlib.Graph();
  849. dagreGraph.setDefaultEdgeLabel(() => ({}));
  850. const isHorizontal = direction === 'LR';
  851. dagreGraph.setGraph({
  852. rankdir: direction,
  853. nodesep: 120, // 垂直间距 - 增加以避免节点重叠
  854. ranksep: 280, // 水平间距 - 增加以容纳更宽的节点
  855. });
  856. // 添加节点 - 根据节点类型设置不同的尺寸
  857. nodes.forEach((node) => {
  858. let nodeWidth = 280;
  859. let nodeHeight = 180;
  860. // note 节点有轮播图,需要更大的空间
  861. if (node.type === 'note') {
  862. nodeWidth = 320;
  863. nodeHeight = 350; // 增加高度以容纳轮播图
  864. }
  865. dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight });
  866. });
  867. // 添加边
  868. edges.forEach((edge) => {
  869. dagreGraph.setEdge(edge.source, edge.target);
  870. });
  871. // 计算布局
  872. window.dagre.layout(dagreGraph);
  873. console.log('✅ Dagre layout completed');
  874. // 更新节点位置和 handle 位置
  875. nodes.forEach((node) => {
  876. const nodeWithPosition = dagreGraph.node(node.id);
  877. if (!nodeWithPosition) {
  878. console.warn('Node position not found for:', node.id);
  879. return;
  880. }
  881. node.targetPosition = isHorizontal ? 'left' : 'top';
  882. node.sourcePosition = isHorizontal ? 'right' : 'bottom';
  883. // 根据节点类型获取尺寸
  884. let nodeWidth = 280;
  885. let nodeHeight = 180;
  886. if (node.type === 'note') {
  887. nodeWidth = 320;
  888. nodeHeight = 350;
  889. }
  890. // 将 dagre 的中心点位置转换为 React Flow 的左上角位置
  891. node.position = {
  892. x: nodeWithPosition.x - nodeWidth / 2,
  893. y: nodeWithPosition.y - nodeHeight / 2,
  894. };
  895. });
  896. console.log('✅ Layout completed, sample node:', nodes[0]);
  897. return { nodes, edges };
  898. } catch (error) {
  899. console.error('❌ Error in dagre layout:', error);
  900. console.error('Error details:', error.message, error.stack);
  901. // 降级处理
  902. console.log('Using fallback layout...');
  903. const levelGroups = {};
  904. nodes.forEach(node => {
  905. const level = node.data.level || 0;
  906. if (!levelGroups[level]) levelGroups[level] = [];
  907. levelGroups[level].push(node);
  908. });
  909. Object.entries(levelGroups).forEach(([level, nodeList]) => {
  910. const x = parseInt(level) * 350;
  911. nodeList.forEach((node, index) => {
  912. node.position = { x, y: index * 150 };
  913. node.targetPosition = 'left';
  914. node.sourcePosition = 'right';
  915. });
  916. });
  917. return { nodes, edges };
  918. }
  919. }
  920. function transformData(data) {
  921. const nodes = [];
  922. const edges = [];
  923. const originalIdToCanvasId = {}; // 原始ID -> 画布ID的映射
  924. const canvasIdToNodeData = {}; // 避免重复创建相同的节点
  925. // 创建节点
  926. Object.entries(data.nodes).forEach(([originalId, node]) => {
  927. // 统一处理所有类型的节点
  928. const nodeType = node.type || 'query';
  929. // 直接使用originalId作为canvasId,避免冲突
  930. const canvasId = originalId;
  931. originalIdToCanvasId[originalId] = canvasId;
  932. // 如果这个 canvasId 还没有创建过节点,则创建
  933. if (!canvasIdToNodeData[canvasId]) {
  934. canvasIdToNodeData[canvasId] = true;
  935. // 根据节点类型创建不同的数据结构
  936. if (nodeType === 'note' || nodeType === 'post') {
  937. nodes.push({
  938. id: canvasId,
  939. originalId: originalId,
  940. type: 'note',
  941. data: {
  942. title: node.query || node.title || '帖子',
  943. matchLevel: node.match_level,
  944. score: node.relevance_score ? node.relevance_score.toFixed(2) : '0.00',
  945. description: node.body_text || node.desc || '',
  946. isSelected: node.is_selected !== undefined ? node.is_selected : true,
  947. imageList: node.image_list || [],
  948. noteUrl: node.note_url || '',
  949. evaluationReason: node.evaluationReason || node.evaluation_reason || '',
  950. interact_info: node.interact_info || {},
  951. nodeType: nodeType,
  952. },
  953. position: { x: 0, y: 0 },
  954. });
  955. } else {
  956. // query, seg, q, search, root 等节点
  957. let displayTitle = node.query || originalId;
  958. nodes.push({
  959. id: canvasId,
  960. originalId: originalId,
  961. type: 'query', // 使用 query 组件渲染所有非note节点
  962. data: {
  963. title: displayTitle,
  964. level: node.level || 0,
  965. score: node.relevance_score ? node.relevance_score.toFixed(2) : '0.00',
  966. strategy: node.strategy || '',
  967. parent: node.parent_query || '',
  968. isSelected: node.is_selected !== undefined ? node.is_selected : true,
  969. evaluationReason: node.evaluationReason || node.evaluation_reason || '',
  970. nodeType: nodeType, // 传递实际节点类型用于样式
  971. searchCount: node.search_count, // search 节点特有
  972. totalPosts: node.total_posts, // search 节点特有
  973. selectedWord: node.selected_word || '', // 加词节点特有 - 显示选择的词
  974. },
  975. position: { x: 0, y: 0 },
  976. });
  977. }
  978. }
  979. });
  980. // 创建边 - 使用虚线样式,映射到画布ID
  981. data.edges.forEach((edge, index) => {
  982. const edgeColors = {
  983. '初始分词': '#10b981',
  984. '调用sug': '#06b6d4',
  985. '同义改写': '#f59e0b',
  986. '加词': '#3b82f6',
  987. '抽象改写': '#8b5cf6',
  988. '基于部分匹配改进': '#ec4899',
  989. '结果分支-抽象改写': '#a855f7',
  990. '结果分支-同义改写': '#fb923c',
  991. 'query_to_note': '#ec4899',
  992. };
  993. const color = edgeColors[edge.strategy] || edgeColors[edge.edge_type] || '#d1d5db';
  994. const isNoteEdge = edge.edge_type === 'query_to_note';
  995. edges.push({
  996. id: \`edge-\${index}\`,
  997. source: originalIdToCanvasId[edge.from], // 使用画布ID
  998. target: originalIdToCanvasId[edge.to], // 使用画布ID
  999. type: 'simplebezier', // 使用简单贝塞尔曲线
  1000. animated: isNoteEdge,
  1001. style: {
  1002. stroke: color,
  1003. strokeWidth: isNoteEdge ? 2.5 : 2,
  1004. strokeDasharray: isNoteEdge ? '5,5' : '8,4',
  1005. },
  1006. markerEnd: {
  1007. type: 'arrowclosed',
  1008. color: color,
  1009. width: 20,
  1010. height: 20,
  1011. },
  1012. });
  1013. });
  1014. // 使用 dagre 自动计算布局 - 从左到右
  1015. return getLayoutedElements(nodes, edges, 'LR');
  1016. }
  1017. function FlowContent() {
  1018. const { nodes: initialNodes, edges: initialEdges } = useMemo(() => {
  1019. console.log('🔍 Transforming data...');
  1020. const result = transformData(data);
  1021. console.log('✅ Transformed:', result.nodes.length, 'nodes,', result.edges.length, 'edges');
  1022. return result;
  1023. }, []);
  1024. // 初始化:找出所有有子节点的节点,默认折叠(画布节点)
  1025. const initialCollapsedNodes = useMemo(() => {
  1026. const nodesWithChildren = new Set();
  1027. initialEdges.forEach(edge => {
  1028. nodesWithChildren.add(edge.source);
  1029. });
  1030. // 排除根节点(level 0),让根节点默认展开
  1031. const rootNode = initialNodes.find(n => n.data.level === 0);
  1032. if (rootNode) {
  1033. nodesWithChildren.delete(rootNode.id);
  1034. }
  1035. return nodesWithChildren;
  1036. }, [initialNodes, initialEdges]);
  1037. // 树节点的折叠状态需要在树构建后初始化
  1038. const [collapsedNodes, setCollapsedNodes] = useState(() => initialCollapsedNodes);
  1039. const [collapsedTreeNodes, setCollapsedTreeNodes] = useState(new Set());
  1040. const [selectedNodeId, setSelectedNodeId] = useState(null);
  1041. const [hiddenNodes, setHiddenNodes] = useState(new Set()); // 用户手动隐藏的节点
  1042. const [focusMode, setFocusMode] = useState(false); // 全局聚焦模式,默认关闭
  1043. const [focusedNodeId, setFocusedNodeId] = useState(null); // 单独聚焦的节点ID
  1044. // 获取 React Flow 实例以控制画布
  1045. const { setCenter, fitView } = useReactFlow();
  1046. // 获取某个节点的所有后代节点ID
  1047. const getDescendants = useCallback((nodeId) => {
  1048. const descendants = new Set();
  1049. const queue = [nodeId];
  1050. while (queue.length > 0) {
  1051. const current = queue.shift();
  1052. initialEdges.forEach(edge => {
  1053. if (edge.source === current && !descendants.has(edge.target)) {
  1054. descendants.add(edge.target);
  1055. queue.push(edge.target);
  1056. }
  1057. });
  1058. }
  1059. return descendants;
  1060. }, [initialEdges]);
  1061. // 获取直接父节点
  1062. const getDirectParents = useCallback((nodeId) => {
  1063. const parents = [];
  1064. initialEdges.forEach(edge => {
  1065. if (edge.target === nodeId) {
  1066. parents.push(edge.source);
  1067. }
  1068. });
  1069. return parents;
  1070. }, [initialEdges]);
  1071. // 获取直接子节点
  1072. const getDirectChildren = useCallback((nodeId) => {
  1073. const children = [];
  1074. initialEdges.forEach(edge => {
  1075. if (edge.source === nodeId) {
  1076. children.push(edge.target);
  1077. }
  1078. });
  1079. return children;
  1080. }, [initialEdges]);
  1081. // 切换节点折叠状态
  1082. const toggleNodeCollapse = useCallback((nodeId) => {
  1083. setCollapsedNodes(prev => {
  1084. const newSet = new Set(prev);
  1085. const descendants = getDescendants(nodeId);
  1086. if (newSet.has(nodeId)) {
  1087. // 展开:移除此节点,但保持其他折叠的节点
  1088. newSet.delete(nodeId);
  1089. } else {
  1090. // 折叠:添加此节点
  1091. newSet.add(nodeId);
  1092. }
  1093. return newSet;
  1094. });
  1095. }, [getDescendants]);
  1096. // 过滤可见的节点和边,并重新计算布局
  1097. const { nodes, edges } = useMemo(() => {
  1098. const nodesToHide = new Set();
  1099. // 判断使用哪个节点ID进行聚焦:优先使用单独聚焦的节点,否则使用全局聚焦模式的选中节点
  1100. const effectiveFocusNodeId = focusedNodeId || (focusMode ? selectedNodeId : null);
  1101. // 聚焦模式:只显示聚焦节点、其父节点和直接子节点
  1102. if (effectiveFocusNodeId) {
  1103. const visibleInFocus = new Set([effectiveFocusNodeId]);
  1104. // 添加所有父节点
  1105. initialEdges.forEach(edge => {
  1106. if (edge.target === effectiveFocusNodeId) {
  1107. visibleInFocus.add(edge.source);
  1108. }
  1109. });
  1110. // 添加所有直接子节点
  1111. initialEdges.forEach(edge => {
  1112. if (edge.source === effectiveFocusNodeId) {
  1113. visibleInFocus.add(edge.target);
  1114. }
  1115. });
  1116. // 隐藏不在聚焦范围内的节点
  1117. initialNodes.forEach(node => {
  1118. if (!visibleInFocus.has(node.id)) {
  1119. nodesToHide.add(node.id);
  1120. }
  1121. });
  1122. } else {
  1123. // 非聚焦模式:使用原有的折叠逻辑
  1124. // 收集所有被折叠节点的后代
  1125. collapsedNodes.forEach(collapsedId => {
  1126. const descendants = getDescendants(collapsedId);
  1127. descendants.forEach(id => nodesToHide.add(id));
  1128. });
  1129. }
  1130. // 添加用户手动隐藏的节点
  1131. hiddenNodes.forEach(id => nodesToHide.add(id));
  1132. const visibleNodes = initialNodes
  1133. .filter(node => !nodesToHide.has(node.id))
  1134. .map(node => ({
  1135. ...node,
  1136. data: {
  1137. ...node.data,
  1138. isCollapsed: collapsedNodes.has(node.id),
  1139. hasChildren: initialEdges.some(e => e.source === node.id),
  1140. onToggleCollapse: () => toggleNodeCollapse(node.id),
  1141. onHideSelf: () => {
  1142. setHiddenNodes(prev => {
  1143. const newSet = new Set(prev);
  1144. newSet.add(node.id);
  1145. return newSet;
  1146. });
  1147. },
  1148. onFocus: () => {
  1149. // 切换聚焦状态
  1150. if (focusedNodeId === node.id) {
  1151. setFocusedNodeId(null); // 如果已经聚焦,则取消聚焦
  1152. } else {
  1153. // 先取消之前的聚焦,然后聚焦到当前节点
  1154. setFocusedNodeId(node.id);
  1155. // 延迟聚焦视图到该节点
  1156. setTimeout(() => {
  1157. fitView({
  1158. nodes: [{ id: node.id }],
  1159. duration: 800,
  1160. padding: 0.3,
  1161. });
  1162. }, 100);
  1163. }
  1164. },
  1165. isFocused: focusedNodeId === node.id,
  1166. isHighlighted: selectedNodeId === node.id,
  1167. }
  1168. }));
  1169. const visibleEdges = initialEdges.filter(
  1170. edge => !nodesToHide.has(edge.source) && !nodesToHide.has(edge.target)
  1171. );
  1172. // 重新计算布局 - 只对可见节点
  1173. if (typeof window !== 'undefined' && typeof window.dagre !== 'undefined') {
  1174. try {
  1175. const dagreGraph = new window.dagre.graphlib.Graph();
  1176. dagreGraph.setDefaultEdgeLabel(() => ({}));
  1177. dagreGraph.setGraph({
  1178. rankdir: 'LR',
  1179. nodesep: 120, // 垂直间距 - 增加以避免节点重叠
  1180. ranksep: 280, // 水平间距 - 增加以容纳更宽的节点
  1181. });
  1182. visibleNodes.forEach((node) => {
  1183. let nodeWidth = 280;
  1184. let nodeHeight = 180;
  1185. // note 节点有轮播图,需要更大的空间
  1186. if (node.type === 'note') {
  1187. nodeWidth = 320;
  1188. nodeHeight = 350;
  1189. }
  1190. dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight });
  1191. });
  1192. visibleEdges.forEach((edge) => {
  1193. dagreGraph.setEdge(edge.source, edge.target);
  1194. });
  1195. window.dagre.layout(dagreGraph);
  1196. visibleNodes.forEach((node) => {
  1197. const nodeWithPosition = dagreGraph.node(node.id);
  1198. if (nodeWithPosition) {
  1199. // 根据节点类型获取对应的尺寸
  1200. let nodeWidth = 280;
  1201. let nodeHeight = 180;
  1202. if (node.type === 'note') {
  1203. nodeWidth = 320;
  1204. nodeHeight = 350;
  1205. }
  1206. node.position = {
  1207. x: nodeWithPosition.x - nodeWidth / 2,
  1208. y: nodeWithPosition.y - nodeHeight / 2,
  1209. };
  1210. node.targetPosition = 'left';
  1211. node.sourcePosition = 'right';
  1212. }
  1213. });
  1214. console.log('✅ Dynamic layout recalculated for', visibleNodes.length, 'visible nodes');
  1215. } catch (error) {
  1216. console.error('❌ Error in dynamic layout:', error);
  1217. }
  1218. }
  1219. return { nodes: visibleNodes, edges: visibleEdges };
  1220. }, [initialNodes, initialEdges, collapsedNodes, hiddenNodes, focusMode, focusedNodeId, getDescendants, toggleNodeCollapse, selectedNodeId]);
  1221. // 构建树形结构 - 允许一个节点有多个父节点
  1222. const buildTree = useCallback(() => {
  1223. const nodeMap = new Map();
  1224. initialNodes.forEach(node => {
  1225. nodeMap.set(node.id, node);
  1226. });
  1227. // 为每个节点创建树节点的副本(允许多次出现)
  1228. const createTreeNode = (nodeId, pathKey) => {
  1229. const node = nodeMap.get(nodeId);
  1230. if (!node) return null;
  1231. return {
  1232. ...node,
  1233. treeKey: pathKey, // 唯一的树路径key,用于React key
  1234. children: []
  1235. };
  1236. };
  1237. // 构建父子关系映射:记录每个节点的所有父节点,去重边
  1238. const parentToChildren = new Map();
  1239. const childToParents = new Map();
  1240. initialEdges.forEach(edge => {
  1241. // 记录父->子关系(去重:同一个父节点到同一个子节点只记录一次)
  1242. if (!parentToChildren.has(edge.source)) {
  1243. parentToChildren.set(edge.source, []);
  1244. }
  1245. const children = parentToChildren.get(edge.source);
  1246. if (!children.includes(edge.target)) {
  1247. children.push(edge.target);
  1248. }
  1249. // 记录子->父关系(用于判断是否有多个父节点,也去重)
  1250. if (!childToParents.has(edge.target)) {
  1251. childToParents.set(edge.target, []);
  1252. }
  1253. const parents = childToParents.get(edge.target);
  1254. if (!parents.includes(edge.source)) {
  1255. parents.push(edge.source);
  1256. }
  1257. });
  1258. // 递归构建树
  1259. const buildSubtree = (nodeId, pathKey, visitedInPath) => {
  1260. // 避免循环引用:如果当前路径中已经访问过这个节点,跳过
  1261. if (visitedInPath.has(nodeId)) {
  1262. return null;
  1263. }
  1264. const treeNode = createTreeNode(nodeId, pathKey);
  1265. if (!treeNode) return null;
  1266. const newVisitedInPath = new Set(visitedInPath);
  1267. newVisitedInPath.add(nodeId);
  1268. const children = parentToChildren.get(nodeId) || [];
  1269. treeNode.children = children
  1270. .map((childId, index) => buildSubtree(childId, pathKey + '-' + childId + '-' + index, newVisitedInPath))
  1271. .filter(child => child !== null);
  1272. return treeNode;
  1273. };
  1274. // 找出所有根节点(没有入边的节点)
  1275. const hasParent = new Set();
  1276. initialEdges.forEach(edge => {
  1277. hasParent.add(edge.target);
  1278. });
  1279. const roots = [];
  1280. initialNodes.forEach((node, index) => {
  1281. if (!hasParent.has(node.id)) {
  1282. const treeNode = buildSubtree(node.id, 'root-' + node.id + '-' + index, new Set());
  1283. if (treeNode) roots.push(treeNode);
  1284. }
  1285. });
  1286. return roots;
  1287. }, [initialNodes, initialEdges]);
  1288. const treeRoots = useMemo(() => buildTree(), [buildTree]);
  1289. // 生成树形文本结构
  1290. const generateTreeText = useCallback(() => {
  1291. const lines = [];
  1292. // 递归生成树形文本
  1293. const traverse = (nodes, prefix = '', isLast = true, depth = 0) => {
  1294. nodes.forEach((node, index) => {
  1295. const isLastNode = index === nodes.length - 1;
  1296. const nodeData = initialNodes.find(n => n.id === node.id)?.data || {};
  1297. const nodeType = nodeData.nodeType || node.data?.nodeType || 'unknown';
  1298. const title = nodeData.title || node.data?.title || node.id;
  1299. // 优先从node.data获取score,然后从nodeData获取
  1300. let score = null;
  1301. if (node.data?.score !== undefined && node.data?.score !== null) {
  1302. score = node.data.score;
  1303. } else if (node.data?.relevance_score !== undefined && node.data?.relevance_score !== null) {
  1304. score = node.data.relevance_score;
  1305. } else if (nodeData.score !== undefined && nodeData.score !== null) {
  1306. score = nodeData.score;
  1307. } else if (nodeData.relevance_score !== undefined && nodeData.relevance_score !== null) {
  1308. score = nodeData.relevance_score;
  1309. }
  1310. const strategy = nodeData.strategy || node.data?.strategy || '';
  1311. // 构建当前行 - score可能是数字或字符串,step/round节点不显示分数
  1312. const connector = isLastNode ? '└─' : '├─';
  1313. let scoreText = '';
  1314. if (nodeType !== 'step' && nodeType !== 'round' && score !== null && score !== undefined) {
  1315. // score可能已经是字符串格式(如 "0.05"),也可能是数字
  1316. const scoreStr = typeof score === 'number' ? score.toFixed(2) : score;
  1317. scoreText = \` (分数: \${scoreStr})\`;
  1318. }
  1319. const strategyText = strategy ? \` [\${strategy}]\` : '';
  1320. lines.push(\`\${prefix}\${connector} \${title}\${scoreText}\${strategyText}\`);
  1321. // 递归处理子节点
  1322. if (node.children && node.children.length > 0) {
  1323. const childPrefix = prefix + (isLastNode ? ' ' : '│ ');
  1324. traverse(node.children, childPrefix, isLastNode, depth + 1);
  1325. }
  1326. });
  1327. };
  1328. // 添加标题
  1329. const rootNode = initialNodes.find(n => n.data?.level === 0);
  1330. if (rootNode) {
  1331. lines.push(\`📊 查询扩展树形结构\`);
  1332. lines.push(\`原始问题: \${rootNode.data.title || rootNode.data.query}\`);
  1333. lines.push('');
  1334. }
  1335. traverse(treeRoots);
  1336. return lines.join('\\n');
  1337. }, [treeRoots, initialNodes]);
  1338. // 复制树形结构到剪贴板
  1339. const copyTreeToClipboard = useCallback(async () => {
  1340. try {
  1341. const treeText = generateTreeText();
  1342. await navigator.clipboard.writeText(treeText);
  1343. alert('✅ 树形结构已复制到剪贴板!');
  1344. } catch (err) {
  1345. console.error('复制失败:', err);
  1346. alert('❌ 复制失败,请手动复制');
  1347. }
  1348. }, [generateTreeText]);
  1349. // 初始化树节点折叠状态
  1350. useEffect(() => {
  1351. const getAllTreeKeys = (nodes) => {
  1352. const keys = new Set();
  1353. const traverse = (node) => {
  1354. if (node.children && node.children.length > 0) {
  1355. // 排除根节点
  1356. if (node.data.level !== 0) {
  1357. keys.add(node.treeKey);
  1358. }
  1359. node.children.forEach(traverse);
  1360. }
  1361. };
  1362. nodes.forEach(traverse);
  1363. return keys;
  1364. };
  1365. setCollapsedTreeNodes(getAllTreeKeys(treeRoots));
  1366. }, [treeRoots]);
  1367. const renderTree = useCallback((treeNodes, level = 0) => {
  1368. return treeNodes.map(node => {
  1369. // 使用 treeKey 来区分树中的不同实例
  1370. const isCollapsed = collapsedTreeNodes.has(node.treeKey);
  1371. const isSelected = selectedNodeId === node.id;
  1372. return (
  1373. <TreeNode
  1374. key={node.treeKey}
  1375. node={node}
  1376. level={level}
  1377. isCollapsed={isCollapsed}
  1378. isSelected={isSelected}
  1379. onToggle={() => {
  1380. setCollapsedTreeNodes(prev => {
  1381. const newSet = new Set(prev);
  1382. if (newSet.has(node.treeKey)) {
  1383. newSet.delete(node.treeKey);
  1384. } else {
  1385. newSet.add(node.treeKey);
  1386. }
  1387. return newSet;
  1388. });
  1389. }}
  1390. onSelect={() => {
  1391. const nodeId = node.id;
  1392. // 展开所有祖先节点
  1393. const ancestorIds = [nodeId];
  1394. const findAncestors = (id) => {
  1395. initialEdges.forEach(edge => {
  1396. if (edge.target === id && !ancestorIds.includes(edge.source)) {
  1397. ancestorIds.push(edge.source);
  1398. findAncestors(edge.source);
  1399. }
  1400. });
  1401. };
  1402. findAncestors(nodeId);
  1403. // 如果节点或其祖先被隐藏,先恢复它们
  1404. setHiddenNodes(prev => {
  1405. const newSet = new Set(prev);
  1406. ancestorIds.forEach(id => newSet.delete(id));
  1407. return newSet;
  1408. });
  1409. setSelectedNodeId(nodeId);
  1410. // 获取选中节点的直接子节点
  1411. const childrenIds = [];
  1412. initialEdges.forEach(edge => {
  1413. if (edge.source === nodeId) {
  1414. childrenIds.push(edge.target);
  1415. }
  1416. });
  1417. setCollapsedNodes(prev => {
  1418. const newSet = new Set(prev);
  1419. // 展开所有祖先节点
  1420. ancestorIds.forEach(id => newSet.delete(id));
  1421. // 展开选中节点本身
  1422. newSet.delete(nodeId);
  1423. // 展开选中节点的直接子节点
  1424. childrenIds.forEach(id => newSet.delete(id));
  1425. return newSet;
  1426. });
  1427. // 延迟聚焦,等待节点展开和布局重新计算
  1428. setTimeout(() => {
  1429. fitView({
  1430. nodes: [{ id: nodeId }],
  1431. duration: 800,
  1432. padding: 0.3,
  1433. });
  1434. }, 300);
  1435. }}
  1436. >
  1437. {node.children && node.children.length > 0 && renderTree(node.children, level + 1)}
  1438. </TreeNode>
  1439. );
  1440. });
  1441. }, [collapsedTreeNodes, selectedNodeId, nodes, setCenter, initialEdges, setCollapsedNodes, fitView]);
  1442. console.log('📊 Rendering with', nodes.length, 'visible nodes and', edges.length, 'visible edges');
  1443. if (nodes.length === 0) {
  1444. return (
  1445. <div style={{ padding: 50, color: 'red', fontSize: 20 }}>
  1446. ERROR: No nodes to display!
  1447. </div>
  1448. );
  1449. }
  1450. return (
  1451. <div style={{ width: '100vw', height: '100vh', background: '#f9fafb', display: 'flex', flexDirection: 'column' }}>
  1452. {/* 顶部面包屑导航栏 */}
  1453. <div style={{
  1454. minHeight: '48px',
  1455. maxHeight: '120px',
  1456. background: 'white',
  1457. borderBottom: '1px solid #e5e7eb',
  1458. display: 'flex',
  1459. alignItems: 'flex-start',
  1460. padding: '12px 24px',
  1461. zIndex: 1000,
  1462. boxShadow: '0 1px 3px rgba(0, 0, 0, 0.05)',
  1463. flexShrink: 0,
  1464. overflowY: 'auto',
  1465. }}>
  1466. <div style={{ width: '100%' }}>
  1467. {selectedNodeId ? (
  1468. <div style={{ fontSize: '12px', color: '#6b7280' }}>
  1469. {/* 面包屑导航 - 显示所有路径 */}
  1470. {(() => {
  1471. const selectedNode = nodes.find(n => n.id === selectedNodeId);
  1472. if (!selectedNode) return null;
  1473. // 找到所有从根节点到当前节点的路径
  1474. const findAllPaths = (targetId) => {
  1475. const paths = [];
  1476. const buildPath = (nodeId, currentPath) => {
  1477. const node = initialNodes.find(n => n.id === nodeId);
  1478. if (!node) return;
  1479. const newPath = [node, ...currentPath];
  1480. // 找到所有父节点
  1481. const parents = initialEdges.filter(e => e.target === nodeId).map(e => e.source);
  1482. if (parents.length === 0) {
  1483. // 到达根节点
  1484. paths.push(newPath);
  1485. } else {
  1486. // 递归处理所有父节点
  1487. parents.forEach(parentId => {
  1488. buildPath(parentId, newPath);
  1489. });
  1490. }
  1491. };
  1492. buildPath(targetId, []);
  1493. return paths;
  1494. };
  1495. const allPaths = findAllPaths(selectedNodeId);
  1496. // 去重:将路径转换为字符串进行比较
  1497. const uniquePaths = [];
  1498. const pathStrings = new Set();
  1499. allPaths.forEach(path => {
  1500. const pathString = path.map(n => n.id).join('->');
  1501. if (!pathStrings.has(pathString)) {
  1502. pathStrings.add(pathString);
  1503. uniquePaths.push(path);
  1504. }
  1505. });
  1506. return (
  1507. <div style={{ display: 'flex', flexDirection: 'column', gap: '6px' }}>
  1508. {uniquePaths.map((path, pathIndex) => (
  1509. <div key={pathIndex} style={{ display: 'flex', alignItems: 'center', gap: '6px', flexWrap: 'wrap' }}>
  1510. {pathIndex > 0 && <span style={{ color: '#d1d5db', marginRight: '4px' }}>或</span>}
  1511. {path.map((node, index) => {
  1512. // 获取节点的 score、strategy 和 isSelected
  1513. const nodeScore = node.data.score ? parseFloat(node.data.score) : 0;
  1514. const nodeStrategy = node.data.strategy || '';
  1515. const strategyColor = getStrategyColor(nodeStrategy);
  1516. const nodeIsSelected = node.type === 'note' ? node.data.matchLevel !== 'unsatisfied' : node.data.isSelected !== false;
  1517. const nodeActualType = node.data.nodeType || node.type; // 获取实际节点类型
  1518. return (
  1519. <React.Fragment key={node.id + '-' + index}>
  1520. <span
  1521. onClick={() => {
  1522. const nodeId = node.id;
  1523. // 找到所有祖先节点
  1524. const ancestorIds = [nodeId];
  1525. const findAncestors = (id) => {
  1526. initialEdges.forEach(edge => {
  1527. if (edge.target === id && !ancestorIds.includes(edge.source)) {
  1528. ancestorIds.push(edge.source);
  1529. findAncestors(edge.source);
  1530. }
  1531. });
  1532. };
  1533. findAncestors(nodeId);
  1534. // 如果节点或其祖先被隐藏,先恢复它们
  1535. setHiddenNodes(prev => {
  1536. const newSet = new Set(prev);
  1537. ancestorIds.forEach(id => newSet.delete(id));
  1538. return newSet;
  1539. });
  1540. // 展开目录树中到达该节点的路径
  1541. // 需要找到所有包含该节点的树路径的 treeKey,并展开它们的父节点
  1542. setCollapsedTreeNodes(prev => {
  1543. const newSet = new Set(prev);
  1544. // 清空所有折叠状态,让目录树完全展开到选中节点
  1545. // 这样可以确保选中节点在目录中可见
  1546. return new Set();
  1547. });
  1548. setSelectedNodeId(nodeId);
  1549. setTimeout(() => {
  1550. fitView({
  1551. nodes: [{ id: nodeId }],
  1552. duration: 800,
  1553. padding: 0.3,
  1554. });
  1555. }, 100);
  1556. }}
  1557. style={{
  1558. padding: '6px 8px',
  1559. borderRadius: '4px',
  1560. background: 'white',
  1561. border: index === path.length - 1 ? '2px solid #3b82f6' : '1px solid #d1d5db',
  1562. color: '#374151',
  1563. fontWeight: index === path.length - 1 ? '600' : '400',
  1564. width: '180px',
  1565. cursor: 'pointer',
  1566. transition: 'all 0.2s ease',
  1567. position: 'relative',
  1568. display: 'inline-flex',
  1569. flexDirection: 'column',
  1570. gap: '4px',
  1571. }}
  1572. onMouseEnter={(e) => {
  1573. e.currentTarget.style.opacity = '0.8';
  1574. }}
  1575. onMouseLeave={(e) => {
  1576. e.currentTarget.style.opacity = '1';
  1577. }}
  1578. title={\`\${node.data.title || node.id} (Score: \${nodeScore.toFixed(2)}, Strategy: \${nodeStrategy}, Selected: \${nodeIsSelected})\`}
  1579. >
  1580. {/* 上半部分:竖线 + 图标 + 文字 + 分数 */}
  1581. <div style={{
  1582. display: 'flex',
  1583. alignItems: 'center',
  1584. gap: '6px',
  1585. }}>
  1586. {/* 策略类型竖线 */}
  1587. <div style={{
  1588. width: '3px',
  1589. height: '16px',
  1590. background: strategyColor,
  1591. borderRadius: '2px',
  1592. flexShrink: 0,
  1593. }} />
  1594. {/* 节点文字 */}
  1595. <span style={{
  1596. flex: 1,
  1597. fontSize: '12px',
  1598. color: nodeIsSelected ? '#374151' : '#ef4444',
  1599. }}>
  1600. {truncateMiddle(node.data.title || node.id, 18)}
  1601. </span>
  1602. {/* 分数显示 - 步骤和轮次节点不显示分数 */}
  1603. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  1604. <span style={{
  1605. fontSize: '10px',
  1606. color: '#6b7280',
  1607. fontWeight: '500',
  1608. flexShrink: 0,
  1609. }}>
  1610. {nodeScore.toFixed(2)}
  1611. </span>
  1612. )}
  1613. </div>
  1614. {/* 分数下划线 - 步骤和轮次节点不显示 */}
  1615. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  1616. <div style={{
  1617. width: (nodeScore * 100) + '%',
  1618. height: '2px',
  1619. background: getScoreColor(nodeScore),
  1620. borderRadius: '1px',
  1621. marginLeft: '9px',
  1622. }} />
  1623. )}
  1624. </span>
  1625. {index < path.length - 1 && <span style={{ color: '#9ca3af' }}>›</span>}
  1626. </React.Fragment>
  1627. )})}
  1628. </div>
  1629. ))}
  1630. </div>
  1631. );
  1632. })()}
  1633. </div>
  1634. ) : (
  1635. <div style={{ fontSize: '13px', color: '#9ca3af', textAlign: 'center' }}>
  1636. 选择一个节点查看路径
  1637. </div>
  1638. )}
  1639. </div>
  1640. </div>
  1641. {/* 主内容区:目录 + 画布 */}
  1642. <div style={{ display: 'flex', flex: 1, overflow: 'hidden' }}>
  1643. {/* 左侧目录树 */}
  1644. <div style={{
  1645. width: '320px',
  1646. background: 'white',
  1647. borderRight: '1px solid #e5e7eb',
  1648. display: 'flex',
  1649. flexDirection: 'column',
  1650. flexShrink: 0,
  1651. }}>
  1652. <div style={{
  1653. padding: '12px 16px',
  1654. borderBottom: '1px solid #e5e7eb',
  1655. display: 'flex',
  1656. justifyContent: 'space-between',
  1657. alignItems: 'center',
  1658. }}>
  1659. <span style={{
  1660. fontWeight: '600',
  1661. fontSize: '14px',
  1662. color: '#111827',
  1663. }}>
  1664. 节点目录
  1665. </span>
  1666. <div style={{ display: 'flex', gap: '6px' }}>
  1667. <button
  1668. onClick={() => {
  1669. setCollapsedTreeNodes(new Set());
  1670. }}
  1671. style={{
  1672. fontSize: '11px',
  1673. padding: '4px 8px',
  1674. borderRadius: '4px',
  1675. border: '1px solid #d1d5db',
  1676. background: 'white',
  1677. color: '#6b7280',
  1678. cursor: 'pointer',
  1679. fontWeight: '500',
  1680. }}
  1681. title="展开全部节点"
  1682. >
  1683. 全部展开
  1684. </button>
  1685. <button
  1686. onClick={() => {
  1687. const getAllTreeKeys = (nodes) => {
  1688. const keys = new Set();
  1689. const traverse = (node) => {
  1690. if (node.children && node.children.length > 0) {
  1691. keys.add(node.treeKey);
  1692. node.children.forEach(traverse);
  1693. }
  1694. };
  1695. nodes.forEach(traverse);
  1696. return keys;
  1697. };
  1698. setCollapsedTreeNodes(getAllTreeKeys(treeRoots));
  1699. }}
  1700. style={{
  1701. fontSize: '11px',
  1702. padding: '4px 8px',
  1703. borderRadius: '4px',
  1704. border: '1px solid #d1d5db',
  1705. background: 'white',
  1706. color: '#6b7280',
  1707. cursor: 'pointer',
  1708. fontWeight: '500',
  1709. }}
  1710. title="折叠全部节点"
  1711. >
  1712. 全部折叠
  1713. </button>
  1714. <button
  1715. onClick={copyTreeToClipboard}
  1716. style={{
  1717. fontSize: '11px',
  1718. padding: '4px 8px',
  1719. borderRadius: '4px',
  1720. border: '1px solid #3b82f6',
  1721. background: '#3b82f6',
  1722. color: 'white',
  1723. cursor: 'pointer',
  1724. fontWeight: '500',
  1725. transition: 'all 0.2s',
  1726. }}
  1727. onMouseEnter={(e) => e.currentTarget.style.background = '#2563eb'}
  1728. onMouseLeave={(e) => e.currentTarget.style.background = '#3b82f6'}
  1729. title="复制树形结构为文本格式"
  1730. >
  1731. 📋 复制树形结构
  1732. </button>
  1733. </div>
  1734. </div>
  1735. <div style={{
  1736. flex: 1,
  1737. overflowX: 'auto',
  1738. overflowY: 'auto',
  1739. padding: '8px',
  1740. }}>
  1741. <div style={{ minWidth: 'fit-content' }}>
  1742. {renderTree(treeRoots)}
  1743. </div>
  1744. </div>
  1745. </div>
  1746. {/* 画布区域 */}
  1747. <div style={{ flex: 1, position: 'relative' }}>
  1748. {/* 右侧图例 */}
  1749. <div style={{
  1750. position: 'absolute',
  1751. top: '20px',
  1752. right: '20px',
  1753. background: 'white',
  1754. padding: '16px',
  1755. borderRadius: '12px',
  1756. boxShadow: '0 4px 12px rgba(0, 0, 0, 0.08)',
  1757. zIndex: 1000,
  1758. maxWidth: '260px',
  1759. border: '1px solid #e5e7eb',
  1760. }}>
  1761. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '12px' }}>
  1762. <h3 style={{ fontSize: '14px', fontWeight: '600', color: '#111827', margin: 0 }}>图例</h3>
  1763. <button
  1764. onClick={() => setFocusMode(!focusMode)}
  1765. style={{
  1766. fontSize: '11px',
  1767. padding: '4px 8px',
  1768. borderRadius: '4px',
  1769. border: '1px solid',
  1770. borderColor: focusMode ? '#3b82f6' : '#d1d5db',
  1771. background: focusMode ? '#3b82f6' : 'white',
  1772. color: focusMode ? 'white' : '#6b7280',
  1773. cursor: 'pointer',
  1774. fontWeight: '500',
  1775. }}
  1776. title={focusMode ? '关闭聚焦模式' : '开启聚焦模式'}
  1777. >
  1778. {focusMode ? '🎯 聚焦' : '📊 全图'}
  1779. </button>
  1780. </div>
  1781. <div style={{ fontSize: '12px' }}>
  1782. {/* 画布节点展开/折叠控制 */}
  1783. <div style={{ marginBottom: '12px', paddingBottom: '12px', borderBottom: '1px solid #f3f4f6' }}>
  1784. <div style={{ fontSize: '12px', fontWeight: '500', marginBottom: '8px', color: '#374151' }}>节点控制</div>
  1785. <div style={{ display: 'flex', gap: '6px' }}>
  1786. <button
  1787. onClick={() => {
  1788. setCollapsedNodes(new Set());
  1789. }}
  1790. style={{
  1791. fontSize: '11px',
  1792. padding: '4px 8px',
  1793. borderRadius: '4px',
  1794. border: '1px solid #d1d5db',
  1795. background: 'white',
  1796. color: '#6b7280',
  1797. cursor: 'pointer',
  1798. fontWeight: '500',
  1799. flex: 1,
  1800. }}
  1801. title="展开画布中所有节点的子节点"
  1802. >
  1803. 全部展开
  1804. </button>
  1805. <button
  1806. onClick={() => {
  1807. const allNodeIds = new Set(initialNodes.map(n => n.id));
  1808. setCollapsedNodes(allNodeIds);
  1809. }}
  1810. style={{
  1811. fontSize: '11px',
  1812. padding: '4px 8px',
  1813. borderRadius: '4px',
  1814. border: '1px solid #d1d5db',
  1815. background: 'white',
  1816. color: '#6b7280',
  1817. cursor: 'pointer',
  1818. fontWeight: '500',
  1819. flex: 1,
  1820. }}
  1821. title="折叠画布中所有节点的子节点"
  1822. >
  1823. 全部折叠
  1824. </button>
  1825. </div>
  1826. </div>
  1827. <div style={{ paddingTop: '12px', borderTop: '1px solid #f3f4f6' }}>
  1828. <div style={{ fontSize: '12px', fontWeight: '500', marginBottom: '8px', color: '#374151' }}>策略类型</div>
  1829. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1830. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#10b981', opacity: 0.7 }}></div>
  1831. <span style={{ color: '#6b7280', fontSize: '11px' }}>初始分词</span>
  1832. </div>
  1833. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1834. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#06b6d4', opacity: 0.7 }}></div>
  1835. <span style={{ color: '#6b7280', fontSize: '11px' }}>调用sug</span>
  1836. </div>
  1837. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1838. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#f59e0b', opacity: 0.7 }}></div>
  1839. <span style={{ color: '#6b7280', fontSize: '11px' }}>同义改写</span>
  1840. </div>
  1841. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1842. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#3b82f6', opacity: 0.7 }}></div>
  1843. <span style={{ color: '#6b7280', fontSize: '11px' }}>加词</span>
  1844. </div>
  1845. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1846. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#8b5cf6', opacity: 0.7 }}></div>
  1847. <span style={{ color: '#6b7280', fontSize: '11px' }}>抽象改写</span>
  1848. </div>
  1849. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1850. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#ec4899', opacity: 0.7 }}></div>
  1851. <span style={{ color: '#6b7280', fontSize: '11px' }}>基于部分匹配改进</span>
  1852. </div>
  1853. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1854. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#a855f7', opacity: 0.7 }}></div>
  1855. <span style={{ color: '#6b7280', fontSize: '11px' }}>结果分支-抽象改写</span>
  1856. </div>
  1857. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  1858. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#fb923c', opacity: 0.7 }}></div>
  1859. <span style={{ color: '#6b7280', fontSize: '11px' }}>结果分支-同义改写</span>
  1860. </div>
  1861. </div>
  1862. <div style={{
  1863. marginTop: '12px',
  1864. paddingTop: '12px',
  1865. borderTop: '1px solid #f3f4f6',
  1866. fontSize: '11px',
  1867. color: '#9ca3af',
  1868. lineHeight: '1.5',
  1869. }}>
  1870. 💡 点击节点左上角 × 隐藏节点
  1871. </div>
  1872. {/* 隐藏节点列表 - 在图例内部 */}
  1873. {hiddenNodes.size > 0 && (
  1874. <div style={{
  1875. marginTop: '12px',
  1876. paddingTop: '12px',
  1877. borderTop: '1px solid #f3f4f6',
  1878. }}>
  1879. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '8px' }}>
  1880. <h4 style={{ fontSize: '12px', fontWeight: '600', color: '#111827' }}>已隐藏节点</h4>
  1881. <button
  1882. onClick={() => setHiddenNodes(new Set())}
  1883. style={{
  1884. fontSize: '10px',
  1885. color: '#3b82f6',
  1886. background: 'none',
  1887. border: 'none',
  1888. cursor: 'pointer',
  1889. textDecoration: 'underline',
  1890. }}
  1891. >
  1892. 全部恢复
  1893. </button>
  1894. </div>
  1895. <div style={{ fontSize: '12px', maxHeight: '200px', overflow: 'auto' }}>
  1896. {Array.from(hiddenNodes).map(nodeId => {
  1897. const node = initialNodes.find(n => n.id === nodeId);
  1898. if (!node) return null;
  1899. return (
  1900. <div
  1901. key={nodeId}
  1902. style={{
  1903. display: 'flex',
  1904. justifyContent: 'space-between',
  1905. alignItems: 'center',
  1906. padding: '6px 8px',
  1907. margin: '4px 0',
  1908. background: '#f9fafb',
  1909. borderRadius: '6px',
  1910. fontSize: '11px',
  1911. }}
  1912. >
  1913. <span
  1914. style={{
  1915. flex: 1,
  1916. overflow: 'hidden',
  1917. textOverflow: 'ellipsis',
  1918. whiteSpace: 'nowrap',
  1919. color: '#374151',
  1920. }}
  1921. title={node.data.title || nodeId}
  1922. >
  1923. {node.data.title || nodeId}
  1924. </span>
  1925. <button
  1926. onClick={() => {
  1927. setHiddenNodes(prev => {
  1928. const newSet = new Set(prev);
  1929. newSet.delete(nodeId);
  1930. return newSet;
  1931. });
  1932. }}
  1933. style={{
  1934. marginLeft: '8px',
  1935. fontSize: '10px',
  1936. color: '#10b981',
  1937. background: 'none',
  1938. border: 'none',
  1939. cursor: 'pointer',
  1940. flexShrink: 0,
  1941. }}
  1942. >
  1943. 恢复
  1944. </button>
  1945. </div>
  1946. );
  1947. })}
  1948. </div>
  1949. </div>
  1950. )}
  1951. </div>
  1952. </div>
  1953. {/* React Flow 画布 */}
  1954. <ReactFlow
  1955. nodes={nodes}
  1956. edges={edges}
  1957. nodeTypes={nodeTypes}
  1958. fitView
  1959. fitViewOptions={{ padding: 0.2, duration: 500 }}
  1960. minZoom={0.1}
  1961. maxZoom={1.5}
  1962. nodesDraggable={true}
  1963. nodesConnectable={false}
  1964. elementsSelectable={true}
  1965. defaultEdgeOptions={{
  1966. type: 'smoothstep',
  1967. }}
  1968. proOptions={{ hideAttribution: true }}
  1969. onNodeClick={(event, clickedNode) => {
  1970. setSelectedNodeId(clickedNode.id);
  1971. }}
  1972. >
  1973. <Controls style={{ bottom: '20px', left: 'auto', right: '20px' }} />
  1974. <Background variant="dots" gap={20} size={1} color="#e5e7eb" />
  1975. </ReactFlow>
  1976. </div>
  1977. </div>
  1978. </div>
  1979. );
  1980. }
  1981. function App() {
  1982. return (
  1983. <ReactFlowProvider>
  1984. <FlowContent />
  1985. </ReactFlowProvider>
  1986. );
  1987. }
  1988. const root = createRoot(document.getElementById('root'));
  1989. root.render(<App />);
  1990. `;
  1991. fs.writeFileSync(reactComponentPath, reactComponent);
  1992. // 使用 esbuild 打包
  1993. console.log('🎨 Building modern visualization...');
  1994. build({
  1995. entryPoints: [reactComponentPath],
  1996. bundle: true,
  1997. outfile: path.join(__dirname, 'bundle_v2.js'),
  1998. format: 'iife',
  1999. loader: {
  2000. '.css': 'css',
  2001. },
  2002. minify: false,
  2003. sourcemap: 'inline',
  2004. // 强制所有 React 引用指向同一个位置,避免多副本
  2005. alias: {
  2006. 'react': path.join(__dirname, 'node_modules/react'),
  2007. 'react-dom': path.join(__dirname, 'node_modules/react-dom'),
  2008. 'react/jsx-runtime': path.join(__dirname, 'node_modules/react/jsx-runtime'),
  2009. 'react/jsx-dev-runtime': path.join(__dirname, 'node_modules/react/jsx-dev-runtime'),
  2010. },
  2011. }).then(() => {
  2012. // 读取打包后的 JS
  2013. const bundleJs = fs.readFileSync(path.join(__dirname, 'bundle_v2.js'), 'utf-8');
  2014. // 读取 CSS
  2015. const cssPath = path.join(__dirname, 'node_modules/@xyflow/react/dist/style.css');
  2016. const css = fs.readFileSync(cssPath, 'utf-8');
  2017. // 生成最终 HTML
  2018. const html = `<!DOCTYPE html>
  2019. <html lang="zh-CN">
  2020. <head>
  2021. <meta charset="UTF-8">
  2022. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  2023. <title>查询图可视化</title>
  2024. <link rel="preconnect" href="https://fonts.googleapis.com">
  2025. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  2026. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
  2027. <script src="https://unpkg.com/dagre@0.8.5/dist/dagre.min.js"></script>
  2028. <script>
  2029. // 过滤特定的 React 警告
  2030. const originalError = console.error;
  2031. console.error = (...args) => {
  2032. if (typeof args[0] === 'string' && args[0].includes('Each child in a list should have a unique "key" prop')) {
  2033. return;
  2034. }
  2035. originalError.apply(console, args);
  2036. };
  2037. </script>
  2038. <style>
  2039. * {
  2040. margin: 0;
  2041. padding: 0;
  2042. box-sizing: border-box;
  2043. }
  2044. body {
  2045. font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  2046. overflow: hidden;
  2047. -webkit-font-smoothing: antialiased;
  2048. -moz-osx-font-smoothing: grayscale;
  2049. }
  2050. #root {
  2051. width: 100vw;
  2052. height: 100vh;
  2053. }
  2054. ${css}
  2055. /* 自定义样式覆盖 */
  2056. .react-flow__edge-path {
  2057. stroke-linecap: round;
  2058. }
  2059. .react-flow__controls {
  2060. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  2061. border: 1px solid #e5e7eb;
  2062. border-radius: 8px;
  2063. }
  2064. .react-flow__controls-button {
  2065. border: none;
  2066. border-bottom: 1px solid #e5e7eb;
  2067. }
  2068. .react-flow__controls-button:hover {
  2069. background: #f9fafb;
  2070. }
  2071. </style>
  2072. </head>
  2073. <body>
  2074. <div id="root"></div>
  2075. <script>${bundleJs}</script>
  2076. </body>
  2077. </html>`;
  2078. // 写入输出文件
  2079. fs.writeFileSync(outputFile, html);
  2080. // 清理临时文件
  2081. fs.unlinkSync(reactComponentPath);
  2082. fs.unlinkSync(path.join(__dirname, 'bundle_v2.js'));
  2083. console.log('✅ Visualization generated: ' + outputFile);
  2084. console.log('📊 Nodes: ' + Object.keys(data.nodes).length);
  2085. console.log('🔗 Edges: ' + data.edges.length);
  2086. }).catch(error => {
  2087. console.error('❌ Build error:', error);
  2088. process.exit(1);
  2089. });