index.js 83 KB

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