index.js 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256
  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'); // 已废弃,使用v3版本
  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. // 尝试读取 search_extract.json(多模态提取数据)
  33. let extractionData = null;
  34. const extractionPath = path.join(path.dirname(inputFile), 'search_extract.json');
  35. if (fs.existsSync(extractionPath)) {
  36. console.log('📸 读取多模态提取数据...');
  37. extractionData = JSON.parse(fs.readFileSync(extractionPath, 'utf-8'));
  38. } else {
  39. console.log('ℹ️ 未找到 search_extract.json,跳过多模态展示');
  40. }
  41. // 选择转换函数
  42. let graphData;
  43. let fullData = null; // 用于目录的完整数据
  44. if (useSimplified) {
  45. console.log('🎨 使用简化视图(合并query节点)');
  46. // 生成简化版用于画布
  47. graphData = convertV8ToGraphSimplified(inputData, searchResults, extractionData);
  48. // 生成完整版用于目录
  49. const fullGraphData = convertV8ToGraphV2(inputData, searchResults, extractionData);
  50. fullData = {
  51. nodes: fullGraphData.nodes,
  52. edges: fullGraphData.edges,
  53. iterations: fullGraphData.iterations
  54. };
  55. console.log(`✅ 简化版: ${Object.keys(graphData.nodes).length} 个节点, ${graphData.edges.length} 条边`);
  56. console.log(`📋 完整版(用于目录): ${Object.keys(fullData.nodes).length} 个节点`);
  57. } else {
  58. console.log('📊 使用详细视图(完整流程)');
  59. graphData = convertV8ToGraphV2(inputData, searchResults, extractionData);
  60. console.log(`✅ 转换完成: ${Object.keys(graphData.nodes).length} 个节点, ${graphData.edges.length} 条边`);
  61. }
  62. data = {
  63. nodes: graphData.nodes,
  64. edges: graphData.edges,
  65. iterations: graphData.iterations,
  66. fullData: fullData // 传递完整数据
  67. };
  68. } else if (inputData.nodes && inputData.edges) {
  69. // v6.1.2.5 格式,直接使用
  70. console.log('✨ 检测到 v6.1.2.5 格式,直接使用');
  71. data = inputData;
  72. } else {
  73. console.error('❌ 无法识别的数据格式');
  74. process.exit(1);
  75. }
  76. // 创建临时 React 组件文件
  77. const reactComponentPath = path.join(__dirname, 'temp_flow_component_v2.jsx');
  78. const reactComponent = `
  79. import React, { useState, useCallback, useMemo, useEffect } from 'react';
  80. import { createRoot } from 'react-dom/client';
  81. import {
  82. ReactFlow,
  83. Controls,
  84. Background,
  85. useNodesState,
  86. useEdgesState,
  87. Handle,
  88. Position,
  89. useReactFlow,
  90. ReactFlowProvider,
  91. } from '@xyflow/react';
  92. import '@xyflow/react/dist/style.css';
  93. const data = ${JSON.stringify(data, null, 2)};
  94. // 根据节点类型获取边框颜色
  95. function getNodeTypeColor(type) {
  96. const typeColors = {
  97. 'root': '#6b21a8', // 紫色 - 根节点
  98. 'round': '#7c3aed', // 深紫 - Round节点
  99. 'step': '#f59e0b', // 橙色 - 步骤节点
  100. 'seg': '#10b981', // 绿色 - 分词
  101. 'q': '#3b82f6', // 蓝色 - Query
  102. 'sug': '#06b6d4', // 青色 - Sug建议词
  103. 'seed': '#84cc16', // 黄绿 - Seed
  104. 'add_word': '#22c55e', // 绿色 - 加词生成
  105. 'search_word': '#8b5cf6', // 紫色 - 搜索词
  106. 'post': '#ec4899', // 粉色 - 帖子
  107. 'filtered_sug': '#14b8a6',// 青绿 - 筛选的sug
  108. 'next_q': '#2563eb', // 深蓝 - 下轮查询
  109. 'next_seed': '#65a30d', // 深黄绿 - 下轮种子
  110. 'search': '#8b5cf6', // 深紫 - 搜索(兼容旧版)
  111. 'operation': '#f59e0b', // 橙色 - 操作节点(兼容旧版)
  112. 'query': '#3b82f6', // 蓝色 - 查询(兼容旧版)
  113. 'note': '#ec4899', // 粉色 - 帖子(兼容旧版)
  114. };
  115. return typeColors[type] || '#9ca3af';
  116. }
  117. // 查询节点组件 - 卡片样式
  118. function QueryNode({ id, data, sourcePosition, targetPosition }) {
  119. // 所有节点默认展开
  120. const expanded = true;
  121. // 获取节点类型颜色
  122. const typeColor = getNodeTypeColor(data.nodeType || 'query');
  123. return (
  124. <div>
  125. <Handle
  126. type="target"
  127. position={targetPosition || Position.Left}
  128. style={{ background: typeColor, width: 8, height: 8 }}
  129. />
  130. <div
  131. style={{
  132. padding: '12px',
  133. borderRadius: '8px',
  134. border: data.isHighlighted ? \`3px solid \${typeColor}\` :
  135. data.isCollapsed ? \`2px solid \${typeColor}\` :
  136. data.isSelected === false ? '2px dashed #d1d5db' :
  137. \`2px solid \${typeColor}\`,
  138. background: data.isHighlighted ? '#eef2ff' :
  139. data.isSelected === false ? '#f9fafb' : 'white',
  140. minWidth: '200px',
  141. maxWidth: '280px',
  142. boxShadow: data.isHighlighted ? '0 0 0 4px rgba(102, 126, 234, 0.25), 0 4px 16px rgba(102, 126, 234, 0.4)' :
  143. data.isCollapsed ? '0 4px 12px rgba(102, 126, 234, 0.15)' :
  144. data.level === 0 ? '0 4px 12px rgba(139, 92, 246, 0.15)' : '0 2px 6px rgba(0, 0, 0, 0.06)',
  145. transition: 'all 0.3s ease',
  146. cursor: 'pointer',
  147. position: 'relative',
  148. opacity: data.isSelected === false ? 0.6 : 1,
  149. }}
  150. >
  151. {/* 折叠当前节点按钮 - 左边 */}
  152. <div
  153. style={{
  154. position: 'absolute',
  155. top: '6px',
  156. left: '6px',
  157. width: '20px',
  158. height: '20px',
  159. borderRadius: '50%',
  160. background: '#f59e0b',
  161. color: 'white',
  162. display: 'flex',
  163. alignItems: 'center',
  164. justifyContent: 'center',
  165. fontSize: '11px',
  166. fontWeight: 'bold',
  167. cursor: 'pointer',
  168. transition: 'all 0.2s ease',
  169. zIndex: 10,
  170. }}
  171. onClick={(e) => {
  172. e.stopPropagation();
  173. if (data.onHideSelf) {
  174. data.onHideSelf();
  175. }
  176. }}
  177. onMouseEnter={(e) => {
  178. e.currentTarget.style.background = '#d97706';
  179. }}
  180. onMouseLeave={(e) => {
  181. e.currentTarget.style.background = '#f59e0b';
  182. }}
  183. title="隐藏当前节点"
  184. >
  185. ×
  186. </div>
  187. {/* 聚焦按钮 - 右上角 */}
  188. <div
  189. style={{
  190. position: 'absolute',
  191. top: '6px',
  192. right: '6px',
  193. width: '20px',
  194. height: '20px',
  195. borderRadius: '50%',
  196. background: data.isFocused ? '#10b981' : '#e5e7eb',
  197. color: data.isFocused ? 'white' : '#6b7280',
  198. display: 'flex',
  199. alignItems: 'center',
  200. justifyContent: 'center',
  201. fontSize: '11px',
  202. fontWeight: 'bold',
  203. cursor: 'pointer',
  204. transition: 'all 0.2s ease',
  205. zIndex: 10,
  206. }}
  207. onClick={(e) => {
  208. e.stopPropagation();
  209. if (data.onFocus) {
  210. data.onFocus();
  211. }
  212. }}
  213. onMouseEnter={(e) => {
  214. if (!data.isFocused) {
  215. e.currentTarget.style.background = '#d1d5db';
  216. }
  217. }}
  218. onMouseLeave={(e) => {
  219. if (!data.isFocused) {
  220. e.currentTarget.style.background = '#e5e7eb';
  221. }
  222. }}
  223. title={data.isFocused ? '取消聚焦' : '聚焦到此节点'}
  224. >
  225. 🎯
  226. </div>
  227. {/* 折叠/展开子节点按钮 - 右边第二个位置 */}
  228. {data.hasChildren && (
  229. <div
  230. style={{
  231. position: 'absolute',
  232. top: '6px',
  233. right: '30px',
  234. width: '20px',
  235. height: '20px',
  236. borderRadius: '50%',
  237. background: data.isCollapsed ? '#667eea' : '#e5e7eb',
  238. color: data.isCollapsed ? 'white' : '#6b7280',
  239. display: 'flex',
  240. alignItems: 'center',
  241. justifyContent: 'center',
  242. fontSize: '11px',
  243. fontWeight: 'bold',
  244. cursor: 'pointer',
  245. transition: 'all 0.2s ease',
  246. zIndex: 10,
  247. }}
  248. onClick={(e) => {
  249. e.stopPropagation();
  250. data.onToggleCollapse();
  251. }}
  252. title={data.isCollapsed ? '展开子节点' : '折叠子节点'}
  253. >
  254. {data.isCollapsed ? '+' : '−'}
  255. </div>
  256. )}
  257. {/* 卡片内容 */}
  258. <div>
  259. {/* 标题行 */}
  260. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '8px', paddingLeft: '24px', paddingRight: data.hasChildren ? '54px' : '28px' }}>
  261. <div style={{ flex: 1 }}>
  262. <div style={{ display: 'flex', alignItems: 'center', gap: '4px', marginBottom: '3px' }}>
  263. <div style={{
  264. fontSize: '13px',
  265. fontWeight: data.level === 0 ? '700' : '600',
  266. color: data.level === 0 ? '#6b21a8' : '#1f2937',
  267. lineHeight: '1.3',
  268. flex: 1,
  269. }}>
  270. {data.title}
  271. </div>
  272. {data.isSelected === false && (
  273. <div style={{
  274. fontSize: '9px',
  275. padding: '1px 4px',
  276. borderRadius: '3px',
  277. background: '#fee2e2',
  278. color: '#991b1b',
  279. fontWeight: '500',
  280. flexShrink: 0,
  281. }}>
  282. 未选中
  283. </div>
  284. )}
  285. </div>
  286. </div>
  287. </div>
  288. {/* 展开的详细信息 - 始终显示 */}
  289. <div style={{ fontSize: '11px', lineHeight: 1.4 }}>
  290. <div style={{ display: 'flex', gap: '4px', marginBottom: '6px', flexWrap: 'wrap' }}>
  291. <span style={{
  292. display: 'inline-block',
  293. padding: '1px 6px',
  294. borderRadius: '10px',
  295. background: '#eff6ff',
  296. color: '#3b82f6',
  297. fontSize: '10px',
  298. fontWeight: '500',
  299. }}>
  300. Lv.{data.level}
  301. </span>
  302. <span style={{
  303. display: 'inline-block',
  304. padding: '1px 6px',
  305. borderRadius: '10px',
  306. background: '#f0fdf4',
  307. color: '#16a34a',
  308. fontSize: '10px',
  309. fontWeight: '500',
  310. }}>
  311. {data.score}
  312. </span>
  313. {data.strategy && data.strategy !== 'root' && (
  314. <span style={{
  315. display: 'inline-block',
  316. padding: '1px 6px',
  317. borderRadius: '10px',
  318. background: '#fef3c7',
  319. color: '#92400e',
  320. fontSize: '10px',
  321. fontWeight: '500',
  322. }}>
  323. {data.strategy}
  324. </span>
  325. )}
  326. {(data.typeLabel || data.type_label) && (
  327. <span style={{
  328. display: 'inline-block',
  329. padding: '1px 6px',
  330. borderRadius: '10px',
  331. background: '#fce7f3',
  332. color: '#9f1239',
  333. fontSize: '10px',
  334. fontWeight: '500',
  335. }}>
  336. {data.typeLabel || data.type_label}
  337. </span>
  338. )}
  339. {data.is_suggestion && data.suggestion_label && (
  340. <span style={{
  341. display: 'inline-block',
  342. padding: '1px 6px',
  343. borderRadius: '10px',
  344. background: '#ede9fe',
  345. color: '#6d28d9',
  346. fontSize: '10px',
  347. fontWeight: '600',
  348. }}>
  349. {data.suggestion_label}
  350. </span>
  351. )}
  352. </div>
  353. {data.parent && (
  354. <div style={{ color: '#6b7280', fontSize: '10px', marginTop: '4px', paddingTop: '4px', borderTop: '1px solid #f3f4f6' }}>
  355. <strong>Parent:</strong> {data.parent}
  356. </div>
  357. )}
  358. {data.nodeType === 'domain_combination' && Array.isArray(data.source_word_details) && data.source_word_details.length > 0 && (
  359. <div style={{
  360. marginTop: '6px',
  361. paddingTop: '6px',
  362. borderTop: '1px solid #f3f4f6',
  363. fontSize: '10px',
  364. color: '#6b7280',
  365. lineHeight: '1.5',
  366. }}>
  367. <strong style={{ color: '#4b5563' }}>来源词得分:</strong>
  368. <div style={{ marginTop: '4px', display: 'flex', flexDirection: 'column', gap: '4px' }}>
  369. {data.source_word_details.map((detail, idx) => {
  370. const words = (detail.words || []).map((w) => {
  371. const numericScore = typeof w.score === 'number' ? w.score : parseFloat(w.score || '0');
  372. const formattedScore = Number.isFinite(numericScore) ? numericScore.toFixed(2) : '0.00';
  373. return w.text + ' (' + formattedScore + ')';
  374. }).join(' + ');
  375. return (
  376. <div key={idx} style={{ display: 'flex', flexWrap: 'wrap', gap: '4px', alignItems: 'center' }}>
  377. <span style={{ color: '#2563eb' }}>{words}</span>
  378. </div>
  379. );
  380. })}
  381. </div>
  382. <div style={{ marginTop: '4px', fontWeight: '500', color: data.is_above_sources ? '#16a34a' : '#dc2626' }}>
  383. {data.is_above_sources ? '✅ 组合得分高于所有来源词' : '⚠️ 组合得分未超过全部来源词'}
  384. </div>
  385. </div>
  386. )}
  387. {data.selectedWord && (
  388. <div style={{
  389. marginTop: '6px',
  390. paddingTop: '6px',
  391. borderTop: '1px solid #f3f4f6',
  392. fontSize: '10px',
  393. color: '#6b7280',
  394. lineHeight: '1.5',
  395. }}>
  396. <strong style={{ color: '#4b5563' }}>选择词:</strong>
  397. <span style={{ marginLeft: '4px', color: '#3b82f6', fontWeight: '500' }}>{data.selectedWord}</span>
  398. {data.seed_score !== undefined && (
  399. <div style={{ marginTop: '4px' }}>
  400. <strong style={{ color: '#4b5563' }}>种子得分:</strong>
  401. <span style={{ marginLeft: '4px', color: '#16a34a', fontWeight: '500' }}>
  402. {typeof data.seed_score === 'number' ? data.seed_score.toFixed(2) : data.seed_score}
  403. </span>
  404. </div>
  405. )}
  406. </div>
  407. )}
  408. {data.evaluationReason && (
  409. <div style={{
  410. marginTop: '6px',
  411. paddingTop: '6px',
  412. borderTop: '1px solid #f3f4f6',
  413. fontSize: '10px',
  414. color: '#6b7280',
  415. lineHeight: '1.5',
  416. }}>
  417. <strong style={{ color: '#4b5563' }}>评估:</strong>
  418. <div style={{ marginTop: '2px' }}>{data.evaluationReason}</div>
  419. </div>
  420. )}
  421. {data.occurrences && data.occurrences.length > 1 && (
  422. <div style={{
  423. marginTop: '6px',
  424. paddingTop: '6px',
  425. borderTop: '1px solid #f3f4f6',
  426. fontSize: '10px',
  427. color: '#6b7280',
  428. }}>
  429. <strong style={{ color: '#4b5563' }}>演化历史 ({data.occurrences.length}次):</strong>
  430. <div style={{ marginTop: '4px' }}>
  431. {data.occurrences.map((occ, idx) => (
  432. <div key={idx} style={{ marginTop: '2px', paddingLeft: '8px' }}>
  433. <span style={{ color: '#3b82f6', fontWeight: '500' }}>R{occ.round}</span>
  434. {' · '}
  435. <span>{occ.strategy}</span>
  436. {occ.score !== undefined && (
  437. <span style={{ color: '#16a34a', marginLeft: '4px' }}>
  438. ({typeof occ.score === 'number' ? occ.score.toFixed(2) : occ.score})
  439. </span>
  440. )}
  441. </div>
  442. ))}
  443. </div>
  444. </div>
  445. )}
  446. {data.hasSearchResults && (
  447. <div style={{
  448. marginTop: '6px',
  449. paddingTop: '6px',
  450. borderTop: '1px solid #f3f4f6',
  451. fontSize: '10px',
  452. background: '#fef3c7',
  453. padding: '4px 6px',
  454. borderRadius: '4px',
  455. color: '#92400e',
  456. fontWeight: '500',
  457. }}>
  458. 🔍 找到 {data.postCount} 个帖子
  459. </div>
  460. )}
  461. </div>
  462. </div>
  463. </div>
  464. <Handle
  465. type="source"
  466. position={sourcePosition || Position.Right}
  467. style={{ background: '#667eea', width: 8, height: 8 }}
  468. />
  469. </div>
  470. );
  471. }
  472. // 笔记节点组件 - 卡片样式,带轮播图
  473. function NoteNode({ id, data, sourcePosition, targetPosition }) {
  474. const [currentImageIndex, setCurrentImageIndex] = useState(0);
  475. const expanded = true;
  476. const hasImages = data.imageList && data.imageList.length > 0;
  477. const nextImage = (e) => {
  478. e.stopPropagation();
  479. if (hasImages) {
  480. setCurrentImageIndex((prev) => (prev + 1) % data.imageList.length);
  481. }
  482. };
  483. const prevImage = (e) => {
  484. e.stopPropagation();
  485. if (hasImages) {
  486. setCurrentImageIndex((prev) => (prev - 1 + data.imageList.length) % data.imageList.length);
  487. }
  488. };
  489. const handleCardClick = (e) => {
  490. // 如果点击的是链接或按钮(或其子元素),不处理(避免双重触发)
  491. if (e.target.closest('a') || e.target.closest('button')) {
  492. return;
  493. }
  494. // 打开原帖链接
  495. if (data.note_url) {
  496. window.open(data.note_url, '_blank', 'noopener,noreferrer');
  497. }
  498. };
  499. return (
  500. <div>
  501. <Handle
  502. type="target"
  503. position={targetPosition || Position.Left}
  504. style={{ background: '#ec4899', width: 8, height: 8 }}
  505. />
  506. <div
  507. onClick={handleCardClick}
  508. style={{
  509. padding: '14px',
  510. borderRadius: '20px',
  511. border: data.isHighlighted ? '3px solid #ec4899' : '2px solid #fce7f3',
  512. background: data.isHighlighted ? '#eef2ff' : 'white',
  513. minWidth: '220px',
  514. maxWidth: '300px',
  515. 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)',
  516. transition: 'all 0.3s ease',
  517. cursor: 'pointer',
  518. }}
  519. >
  520. {/* 笔记标题 */}
  521. <div style={{ display: 'flex', alignItems: 'flex-start', marginBottom: '8px' }}>
  522. <div style={{ flex: 1 }}>
  523. <div style={{
  524. fontSize: '13px',
  525. fontWeight: '600',
  526. color: '#831843',
  527. lineHeight: '1.4',
  528. marginBottom: '4px',
  529. }}>
  530. {data.title}
  531. </div>
  532. </div>
  533. </div>
  534. {/* 评估信息区域 (V2) */}
  535. {(data.knowledge_score !== undefined || data.post_relevance_score !== undefined || data.is_knowledge !== undefined) && (
  536. <div style={{
  537. marginBottom: '10px',
  538. paddingBottom: '8px',
  539. borderBottom: '1px solid #fce7f3',
  540. }}>
  541. {/* 知识评估 (V2) */}
  542. {(data.knowledge_score !== undefined || data.is_knowledge !== undefined) && (
  543. <div style={{ marginBottom: '8px' }}>
  544. <div style={{ display: 'flex', alignItems: 'center', gap: '8px', marginBottom: '4px' }}>
  545. {/* 星级评分 */}
  546. {data.knowledge_level && (
  547. <span style={{ fontSize: '12px', lineHeight: '1' }}>
  548. {'⭐'.repeat(data.knowledge_level)}
  549. </span>
  550. )}
  551. {/* 综合得分 */}
  552. {data.knowledge_score != null && (
  553. <span style={{
  554. fontSize: '11px',
  555. fontWeight: '700',
  556. color: data.knowledge_score >= 70 ? '#166534' : data.knowledge_score >= 40 ? '#854d0e' : '#991b1b',
  557. }}>
  558. 知识: {data.knowledge_score.toFixed(0)}分
  559. </span>
  560. )}
  561. {/* 兼容旧版: 知识判定标签 */}
  562. {!data.knowledge_score && data.is_knowledge !== undefined && (
  563. <span style={{
  564. display: 'inline-block',
  565. padding: '3px 10px',
  566. borderRadius: '12px',
  567. fontSize: '11px',
  568. fontWeight: '600',
  569. background: data.is_knowledge ? '#dcfce7' : '#fee2e2',
  570. color: data.is_knowledge ? '#166534' : '#991b1b',
  571. }}>
  572. {data.is_knowledge ? '✓ 知识' : '✗ 非知识'}
  573. </span>
  574. )}
  575. </div>
  576. {/* 知识评估总结 */}
  577. {data.knowledge_evaluation?.summary && (
  578. <div style={{
  579. fontSize: '10px',
  580. color: '#9f1239',
  581. lineHeight: '1.4',
  582. marginTop: '4px',
  583. }}>
  584. {data.knowledge_evaluation.summary}
  585. </div>
  586. )}
  587. {/* 兼容旧版: 知识理由 */}
  588. {!data.knowledge_evaluation?.summary && data.knowledge_reason && (
  589. <div style={{
  590. fontSize: '10px',
  591. color: '#9f1239',
  592. lineHeight: '1.4',
  593. marginTop: '4px',
  594. }}>
  595. {data.knowledge_reason}
  596. </div>
  597. )}
  598. </div>
  599. )}
  600. {/* 相关性评估 (V2) */}
  601. {data.post_relevance_score != null && (
  602. <div>
  603. <div style={{
  604. display: 'flex',
  605. alignItems: 'center',
  606. gap: '6px',
  607. marginBottom: '4px',
  608. }}>
  609. {/* V2: 0-100分制 (统一显示分数) */}
  610. <span style={{
  611. fontSize: '11px',
  612. fontWeight: '600',
  613. color: '#9f1239',
  614. }}>
  615. 相关性: {data.post_relevance_score.toFixed(0)}分
  616. </span>
  617. {/* V2结论标签 */}
  618. {data.relevance_conclusion && (
  619. <span style={{
  620. padding: '2px 8px',
  621. borderRadius: '10px',
  622. fontSize: '10px',
  623. fontWeight: '600',
  624. background:
  625. data.relevance_conclusion.includes('高度') ? '#dcfce7' :
  626. data.relevance_conclusion.includes('中度') ? '#fef3c7' : '#fee2e2',
  627. color:
  628. data.relevance_conclusion.includes('高度') ? '#166534' :
  629. data.relevance_conclusion.includes('中度') ? '#854d0e' : '#991b1b',
  630. }}>
  631. {data.relevance_conclusion}
  632. </span>
  633. )}
  634. {/* V1兼容: 显示旧的相关性等级 */}
  635. {!data.relevance_conclusion && data.relevance_level && (
  636. <span style={{
  637. padding: '2px 8px',
  638. borderRadius: '10px',
  639. fontSize: '10px',
  640. fontWeight: '600',
  641. background:
  642. data.relevance_level === '高度相关' ? '#dcfce7' :
  643. data.relevance_level === '中度相关' ? '#fef3c7' : '#fee2e2',
  644. color:
  645. data.relevance_level === '高度相关' ? '#166534' :
  646. data.relevance_level === '中度相关' ? '#854d0e' : '#991b1b',
  647. }}>
  648. {data.relevance_level}
  649. </span>
  650. )}
  651. </div>
  652. {/* 相关性评估总结 (V2) */}
  653. {data.relevance_evaluation?.summary && (
  654. <div style={{
  655. fontSize: '10px',
  656. color: '#9f1239',
  657. lineHeight: '1.4',
  658. }}>
  659. {data.relevance_evaluation.summary}
  660. </div>
  661. )}
  662. {/* 目的性和品类得分 (V2) */}
  663. {data.relevance_evaluation?.purpose_score != null && data.relevance_evaluation?.category_score != null && (
  664. <div style={{
  665. fontSize: '9px',
  666. color: '#9f1239',
  667. marginTop: '3px',
  668. opacity: 0.8,
  669. }}>
  670. 目的性:{data.relevance_evaluation.purpose_score.toFixed(0)}分(70%) |
  671. 品类:{data.relevance_evaluation.category_score.toFixed(0)}分(30%)
  672. </div>
  673. )}
  674. {/* 兼容旧版: 相关性理由 */}
  675. {!data.relevance_evaluation?.summary && data.relevance_reason && (
  676. <div style={{
  677. fontSize: '10px',
  678. color: '#9f1239',
  679. lineHeight: '1.4',
  680. }}>
  681. {data.relevance_reason}
  682. </div>
  683. )}
  684. </div>
  685. )}
  686. </div>
  687. )}
  688. {/* 轮播图 */}
  689. {hasImages && (
  690. <div style={{
  691. position: 'relative',
  692. marginBottom: '8px',
  693. borderRadius: '12px',
  694. overflow: 'hidden',
  695. }}>
  696. <img
  697. src={data.imageList[currentImageIndex].image_url}
  698. alt={\`Image \${currentImageIndex + 1}\`}
  699. style={{
  700. width: '100%',
  701. aspectRatio: '2/3',
  702. height: 'auto',
  703. objectFit: 'cover',
  704. display: 'block',
  705. }}
  706. onError={(e) => {
  707. e.target.style.display = 'none';
  708. }}
  709. />
  710. {data.imageList.length > 1 && (
  711. <>
  712. {/* 左右切换按钮 */}
  713. <button
  714. onClick={prevImage}
  715. style={{
  716. position: 'absolute',
  717. left: '4px',
  718. top: '50%',
  719. transform: 'translateY(-50%)',
  720. background: 'rgba(0, 0, 0, 0.5)',
  721. color: 'white',
  722. border: 'none',
  723. borderRadius: '50%',
  724. width: '24px',
  725. height: '24px',
  726. cursor: 'pointer',
  727. display: 'flex',
  728. alignItems: 'center',
  729. justifyContent: 'center',
  730. fontSize: '14px',
  731. }}
  732. >
  733. </button>
  734. <button
  735. onClick={nextImage}
  736. style={{
  737. position: 'absolute',
  738. right: '4px',
  739. top: '50%',
  740. transform: 'translateY(-50%)',
  741. background: 'rgba(0, 0, 0, 0.5)',
  742. color: 'white',
  743. border: 'none',
  744. borderRadius: '50%',
  745. width: '24px',
  746. height: '24px',
  747. cursor: 'pointer',
  748. display: 'flex',
  749. alignItems: 'center',
  750. justifyContent: 'center',
  751. fontSize: '14px',
  752. }}
  753. >
  754. </button>
  755. {/* 图片计数 */}
  756. <div style={{
  757. position: 'absolute',
  758. bottom: '4px',
  759. right: '4px',
  760. background: 'rgba(0, 0, 0, 0.6)',
  761. color: 'white',
  762. padding: '2px 6px',
  763. borderRadius: '10px',
  764. fontSize: '10px',
  765. }}>
  766. {currentImageIndex + 1}/{data.imageList.length}
  767. </div>
  768. </>
  769. )}
  770. </div>
  771. )}
  772. {/* 互动数据 */}
  773. {data.interact_info && (
  774. <div style={{
  775. display: 'flex',
  776. gap: '8px',
  777. marginBottom: '8px',
  778. flexWrap: 'wrap',
  779. fontSize: '11px',
  780. color: '#9f1239',
  781. }}>
  782. {data.interact_info.liked_count > 0 && (
  783. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  784. ❤️ {data.interact_info.liked_count}
  785. </span>
  786. )}
  787. {data.interact_info.collected_count > 0 && (
  788. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  789. ⭐ {data.interact_info.collected_count}
  790. </span>
  791. )}
  792. {data.interact_info.comment_count > 0 && (
  793. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  794. 💬 {data.interact_info.comment_count}
  795. </span>
  796. )}
  797. {data.interact_info.shared_count > 0 && (
  798. <span style={{ display: 'flex', alignItems: 'center', gap: '2px' }}>
  799. 🔗 {data.interact_info.shared_count}
  800. </span>
  801. )}
  802. </div>
  803. )}
  804. {/* 被哪些query找到 */}
  805. {data.foundByQueries && data.foundByQueries.length > 0 && (
  806. <div style={{
  807. marginBottom: '8px',
  808. padding: '6px 8px',
  809. background: '#f0fdf4',
  810. borderRadius: '6px',
  811. fontSize: '10px',
  812. }}>
  813. <strong style={{ color: '#16a34a' }}>🔍 被找到:</strong>
  814. <div style={{ marginTop: '4px', display: 'flex', flexWrap: 'wrap', gap: '4px' }}>
  815. {data.foundByQueries.map((query, idx) => (
  816. <span key={idx} style={{
  817. display: 'inline-block',
  818. padding: '2px 6px',
  819. background: '#dcfce7',
  820. color: '#166534',
  821. borderRadius: '4px',
  822. fontSize: '9px',
  823. }}>
  824. {query}
  825. </span>
  826. ))}
  827. </div>
  828. {data.foundInRounds && data.foundInRounds.length > 0 && (
  829. <div style={{ marginTop: '4px', color: '#6b7280' }}>
  830. 出现在: Round {data.foundInRounds.join(', ')}
  831. </div>
  832. )}
  833. </div>
  834. )}
  835. {/* 标签 */}
  836. {(data.matchLevel || data.score) && (
  837. <div style={{ display: 'flex', gap: '6px', marginBottom: '8px', flexWrap: 'wrap' }}>
  838. {data.matchLevel && (
  839. <span style={{
  840. display: 'inline-block',
  841. padding: '2px 8px',
  842. borderRadius: '12px',
  843. background: '#fff1f2',
  844. color: '#be123c',
  845. fontSize: '10px',
  846. fontWeight: '500',
  847. }}>
  848. {data.matchLevel}
  849. </span>
  850. )}
  851. {/* Score标签已隐藏 - V2不再需要 */}
  852. </div>
  853. )}
  854. {/* 描述 */}
  855. {expanded && data.description && (
  856. <div style={{
  857. fontSize: '11px',
  858. color: '#9f1239',
  859. lineHeight: '1.5',
  860. paddingTop: '8px',
  861. borderTop: '1px solid #fbcfe8',
  862. }}>
  863. {data.description}
  864. </div>
  865. )}
  866. {/* 评估理由 */}
  867. {expanded && data.evaluationReason && (
  868. <div style={{
  869. fontSize: '10px',
  870. color: '#831843',
  871. lineHeight: '1.5',
  872. paddingTop: '8px',
  873. marginTop: '8px',
  874. borderTop: '1px solid #fbcfe8',
  875. }}>
  876. <strong style={{ color: '#9f1239' }}>评估:</strong>
  877. <div style={{ marginTop: '2px' }}>{data.evaluationReason}</div>
  878. </div>
  879. )}
  880. </div>
  881. <Handle
  882. type="source"
  883. position={sourcePosition || Position.Right}
  884. style={{ background: '#ec4899', width: 8, height: 8 }}
  885. />
  886. </div>
  887. );
  888. }
  889. // AnalysisNode 组件:展示AI分析(左侧OCR文字,右侧缩略图+描述)
  890. function AnalysisNode({ data }) {
  891. const nodeStyle = {
  892. background: '#fffbeb',
  893. border: '2px solid #fbbf24',
  894. borderRadius: '8px',
  895. padding: '12px',
  896. minWidth: '700px',
  897. maxWidth: '900px',
  898. fontSize: '12px',
  899. boxShadow: '0 4px 6px rgba(0,0,0,0.1)',
  900. cursor: 'pointer',
  901. };
  902. const handleCardClick = (e) => {
  903. // 如果点击的是链接或按钮(或其子元素),不处理(避免双重触发)
  904. if (e.target.closest('a') || e.target.closest('button')) {
  905. return;
  906. }
  907. // 打开原帖链接
  908. if (data.note_url) {
  909. window.open(data.note_url, '_blank', 'noopener,noreferrer');
  910. }
  911. };
  912. return (
  913. <div style={nodeStyle} onClick={handleCardClick}>
  914. <Handle
  915. type="target"
  916. position={Position.Left}
  917. style={{ background: '#fbbf24', width: 8, height: 8 }}
  918. />
  919. {/* 标题 */}
  920. <div style={{
  921. fontSize: '14px',
  922. fontWeight: 'bold',
  923. marginBottom: '8px',
  924. color: '#92400e',
  925. }}>
  926. 🖼️ {data.query}
  927. </div>
  928. {/* 评分和互动数据 */}
  929. <div style={{
  930. display: 'flex',
  931. justifyContent: 'space-between',
  932. marginBottom: '8px',
  933. padding: '6px',
  934. background: '#fef3c7',
  935. borderRadius: '4px',
  936. }}>
  937. <div style={{ fontSize: '11px', fontWeight: 'bold' }}>
  938. Score: {data.interact_info?.relevance_score || 0}
  939. </div>
  940. <div style={{ display: 'flex', gap: '12px', fontSize: '11px' }}>
  941. {data.interact_info?.liked_count > 0 && (
  942. <span>❤️ {data.interact_info.liked_count}</span>
  943. )}
  944. {data.interact_info?.collected_count > 0 && (
  945. <span>⭐ {data.interact_info.collected_count}</span>
  946. )}
  947. {data.interact_info?.comment_count > 0 && (
  948. <span>💬 {data.interact_info.comment_count}</span>
  949. )}
  950. </div>
  951. </div>
  952. {/* 完整正文内容 */}
  953. {data.body_text && (
  954. <div style={{
  955. padding: '8px',
  956. background: 'white',
  957. borderRadius: '4px',
  958. marginBottom: '12px',
  959. fontSize: '11px',
  960. lineHeight: '1.5',
  961. border: '1px solid #fbbf24',
  962. whiteSpace: 'pre-wrap',
  963. wordBreak: 'break-word',
  964. }}>
  965. {data.body_text}
  966. </div>
  967. )}
  968. {/* AI分析 - 左右分栏 */}
  969. {data.extraction && data.extraction.images && (
  970. <div style={{
  971. display: 'flex',
  972. flexDirection: 'column',
  973. gap: '12px',
  974. }}>
  975. {data.extraction.images.map((img, idx) => (
  976. <div
  977. key={idx}
  978. style={{
  979. display: 'flex',
  980. flexDirection: 'row',
  981. gap: '16px',
  982. padding: '10px',
  983. background: 'white',
  984. borderRadius: '4px',
  985. border: '1px solid #d97706',
  986. alignItems: 'flex-start',
  987. }}
  988. >
  989. {/* 左侧:OCR提取文字 */}
  990. <div style={{
  991. flex: '1', // 1/3宽度
  992. minWidth: '0',
  993. }}>
  994. <div style={{
  995. fontSize: '11px',
  996. fontWeight: 'bold',
  997. color: '#92400e',
  998. marginBottom: '6px',
  999. }}>
  1000. 📝 图片 {idx + 1}/{data.extraction.images.length}
  1001. </div>
  1002. {img.extract_text && (
  1003. <div style={{
  1004. fontSize: '11px',
  1005. color: '#1f2937',
  1006. lineHeight: '1.6',
  1007. padding: '8px',
  1008. background: '#fef9e7',
  1009. borderRadius: '3px',
  1010. borderLeft: '3px solid #f39c12',
  1011. wordBreak: 'break-word',
  1012. }}>
  1013. <div style={{
  1014. fontSize: '10px',
  1015. fontWeight: 'bold',
  1016. color: '#d97706',
  1017. marginBottom: '4px',
  1018. }}>
  1019. 【提取文字】
  1020. </div>
  1021. {img.extract_text}
  1022. </div>
  1023. )}
  1024. </div>
  1025. {/* 右侧:缩略图 + 描述 */}
  1026. <div style={{
  1027. flex: '2', // 2/3宽度
  1028. display: 'flex',
  1029. flexDirection: 'column',
  1030. gap: '8px',
  1031. minWidth: '200px',
  1032. }}>
  1033. {/* 缩略图 */}
  1034. {data.image_list && data.image_list[idx] && (
  1035. <img
  1036. src={(data.image_list[idx].image_url || data.image_list[idx])}
  1037. alt={'图片' + (idx + 1)}
  1038. style={{
  1039. width: '100%',
  1040. height: 'auto',
  1041. maxHeight: '180px',
  1042. objectFit: 'contain',
  1043. borderRadius: '4px',
  1044. border: '1px solid #d97706',
  1045. cursor: 'pointer',
  1046. }}
  1047. onError={(e) => {
  1048. e.target.style.display = 'none';
  1049. }}
  1050. />
  1051. )}
  1052. {/* 描述文字(完整展示) */}
  1053. {img.description && (
  1054. <div
  1055. style={{
  1056. fontSize: '10px',
  1057. color: '#78350f',
  1058. lineHeight: '1.5',
  1059. wordBreak: 'break-word',
  1060. padding: '8px',
  1061. background: '#fef9e7',
  1062. borderRadius: '3px',
  1063. border: '1px solid #f39c12',
  1064. }}
  1065. >
  1066. <div style={{
  1067. fontSize: '9px',
  1068. fontWeight: 'bold',
  1069. color: '#d97706',
  1070. marginBottom: '4px',
  1071. }}>
  1072. 【图片描述】
  1073. </div>
  1074. {img.description}
  1075. </div>
  1076. )}
  1077. </div>
  1078. </div>
  1079. ))}
  1080. </div>
  1081. )}
  1082. {/* 查看原帖链接 */}
  1083. {data.note_url && (
  1084. <div style={{ marginTop: '8px', fontSize: '10px' }}>
  1085. <a
  1086. href={data.note_url}
  1087. target="_blank"
  1088. rel="noopener noreferrer"
  1089. style={{ color: '#92400e', textDecoration: 'underline' }}
  1090. >
  1091. 🔗 查看原帖
  1092. </a>
  1093. </div>
  1094. )}
  1095. <Handle
  1096. type="source"
  1097. position={Position.Right}
  1098. style={{ background: '#fbbf24', width: 8, height: 8 }}
  1099. />
  1100. </div>
  1101. );
  1102. }
  1103. const nodeTypes = {
  1104. query: QueryNode,
  1105. note: NoteNode,
  1106. post: NoteNode, // 帖子节点使用 NoteNode 组件渲染
  1107. analysis: AnalysisNode,
  1108. };
  1109. // 根据 score 获取颜色
  1110. function getScoreColor(score) {
  1111. if (score >= 0.7) return '#10b981'; // 绿色 - 高分
  1112. if (score >= 0.4) return '#f59e0b'; // 橙色 - 中分
  1113. return '#ef4444'; // 红色 - 低分
  1114. }
  1115. // 截断文本,保留头尾,中间显示省略号
  1116. function truncateMiddle(text, maxLength = 20) {
  1117. if (!text || text.length <= maxLength) return text;
  1118. const headLength = Math.ceil(maxLength * 0.4);
  1119. const tailLength = Math.floor(maxLength * 0.4);
  1120. const head = text.substring(0, headLength);
  1121. const tail = text.substring(text.length - tailLength);
  1122. return \`\${head}...\${tail}\`;
  1123. }
  1124. // 根据策略获取颜色
  1125. // 智能提取主要策略的辅助函数
  1126. function getPrimaryStrategy(nodeData) {
  1127. // 优先级1: 使用 primaryStrategy 字段
  1128. if (nodeData.primaryStrategy) {
  1129. return nodeData.primaryStrategy;
  1130. }
  1131. // 优先级2: 从 occurrences 数组中获取最新的策略
  1132. if (nodeData.occurrences && Array.isArray(nodeData.occurrences) && nodeData.occurrences.length > 0) {
  1133. const latestOccurrence = nodeData.occurrences[nodeData.occurrences.length - 1];
  1134. if (latestOccurrence && latestOccurrence.strategy) {
  1135. return latestOccurrence.strategy;
  1136. }
  1137. }
  1138. // 优先级3: 拆分组合策略字符串,取第一个
  1139. if (nodeData.strategy && typeof nodeData.strategy === 'string') {
  1140. const strategies = nodeData.strategy.split(' + ');
  1141. if (strategies.length > 0 && strategies[0]) {
  1142. return strategies[0].trim();
  1143. }
  1144. }
  1145. // 默认返回原始strategy或未知
  1146. return nodeData.strategy || '未知';
  1147. }
  1148. function getStrategyColor(strategy) {
  1149. const strategyColors = {
  1150. '初始分词': '#10b981',
  1151. '调用sug': '#06b6d4',
  1152. '同义改写': '#f59e0b',
  1153. '加词': '#3b82f6',
  1154. '抽象改写': '#8b5cf6',
  1155. '基于部分匹配改进': '#ec4899',
  1156. '结果分支-抽象改写': '#a855f7',
  1157. '结果分支-同义改写': '#fb923c',
  1158. // v6.1.2.8 新增策略
  1159. '原始问题': '#6b21a8',
  1160. '来自分词': '#10b981',
  1161. '加词生成': '#ef4444',
  1162. '建议词': '#06b6d4',
  1163. '执行搜索': '#8b5cf6',
  1164. // 添加简化版本的策略映射
  1165. '分词': '#10b981',
  1166. '推荐词': '#06b6d4',
  1167. };
  1168. return strategyColors[strategy] || '#9ca3af';
  1169. }
  1170. // 树节点组件
  1171. function TreeNode({ node, level, children, isCollapsed, onToggle, isSelected, onSelect }) {
  1172. const hasChildren = children && children.length > 0;
  1173. const score = node.data.score ? parseFloat(node.data.score) : 0;
  1174. const strategy = getPrimaryStrategy(node.data); // 使用智能提取函数
  1175. const strategyColor = getStrategyColor(strategy);
  1176. const nodeActualType = node.data.nodeType || node.type; // 获取实际节点类型
  1177. const isDomainCombination = nodeActualType === 'domain_combination';
  1178. let sourceSummary = '';
  1179. if (isDomainCombination && Array.isArray(node.data.source_word_details) && node.data.source_word_details.length > 0) {
  1180. const summaryParts = [];
  1181. node.data.source_word_details.forEach((detail) => {
  1182. const words = Array.isArray(detail.words) ? detail.words : [];
  1183. const wordTexts = [];
  1184. words.forEach((w) => {
  1185. const numericScore = typeof w.score === 'number' ? w.score : parseFloat(w.score || '0');
  1186. const formattedScore = Number.isFinite(numericScore) ? numericScore.toFixed(2) : '0.00';
  1187. wordTexts.push(w.text + ' (' + formattedScore + ')');
  1188. });
  1189. if (wordTexts.length > 0) {
  1190. const segmentLabel = detail.segment_type ? '[' + detail.segment_type + '] ' : '';
  1191. summaryParts.push(segmentLabel + wordTexts.join(' + '));
  1192. }
  1193. });
  1194. sourceSummary = summaryParts.join(' | ');
  1195. }
  1196. // 计算字体颜色:根据分数提升幅度判断
  1197. let fontColor = '#374151'; // 默认颜色
  1198. if (node.type === 'note') {
  1199. // V2评估:基于知识得分和相关性得分判断颜色
  1200. const knowledgeScore = node.data.knowledge_score;
  1201. const relevanceScore = node.data.post_relevance_score;
  1202. if (knowledgeScore != null && relevanceScore != null) {
  1203. if (knowledgeScore <= 40) {
  1204. fontColor = '#ef4444'; // 红色 - 知识得分低
  1205. } else if (knowledgeScore > 40 && relevanceScore > 40) {
  1206. fontColor = '#22c55e'; // 绿色 - 知识和相关性都高
  1207. } else {
  1208. fontColor = '#eab308'; // 黄色 - 知识得分高但相关性低
  1209. }
  1210. } else {
  1211. // V1兼容:如果没有V2评估数据,使用matchLevel判断
  1212. fontColor = node.data.matchLevel === 'unsatisfied' ? '#ef4444' : '#374151';
  1213. }
  1214. } else if (node.data.seed_score !== undefined) {
  1215. const parentScore = parseFloat(node.data.seed_score);
  1216. const gain = score - parentScore;
  1217. fontColor = gain >= 0.05 ? '#16a34a' : '#ef4444';
  1218. } else if (node.data.isSelected === false) {
  1219. fontColor = '#ef4444';
  1220. }
  1221. return (
  1222. <div style={{ marginLeft: level * 12 + 'px', marginBottom: '8px' }}>
  1223. <div
  1224. style={{
  1225. padding: '6px 8px',
  1226. borderRadius: '4px',
  1227. cursor: 'pointer',
  1228. background: 'transparent',
  1229. border: isSelected ? '1px solid #3b82f6' : '1px solid transparent',
  1230. display: 'flex',
  1231. alignItems: 'center',
  1232. gap: '6px',
  1233. transition: 'all 0.2s ease',
  1234. position: 'relative',
  1235. overflow: 'visible',
  1236. }}
  1237. onMouseEnter={(e) => {
  1238. if (!isSelected) e.currentTarget.style.background = '#f9fafb';
  1239. }}
  1240. onMouseLeave={(e) => {
  1241. if (!isSelected) e.currentTarget.style.background = 'transparent';
  1242. }}
  1243. >
  1244. {/* 策略类型竖线 */}
  1245. <div style={{
  1246. width: '3px',
  1247. height: '20px',
  1248. background: strategyColor,
  1249. borderRadius: '2px',
  1250. flexShrink: 0,
  1251. position: 'relative',
  1252. zIndex: 1,
  1253. }} />
  1254. {hasChildren && (
  1255. <span
  1256. style={{
  1257. fontSize: '10px',
  1258. color: '#6b7280',
  1259. cursor: 'pointer',
  1260. width: '16px',
  1261. textAlign: 'center',
  1262. position: 'relative',
  1263. zIndex: 1,
  1264. }}
  1265. onClick={(e) => {
  1266. e.stopPropagation();
  1267. onToggle();
  1268. }}
  1269. >
  1270. {isCollapsed ? '▶' : '▼'}
  1271. </span>
  1272. )}
  1273. {!hasChildren && <span style={{ width: '16px', position: 'relative', zIndex: 1 }}></span>}
  1274. <div
  1275. style={{
  1276. flex: 1,
  1277. fontSize: '12px',
  1278. color: '#374151',
  1279. position: 'relative',
  1280. zIndex: 1,
  1281. minWidth: 0,
  1282. display: 'flex',
  1283. flexDirection: 'column',
  1284. gap: '4px',
  1285. }}
  1286. onClick={onSelect}
  1287. >
  1288. <div style={{
  1289. display: 'flex',
  1290. alignItems: 'center',
  1291. gap: '8px',
  1292. }}>
  1293. {/* 文本标题 - 左侧 */}
  1294. <div style={{
  1295. fontWeight: level === 0 ? '600' : '400',
  1296. flex: 1,
  1297. minWidth: 0,
  1298. color: node.data.scoreColor || fontColor,
  1299. overflow: 'hidden',
  1300. textOverflow: 'ellipsis',
  1301. whiteSpace: 'nowrap',
  1302. }}
  1303. title={node.data.title || node.id}
  1304. >
  1305. {node.data.title || node.id}
  1306. </div>
  1307. {/* 域标识 - 右侧,挨着分数,优先显示域类型,否则显示域索引或域字符串,但domain_combination节点不显示 */}
  1308. {(node.data.domain_type || node.data.domains_str || (node.data.domain_index !== null && node.data.domain_index !== undefined)) && nodeActualType !== 'domain_combination' && (
  1309. <span style={{
  1310. fontSize: '12px',
  1311. color: '#fff',
  1312. background: '#6366f1',
  1313. padding: '2px 5px',
  1314. borderRadius: '3px',
  1315. flexShrink: 0,
  1316. fontWeight: '600',
  1317. marginLeft: '4px',
  1318. }}
  1319. title={
  1320. node.data.domain_type ? '域: ' + node.data.domain_type + ' (D' + node.data.domain_index + ')' :
  1321. node.data.domains_str ? '域: ' + node.data.domains_str :
  1322. '域 D' + node.data.domain_index
  1323. }
  1324. >
  1325. {node.data.domain_type || node.data.domains_str || ('D' + node.data.domain_index)}
  1326. </span>
  1327. )}
  1328. {node.data.is_suggestion && node.data.suggestion_label && (
  1329. <span style={{
  1330. fontSize: '12px',
  1331. color: '#fff',
  1332. background: '#8b5cf6',
  1333. padding: '2px 5px',
  1334. borderRadius: '3px',
  1335. flexShrink: 0,
  1336. fontWeight: '600',
  1337. }}
  1338. >
  1339. {node.data.suggestion_label}
  1340. </span>
  1341. )}
  1342. {/* 类型标签 - 显示在右侧靠近分数,蓝色背景 */}
  1343. {node.data.type_label && (
  1344. <span style={{
  1345. fontSize: '12px',
  1346. color: '#fff',
  1347. background: '#6366f1',
  1348. padding: '2px 5px',
  1349. borderRadius: '3px',
  1350. flexShrink: 0,
  1351. fontWeight: '600',
  1352. }}
  1353. title={'类型: ' + node.data.type_label}
  1354. >
  1355. {node.data.type_label}
  1356. </span>
  1357. )}
  1358. {/* 分数显示 - 步骤和轮次节点不显示分数 */}
  1359. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  1360. <span style={{
  1361. fontSize: '11px',
  1362. color: '#6b7280',
  1363. fontWeight: '500',
  1364. flexShrink: 0,
  1365. minWidth: '35px',
  1366. textAlign: 'right',
  1367. }}>
  1368. {score.toFixed(2)}
  1369. </span>
  1370. )}
  1371. </div>
  1372. {/* 域组合的来源词得分(树状视图,右对齐) */}
  1373. {isDomainCombination && sourceSummary && (
  1374. <div style={{
  1375. fontSize: '10px',
  1376. color: '#2563eb',
  1377. lineHeight: '1.4',
  1378. display: 'flex',
  1379. flexDirection: 'column',
  1380. alignItems: 'flex-end',
  1381. gap: '2px',
  1382. textAlign: 'right',
  1383. }}>
  1384. {node.data.source_word_details.map((detail, idx) => {
  1385. const words = Array.isArray(detail.words) ? detail.words : [];
  1386. const summary = words.map((w) => {
  1387. const numericScore = typeof w.score === 'number' ? w.score : parseFloat(w.score || '0');
  1388. const formattedScore = Number.isFinite(numericScore) ? numericScore.toFixed(2) : '0.00';
  1389. return w.text + ' (' + formattedScore + ')';
  1390. }).join(' + ');
  1391. return (
  1392. <span key={idx} title={summary}>
  1393. {summary}
  1394. </span>
  1395. );
  1396. })}
  1397. </div>
  1398. )}
  1399. {/* 分数下划线 - 步骤和轮次节点不显示 */}
  1400. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  1401. <div style={{
  1402. width: (score * 100) + '%',
  1403. height: '2px',
  1404. background: getScoreColor(score),
  1405. borderRadius: '1px',
  1406. }} />
  1407. )}
  1408. </div>
  1409. </div>
  1410. {hasChildren && !isCollapsed && (
  1411. <div>
  1412. {children}
  1413. </div>
  1414. )}
  1415. </div>
  1416. );
  1417. }
  1418. // 使用 dagre 自动布局
  1419. function getLayoutedElements(nodes, edges, direction = 'LR') {
  1420. console.log('🎯 Starting layout with dagre...');
  1421. console.log('Input:', nodes.length, 'nodes,', edges.length, 'edges');
  1422. // 检查 dagre 是否加载
  1423. if (typeof window === 'undefined' || typeof window.dagre === 'undefined') {
  1424. console.warn('⚠️ Dagre not loaded, using fallback layout');
  1425. // 降级到简单布局
  1426. const levelGroups = {};
  1427. nodes.forEach(node => {
  1428. const level = node.data.level || 0;
  1429. if (!levelGroups[level]) levelGroups[level] = [];
  1430. levelGroups[level].push(node);
  1431. });
  1432. Object.entries(levelGroups).forEach(([level, nodeList]) => {
  1433. const x = parseInt(level) * 480;
  1434. nodeList.forEach((node, index) => {
  1435. node.position = { x, y: index * 260 };
  1436. node.targetPosition = 'left';
  1437. node.sourcePosition = 'right';
  1438. });
  1439. });
  1440. return { nodes, edges };
  1441. }
  1442. try {
  1443. const dagreGraph = new window.dagre.graphlib.Graph();
  1444. dagreGraph.setDefaultEdgeLabel(() => ({}));
  1445. const isHorizontal = direction === 'LR';
  1446. dagreGraph.setGraph({
  1447. rankdir: direction,
  1448. nodesep: 250, // 垂直间距 - 增加以适应更高的note节点
  1449. ranksep: 400, // 水平间距 - 增加以容纳更宽的节点
  1450. });
  1451. // 添加节点 - 根据节点类型设置不同的尺寸
  1452. nodes.forEach((node) => {
  1453. let nodeWidth = 320;
  1454. let nodeHeight = 220;
  1455. // note 节点有轮播图,需要更大的空间
  1456. if (node.type === 'note') {
  1457. nodeWidth = 360;
  1458. nodeHeight = 550; // 更新以适应 2:3 比例的轮播图(约450px)+ 其他内容
  1459. }
  1460. // analysis 节点内容很多,需要更大的空间
  1461. else if (node.type === 'analysis') {
  1462. nodeWidth = 900; // 宽度足够容纳左右分栏
  1463. nodeHeight = 600; // 高度足够容纳多张图片
  1464. }
  1465. dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight });
  1466. });
  1467. // 添加边
  1468. edges.forEach((edge) => {
  1469. dagreGraph.setEdge(edge.source, edge.target);
  1470. });
  1471. // 计算布局
  1472. window.dagre.layout(dagreGraph);
  1473. console.log('✅ Dagre layout completed');
  1474. // 更新节点位置和 handle 位置
  1475. nodes.forEach((node) => {
  1476. const nodeWithPosition = dagreGraph.node(node.id);
  1477. if (!nodeWithPosition) {
  1478. console.warn('Node position not found for:', node.id);
  1479. return;
  1480. }
  1481. node.targetPosition = isHorizontal ? 'left' : 'top';
  1482. node.sourcePosition = isHorizontal ? 'right' : 'bottom';
  1483. // 根据节点类型获取尺寸
  1484. let nodeWidth = 320;
  1485. let nodeHeight = 220;
  1486. if (node.type === 'note') {
  1487. nodeWidth = 360;
  1488. nodeHeight = 550;
  1489. }
  1490. // 将 dagre 的中心点位置转换为 React Flow 的左上角位置
  1491. node.position = {
  1492. x: nodeWithPosition.x - nodeWidth / 2,
  1493. y: nodeWithPosition.y - nodeHeight / 2,
  1494. };
  1495. });
  1496. // 为同层级的 note 节点添加交错偏移,避免视觉重叠
  1497. console.log('=== 开始交错偏移逻辑 ===');
  1498. console.log('总节点数:', nodes.length);
  1499. const noteNodes = nodes.filter(n => n.type === 'note');
  1500. console.log('过滤后的 note 节点数:', noteNodes.length);
  1501. if (noteNodes.length > 1) {
  1502. // 输出排序前的位置
  1503. console.log('排序前的 note 节点位置:');
  1504. noteNodes.forEach((n, i) => {
  1505. console.log(' [' + i + '] ' + n.id.substring(0, 40) + '... | type=' + n.type + ' | pos=(' + n.position.x.toFixed(0) + ', ' + n.position.y.toFixed(0) + ')');
  1506. });
  1507. // 按 Y 坐标排序
  1508. noteNodes.sort((a, b) => a.position.y - b.position.y);
  1509. console.log('排序后的 note 节点位置:');
  1510. noteNodes.forEach((n, i) => {
  1511. console.log(' [' + i + '] ' + n.id.substring(0, 40) + '... | pos=(' + n.position.x.toFixed(0) + ', ' + n.position.y.toFixed(0) + ')');
  1512. });
  1513. // 为相邻的 note 节点添加 X 方向的交错
  1514. let appliedCount = 0;
  1515. noteNodes.forEach((node, index) => {
  1516. if (index > 0) {
  1517. const prevNode = noteNodes[index - 1];
  1518. const yDistance = node.position.y - prevNode.position.y;
  1519. console.log('检查节点对 [' + (index-1) + '] vs [' + index + ']:');
  1520. console.log(' Y距离: ' + yDistance.toFixed(0) + 'px');
  1521. // 直接应用偏移,不再判断 Y 距离(因为实际内容高度可能超过预设)
  1522. // 奇数索引向右偏移,偶数索引向左偏移
  1523. const offset = (index % 2 === 0) ? -200 : 200;
  1524. const oldX = node.position.x;
  1525. node.position.x += offset;
  1526. appliedCount++;
  1527. console.log(' ✅ 应用偏移: X ' + oldX.toFixed(0) + ' → ' + node.position.x.toFixed(0) + ' (偏移' + offset + 'px)');
  1528. }
  1529. });
  1530. console.log('总共应用了 ' + appliedCount + ' 次偏移');
  1531. } else {
  1532. console.log('note 节点数量 <= 1,不需要交错');
  1533. }
  1534. console.log('=== 交错偏移逻辑结束 ===');
  1535. console.log('✅ Layout completed, sample node:', nodes[0]);
  1536. return { nodes, edges };
  1537. } catch (error) {
  1538. console.error('❌ Error in dagre layout:', error);
  1539. console.error('Error details:', error.message, error.stack);
  1540. // 降级处理
  1541. console.log('Using fallback layout...');
  1542. const levelGroups = {};
  1543. nodes.forEach(node => {
  1544. const level = node.data.level || 0;
  1545. if (!levelGroups[level]) levelGroups[level] = [];
  1546. levelGroups[level].push(node);
  1547. });
  1548. Object.entries(levelGroups).forEach(([level, nodeList]) => {
  1549. const x = parseInt(level) * 480;
  1550. nodeList.forEach((node, index) => {
  1551. node.position = { x, y: index * 260 };
  1552. node.targetPosition = 'left';
  1553. node.sourcePosition = 'right';
  1554. });
  1555. });
  1556. return { nodes, edges };
  1557. }
  1558. }
  1559. function transformData(data) {
  1560. const nodes = [];
  1561. const edges = [];
  1562. const originalIdToCanvasId = {}; // 原始ID -> 画布ID的映射
  1563. const canvasIdToNodeData = {}; // 避免重复创建相同的节点
  1564. let analysisNodeCount = 0; // 用于给analysis节点添加X偏移
  1565. // 创建节点
  1566. Object.entries(data.nodes).forEach(([originalId, node]) => {
  1567. // 统一处理所有类型的节点
  1568. const nodeType = node.type || 'query';
  1569. // 直接使用originalId作为canvasId,避免冲突
  1570. const canvasId = originalId;
  1571. originalIdToCanvasId[originalId] = canvasId;
  1572. // 如果这个 canvasId 还没有创建过节点,则创建
  1573. if (!canvasIdToNodeData[canvasId]) {
  1574. canvasIdToNodeData[canvasId] = true;
  1575. // 根据节点类型创建不同的数据结构
  1576. if (nodeType === 'note' || nodeType === 'post') {
  1577. nodes.push({
  1578. id: canvasId,
  1579. originalId: originalId,
  1580. type: 'note',
  1581. data: {
  1582. title: node.query || node.title || '帖子',
  1583. matchLevel: node.match_level,
  1584. score: node.relevance_score ? node.relevance_score.toFixed(2) : '0.00',
  1585. description: node.body_text || node.desc || '',
  1586. isSelected: node.is_selected !== undefined ? node.is_selected : true,
  1587. imageList: node.image_list || [],
  1588. note_url: node.note_url || '',
  1589. evaluationReason: node.evaluationReason || node.evaluation_reason || '',
  1590. interact_info: node.interact_info || {},
  1591. nodeType: nodeType,
  1592. // 🆕 评估字段 (V2)
  1593. // 知识评估
  1594. is_knowledge: node.is_knowledge !== undefined ? node.is_knowledge : null,
  1595. knowledge_reason: node.knowledge_reason || '',
  1596. knowledge_score: node.knowledge_score !== undefined ? node.knowledge_score : null,
  1597. knowledge_level: node.knowledge_level !== undefined ? node.knowledge_level : null,
  1598. knowledge_evaluation: node.knowledge_evaluation || null,
  1599. // 相关性评估
  1600. post_relevance_score: node.post_relevance_score !== undefined ? node.post_relevance_score : null,
  1601. relevance_level: node.relevance_level || '',
  1602. relevance_reason: node.relevance_reason || '',
  1603. relevance_conclusion: node.relevance_conclusion || '',
  1604. relevance_evaluation: node.relevance_evaluation || null
  1605. },
  1606. position: { x: 0, y: 0 },
  1607. });
  1608. } else if (nodeType === 'analysis') {
  1609. // AI分析节点 - 添加X偏移避免叠加
  1610. const xOffset = analysisNodeCount * 150; // 每个节点偏移150px
  1611. analysisNodeCount++;
  1612. nodes.push({
  1613. id: canvasId,
  1614. originalId: originalId,
  1615. type: 'analysis',
  1616. data: {
  1617. query: node.query || '[AI分析]',
  1618. note_id: node.note_id,
  1619. note_url: node.note_url,
  1620. title: node.title || '',
  1621. body_text: node.body_text || '',
  1622. interact_info: node.interact_info || {},
  1623. extraction: node.extraction || null,
  1624. image_list: node.image_list || [],
  1625. },
  1626. position: { x: xOffset, y: 0 },
  1627. });
  1628. } else {
  1629. // query, seg, q, search, root 等节点
  1630. let displayTitle = node.query || originalId;
  1631. nodes.push({
  1632. id: canvasId,
  1633. originalId: originalId,
  1634. type: 'query', // 使用 query 组件渲染所有非note节点
  1635. data: {
  1636. title: displayTitle,
  1637. level: node.level || 0,
  1638. score: node.relevance_score ? node.relevance_score.toFixed(2) : '0.00',
  1639. strategy: node.strategy || '',
  1640. parent: node.parent_query || '',
  1641. isSelected: node.is_selected !== undefined ? node.is_selected : true,
  1642. evaluationReason: node.evaluationReason || node.evaluation_reason || '',
  1643. nodeType: nodeType, // 传递实际节点类型用于样式
  1644. searchCount: node.search_count, // search 节点特有
  1645. totalPosts: node.total_posts, // search 节点特有
  1646. selectedWord: node.selected_word || '', // 加词节点特有 - 显示选择的词
  1647. scoreColor: node.scoreColor || null, // SUG节点的颜色标识
  1648. parentQScore: node.parentQScore || 0, // 父Q得分(用于调试)
  1649. domain_index: node.domain_index !== undefined ? node.domain_index : null, // 域索引
  1650. domain_type: node.domain_type || '', // 域类型(如"中心名词"、"核心动作"),只有Q节点有,segment节点不显示
  1651. segment_type: node.segment_type || '', // segment类型(只有segment节点才有)
  1652. type_label: node.type_label || '', // 类型标签
  1653. domains: node.domains || [], // 域索引数组(domain_combination节点特有)
  1654. domains_str: node.domains_str || '', // 域标识字符串(如"D0,D1")
  1655. from_segments: node.from_segments || [], // 来源segments(domain_combination节点特有)
  1656. source_word_details: node.source_word_details || [], // 组合来源词及其得分
  1657. source_scores: node.source_scores || [], // 扁平来源得分
  1658. is_above_sources: node.is_above_sources || false, // 组合是否高于来源得分
  1659. max_source_score: node.max_source_score !== undefined ? node.max_source_score : null, // 来源最高分
  1660. item_type: node.item_type || '', // 构建下一轮节点来源类型
  1661. is_suggestion: node.is_suggestion || false,
  1662. suggestion_label: node.suggestion_label || '',
  1663. },
  1664. position: { x: 0, y: 0 },
  1665. });
  1666. }
  1667. }
  1668. });
  1669. // 创建边 - 使用虚线样式,映射到画布ID
  1670. data.edges.forEach((edge, index) => {
  1671. const edgeColors = {
  1672. '初始分词': '#10b981',
  1673. '调用sug': '#06b6d4',
  1674. '同义改写': '#f59e0b',
  1675. '加词': '#3b82f6',
  1676. '抽象改写': '#8b5cf6',
  1677. '基于部分匹配改进': '#ec4899',
  1678. '结果分支-抽象改写': '#a855f7',
  1679. '结果分支-同义改写': '#fb923c',
  1680. 'query_to_note': '#ec4899',
  1681. };
  1682. const color = edgeColors[edge.strategy] || edgeColors[edge.edge_type] || '#d1d5db';
  1683. const isNoteEdge = edge.edge_type === 'query_to_note';
  1684. edges.push({
  1685. id: \`edge-\${index}\`,
  1686. source: originalIdToCanvasId[edge.from], // 使用画布ID
  1687. target: originalIdToCanvasId[edge.to], // 使用画布ID
  1688. type: 'simplebezier', // 使用简单贝塞尔曲线
  1689. animated: isNoteEdge,
  1690. style: {
  1691. stroke: color,
  1692. strokeWidth: isNoteEdge ? 2.5 : 2,
  1693. strokeDasharray: isNoteEdge ? '5,5' : '8,4',
  1694. },
  1695. markerEnd: {
  1696. type: 'arrowclosed',
  1697. color: color,
  1698. width: 20,
  1699. height: 20,
  1700. },
  1701. });
  1702. });
  1703. // 使用 dagre 自动计算布局 - 从左到右
  1704. return getLayoutedElements(nodes, edges, 'LR');
  1705. }
  1706. function FlowContent() {
  1707. // 画布使用简化数据
  1708. const { nodes: initialNodes, edges: initialEdges } = useMemo(() => {
  1709. console.log('🔍 Transforming data for canvas...');
  1710. const result = transformData(data);
  1711. console.log('✅ Canvas data:', result.nodes.length, 'nodes,', result.edges.length, 'edges');
  1712. return result;
  1713. }, []);
  1714. // 目录使用完整数据(如果存在)
  1715. const { nodes: fullNodes, edges: fullEdges } = useMemo(() => {
  1716. if (data.fullData) {
  1717. console.log('🔍 Transforming full data for tree directory...');
  1718. const result = transformData(data.fullData);
  1719. console.log('✅ Directory data:', result.nodes.length, 'nodes,', result.edges.length, 'edges');
  1720. return result;
  1721. }
  1722. // 如果没有 fullData,使用简化数据
  1723. return { nodes: initialNodes, edges: initialEdges };
  1724. }, [initialNodes, initialEdges]);
  1725. // 初始化:找出所有有子节点的节点,默认折叠(画布节点)
  1726. const initialCollapsedNodes = useMemo(() => {
  1727. const nodesWithChildren = new Set();
  1728. initialEdges.forEach(edge => {
  1729. nodesWithChildren.add(edge.source);
  1730. });
  1731. // 排除根节点(level 0),让根节点默认展开
  1732. const rootNode = initialNodes.find(n => n.data.level === 0);
  1733. if (rootNode) {
  1734. nodesWithChildren.delete(rootNode.id);
  1735. }
  1736. return nodesWithChildren;
  1737. }, [initialNodes, initialEdges]);
  1738. // 树节点的折叠状态需要在树构建后初始化
  1739. const [collapsedNodes, setCollapsedNodes] = useState(() => initialCollapsedNodes);
  1740. const [collapsedTreeNodes, setCollapsedTreeNodes] = useState(new Set());
  1741. const [selectedNodeId, setSelectedNodeId] = useState(null);
  1742. const [hiddenNodes, setHiddenNodes] = useState(new Set()); // 用户手动隐藏的节点
  1743. const [focusMode, setFocusMode] = useState(false); // 全局聚焦模式,默认关闭
  1744. const [focusedNodeId, setFocusedNodeId] = useState(null); // 单独聚焦的节点ID
  1745. const [sidebarWidth, setSidebarWidth] = useState(400); // 左侧目录宽度
  1746. const [isResizing, setIsResizing] = useState(false); // 是否正在拖拽调整宽度
  1747. // 拖拽调整侧边栏宽度的处理逻辑
  1748. const handleMouseDown = useCallback(() => {
  1749. setIsResizing(true);
  1750. }, []);
  1751. useEffect(() => {
  1752. if (!isResizing) return;
  1753. const handleMouseMove = (e) => {
  1754. const newWidth = e.clientX;
  1755. // 限制宽度范围:300px - 700px
  1756. if (newWidth >= 300 && newWidth <= 700) {
  1757. setSidebarWidth(newWidth);
  1758. }
  1759. };
  1760. const handleMouseUp = () => {
  1761. setIsResizing(false);
  1762. };
  1763. document.addEventListener('mousemove', handleMouseMove);
  1764. document.addEventListener('mouseup', handleMouseUp);
  1765. return () => {
  1766. document.removeEventListener('mousemove', handleMouseMove);
  1767. document.removeEventListener('mouseup', handleMouseUp);
  1768. };
  1769. }, [isResizing]);
  1770. // 获取 React Flow 实例以控制画布
  1771. const { setCenter, fitView } = useReactFlow();
  1772. // 获取某个节点的所有后代节点ID
  1773. const getDescendants = useCallback((nodeId) => {
  1774. const descendants = new Set();
  1775. const queue = [nodeId];
  1776. while (queue.length > 0) {
  1777. const current = queue.shift();
  1778. initialEdges.forEach(edge => {
  1779. if (edge.source === current && !descendants.has(edge.target)) {
  1780. descendants.add(edge.target);
  1781. queue.push(edge.target);
  1782. }
  1783. });
  1784. }
  1785. return descendants;
  1786. }, [initialEdges]);
  1787. // 获取直接父节点
  1788. const getDirectParents = useCallback((nodeId) => {
  1789. const parents = [];
  1790. initialEdges.forEach(edge => {
  1791. if (edge.target === nodeId) {
  1792. parents.push(edge.source);
  1793. }
  1794. });
  1795. return parents;
  1796. }, [initialEdges]);
  1797. // 获取直接子节点
  1798. const getDirectChildren = useCallback((nodeId) => {
  1799. const children = [];
  1800. initialEdges.forEach(edge => {
  1801. if (edge.source === nodeId) {
  1802. children.push(edge.target);
  1803. }
  1804. });
  1805. return children;
  1806. }, [initialEdges]);
  1807. // 切换节点折叠状态
  1808. const toggleNodeCollapse = useCallback((nodeId) => {
  1809. setCollapsedNodes(prev => {
  1810. const newSet = new Set(prev);
  1811. const descendants = getDescendants(nodeId);
  1812. if (newSet.has(nodeId)) {
  1813. // 展开:移除此节点,但保持其他折叠的节点
  1814. newSet.delete(nodeId);
  1815. } else {
  1816. // 折叠:添加此节点
  1817. newSet.add(nodeId);
  1818. }
  1819. return newSet;
  1820. });
  1821. }, [getDescendants]);
  1822. // 过滤可见的节点和边,并重新计算布局
  1823. const { nodes, edges } = useMemo(() => {
  1824. const nodesToHide = new Set();
  1825. // 判断使用哪个节点ID进行聚焦:优先使用单独聚焦的节点,否则使用全局聚焦模式的选中节点
  1826. const effectiveFocusNodeId = focusedNodeId || (focusMode ? selectedNodeId : null);
  1827. // 聚焦模式:只显示聚焦节点、其父节点和直接子节点
  1828. if (effectiveFocusNodeId) {
  1829. const visibleInFocus = new Set([effectiveFocusNodeId]);
  1830. // 添加所有父节点
  1831. initialEdges.forEach(edge => {
  1832. if (edge.target === effectiveFocusNodeId) {
  1833. visibleInFocus.add(edge.source);
  1834. }
  1835. });
  1836. // 添加所有直接子节点
  1837. initialEdges.forEach(edge => {
  1838. if (edge.source === effectiveFocusNodeId) {
  1839. visibleInFocus.add(edge.target);
  1840. }
  1841. });
  1842. // 隐藏不在聚焦范围内的节点
  1843. initialNodes.forEach(node => {
  1844. if (!visibleInFocus.has(node.id)) {
  1845. nodesToHide.add(node.id);
  1846. }
  1847. });
  1848. } else {
  1849. // 非聚焦模式:使用原有的折叠逻辑
  1850. // 收集所有被折叠节点的后代
  1851. collapsedNodes.forEach(collapsedId => {
  1852. const descendants = getDescendants(collapsedId);
  1853. descendants.forEach(id => nodesToHide.add(id));
  1854. });
  1855. }
  1856. // 添加用户手动隐藏的节点
  1857. hiddenNodes.forEach(id => nodesToHide.add(id));
  1858. const visibleNodes = initialNodes
  1859. .filter(node => !nodesToHide.has(node.id))
  1860. .map(node => ({
  1861. ...node,
  1862. data: {
  1863. ...node.data,
  1864. isCollapsed: collapsedNodes.has(node.id),
  1865. hasChildren: initialEdges.some(e => e.source === node.id),
  1866. onToggleCollapse: () => toggleNodeCollapse(node.id),
  1867. onHideSelf: () => {
  1868. setHiddenNodes(prev => {
  1869. const newSet = new Set(prev);
  1870. newSet.add(node.id);
  1871. return newSet;
  1872. });
  1873. },
  1874. onFocus: () => {
  1875. // 切换聚焦状态
  1876. if (focusedNodeId === node.id) {
  1877. setFocusedNodeId(null); // 如果已经聚焦,则取消聚焦
  1878. } else {
  1879. // 先取消之前的聚焦,然后聚焦到当前节点
  1880. setFocusedNodeId(node.id);
  1881. // 延迟聚焦视图到该节点
  1882. setTimeout(() => {
  1883. fitView({
  1884. nodes: [{ id: node.id }],
  1885. duration: 800,
  1886. padding: 0.3,
  1887. });
  1888. }, 100);
  1889. }
  1890. },
  1891. isFocused: focusedNodeId === node.id,
  1892. isHighlighted: selectedNodeId === node.id,
  1893. }
  1894. }));
  1895. const visibleEdges = initialEdges.filter(
  1896. edge => !nodesToHide.has(edge.source) && !nodesToHide.has(edge.target)
  1897. );
  1898. // 重新计算布局 - 只对可见节点
  1899. if (typeof window !== 'undefined' && typeof window.dagre !== 'undefined') {
  1900. try {
  1901. const dagreGraph = new window.dagre.graphlib.Graph();
  1902. dagreGraph.setDefaultEdgeLabel(() => ({}));
  1903. dagreGraph.setGraph({
  1904. rankdir: 'LR',
  1905. nodesep: 250, // 增加垂直间距以适应更高的note节点
  1906. ranksep: 400, // 增加水平间距
  1907. });
  1908. visibleNodes.forEach((node) => {
  1909. let nodeWidth = 320;
  1910. let nodeHeight = 220;
  1911. // note 节点有轮播图,需要更大的空间
  1912. if (node.type === 'note') {
  1913. nodeWidth = 360;
  1914. nodeHeight = 550; // 更新以适应 2:3 比例的轮播图(约450px)+ 其他内容
  1915. }
  1916. dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight });
  1917. });
  1918. visibleEdges.forEach((edge) => {
  1919. dagreGraph.setEdge(edge.source, edge.target);
  1920. });
  1921. window.dagre.layout(dagreGraph);
  1922. visibleNodes.forEach((node) => {
  1923. const nodeWithPosition = dagreGraph.node(node.id);
  1924. if (nodeWithPosition) {
  1925. // 根据节点类型获取对应的尺寸
  1926. let nodeWidth = 320;
  1927. let nodeHeight = 220;
  1928. if (node.type === 'note') {
  1929. nodeWidth = 360;
  1930. nodeHeight = 550; // 更新以适应 2:3 比例的轮播图
  1931. }
  1932. node.position = {
  1933. x: nodeWithPosition.x - nodeWidth / 2,
  1934. y: nodeWithPosition.y - nodeHeight / 2,
  1935. };
  1936. node.targetPosition = 'left';
  1937. node.sourcePosition = 'right';
  1938. }
  1939. });
  1940. // 为同层级的 note 节点添加交错偏移,避免视觉重叠
  1941. console.log('[DYNAMIC LAYOUT] 开始应用交错偏移');
  1942. const noteNodesToStagger = visibleNodes.filter(n => n.type === 'note');
  1943. console.log('[DYNAMIC LAYOUT] note 节点数:', noteNodesToStagger.length);
  1944. if (noteNodesToStagger.length > 1) {
  1945. // 按 Y 坐标排序
  1946. noteNodesToStagger.sort((a, b) => a.position.y - b.position.y);
  1947. console.log('[DYNAMIC LAYOUT] 排序后准备应用偏移:');
  1948. noteNodesToStagger.forEach((n, i) => {
  1949. console.log(' [' + i + '] ' + n.id.substring(0, 40) + '... | pos=(' + n.position.x.toFixed(0) + ', ' + n.position.y.toFixed(0) + ')');
  1950. });
  1951. // 为相邻的 note 节点添加 X 方向的交错
  1952. let appliedCount = 0;
  1953. noteNodesToStagger.forEach((node, index) => {
  1954. if (index > 0) {
  1955. // 奇数索引向右偏移,偶数索引向左偏移
  1956. const offset = (index % 2 === 0) ? -200 : 200;
  1957. const oldX = node.position.x;
  1958. node.position.x += offset;
  1959. appliedCount++;
  1960. console.log('[DYNAMIC LAYOUT] ✅ 应用偏移: X ' + oldX.toFixed(0) + ' → ' + node.position.x.toFixed(0) + ' (偏移' + offset + 'px)');
  1961. }
  1962. });
  1963. console.log('[DYNAMIC LAYOUT] 总共应用了 ' + appliedCount + ' 次偏移');
  1964. }
  1965. console.log('✅ Dynamic layout recalculated for', visibleNodes.length, 'visible nodes');
  1966. } catch (error) {
  1967. console.error('❌ Error in dynamic layout:', error);
  1968. }
  1969. }
  1970. return { nodes: visibleNodes, edges: visibleEdges };
  1971. }, [initialNodes, initialEdges, collapsedNodes, hiddenNodes, focusMode, focusedNodeId, getDescendants, toggleNodeCollapse, selectedNodeId]);
  1972. // 构建树形结构 - 允许一个节点有多个父节点
  1973. // 为目录构建树(使用完整数据)
  1974. const buildTree = useCallback(() => {
  1975. // 使用完整数据构建目录树
  1976. const nodeMap = new Map();
  1977. fullNodes.forEach(node => {
  1978. nodeMap.set(node.id, node);
  1979. });
  1980. // 为每个节点创建树节点的副本(允许多次出现)
  1981. const createTreeNode = (nodeId, pathKey) => {
  1982. const node = nodeMap.get(nodeId);
  1983. if (!node) return null;
  1984. return {
  1985. ...node,
  1986. treeKey: pathKey, // 唯一的树路径key,用于React key
  1987. children: []
  1988. };
  1989. };
  1990. // 构建父子关系映射:记录每个节点的所有父节点,去重边
  1991. const parentToChildren = new Map();
  1992. const childToParents = new Map();
  1993. fullEdges.forEach(edge => {
  1994. // 记录父->子关系(去重:同一个父节点到同一个子节点只记录一次)
  1995. if (!parentToChildren.has(edge.source)) {
  1996. parentToChildren.set(edge.source, []);
  1997. }
  1998. const children = parentToChildren.get(edge.source);
  1999. if (!children.includes(edge.target)) {
  2000. children.push(edge.target);
  2001. }
  2002. // 记录子->父关系(用于判断是否有多个父节点,也去重)
  2003. if (!childToParents.has(edge.target)) {
  2004. childToParents.set(edge.target, []);
  2005. }
  2006. const parents = childToParents.get(edge.target);
  2007. if (!parents.includes(edge.source)) {
  2008. parents.push(edge.source);
  2009. }
  2010. });
  2011. // 递归构建树
  2012. const buildSubtree = (nodeId, pathKey, visitedInPath) => {
  2013. // 避免循环引用:如果当前路径中已经访问过这个节点,跳过
  2014. if (visitedInPath.has(nodeId)) {
  2015. return null;
  2016. }
  2017. const treeNode = createTreeNode(nodeId, pathKey);
  2018. if (!treeNode) return null;
  2019. const newVisitedInPath = new Set(visitedInPath);
  2020. newVisitedInPath.add(nodeId);
  2021. const children = parentToChildren.get(nodeId) || [];
  2022. treeNode.children = children
  2023. .map((childId, index) => buildSubtree(childId, pathKey + '-' + childId + '-' + index, newVisitedInPath))
  2024. .filter(child => child !== null);
  2025. return treeNode;
  2026. };
  2027. // 找出所有根节点(没有入边的节点)
  2028. const hasParent = new Set();
  2029. fullEdges.forEach(edge => {
  2030. hasParent.add(edge.target);
  2031. });
  2032. const roots = [];
  2033. fullNodes.forEach((node, index) => {
  2034. if (!hasParent.has(node.id)) {
  2035. const treeNode = buildSubtree(node.id, 'root-' + node.id + '-' + index, new Set());
  2036. if (treeNode) roots.push(treeNode);
  2037. }
  2038. });
  2039. return roots;
  2040. }, [fullNodes, fullEdges]);
  2041. const treeRoots = useMemo(() => buildTree(), [buildTree]);
  2042. // 生成树形文本结构(使用完整数据)
  2043. const generateTreeText = useCallback(() => {
  2044. const lines = [];
  2045. // 递归生成树形文本
  2046. const traverse = (nodes, prefix = '', isLast = true, depth = 0) => {
  2047. nodes.forEach((node, index) => {
  2048. const isLastNode = index === nodes.length - 1;
  2049. const nodeData = fullNodes.find(n => n.id === node.id)?.data || {};
  2050. const nodeType = nodeData.nodeType || node.data?.nodeType || 'unknown';
  2051. const title = nodeData.title || node.data?.title || node.id;
  2052. // 优先从node.data获取score,然后从nodeData获取
  2053. let score = null;
  2054. if (node.data?.score !== undefined && node.data?.score !== null) {
  2055. score = node.data.score;
  2056. } else if (node.data?.relevance_score !== undefined && node.data?.relevance_score !== null) {
  2057. score = node.data.relevance_score;
  2058. } else if (nodeData.score !== undefined && nodeData.score !== null) {
  2059. score = nodeData.score;
  2060. } else if (nodeData.relevance_score !== undefined && nodeData.relevance_score !== null) {
  2061. score = nodeData.relevance_score;
  2062. }
  2063. const strategy = nodeData.strategy || node.data?.strategy || '';
  2064. // 构建当前行 - score可能是数字或字符串,step/round节点不显示分数
  2065. const connector = isLastNode ? '└─' : '├─';
  2066. let scoreText = '';
  2067. if (nodeType !== 'step' && nodeType !== 'round' && score !== null && score !== undefined) {
  2068. // score可能已经是字符串格式(如 "0.05"),也可能是数字
  2069. const scoreStr = typeof score === 'number' ? score.toFixed(2) : score;
  2070. scoreText = \` (分数: \${scoreStr})\`;
  2071. }
  2072. const strategyText = strategy ? \` [\${strategy}]\` : '';
  2073. lines.push(\`\${prefix}\${connector} \${title}\${scoreText}\${strategyText}\`);
  2074. // 递归处理子节点
  2075. if (node.children && node.children.length > 0) {
  2076. const childPrefix = prefix + (isLastNode ? ' ' : '│ ');
  2077. traverse(node.children, childPrefix, isLastNode, depth + 1);
  2078. }
  2079. });
  2080. };
  2081. // 添加标题
  2082. const rootNode = fullNodes.find(n => n.data?.level === 0);
  2083. if (rootNode) {
  2084. lines.push(\`📊 查询扩展树形结构\`);
  2085. lines.push(\`原始问题: \${rootNode.data.title || rootNode.data.query}\`);
  2086. lines.push('');
  2087. }
  2088. traverse(treeRoots);
  2089. return lines.join('\\n');
  2090. }, [treeRoots, fullNodes]);
  2091. // 复制树形结构到剪贴板
  2092. const copyTreeToClipboard = useCallback(async () => {
  2093. try {
  2094. const treeText = generateTreeText();
  2095. await navigator.clipboard.writeText(treeText);
  2096. alert('✅ 树形结构已复制到剪贴板!');
  2097. } catch (err) {
  2098. console.error('复制失败:', err);
  2099. alert('❌ 复制失败,请手动复制');
  2100. }
  2101. }, [generateTreeText]);
  2102. // 初始化树节点折叠状态
  2103. useEffect(() => {
  2104. const getAllTreeKeys = (nodes) => {
  2105. const keys = new Set();
  2106. const traverse = (node) => {
  2107. if (node.children && node.children.length > 0) {
  2108. // 排除根节点
  2109. if (node.data.level !== 0) {
  2110. keys.add(node.treeKey);
  2111. }
  2112. node.children.forEach(traverse);
  2113. }
  2114. };
  2115. nodes.forEach(traverse);
  2116. return keys;
  2117. };
  2118. setCollapsedTreeNodes(getAllTreeKeys(treeRoots));
  2119. }, [treeRoots]);
  2120. // 映射完整节点ID到画布简化节点ID
  2121. const mapTreeNodeToCanvasNode = useCallback((treeNodeId) => {
  2122. // 如果是简化模式,需要映射
  2123. if (data.fullData) {
  2124. // 从完整数据中找到节点
  2125. const fullNode = fullNodes.find(n => n.id === treeNodeId);
  2126. if (!fullNode) return treeNodeId;
  2127. // 根据节点类型和文本找到画布上的简化节点
  2128. const nodeText = fullNode.data.title || fullNode.data.query;
  2129. const nodeType = fullNode.data.nodeType || fullNode.type;
  2130. // Query类节点:找 query_xxx
  2131. if (['q', 'seg', 'sug', 'add_word', 'query'].includes(nodeType)) {
  2132. const canvasNode = initialNodes.find(n =>
  2133. (n.data.title === nodeText || n.data.query === nodeText) &&
  2134. ['query'].includes(n.type)
  2135. );
  2136. return canvasNode ? canvasNode.id : treeNodeId;
  2137. }
  2138. // Post节点:按note_id查找
  2139. if (nodeType === 'post' || nodeType === 'note') {
  2140. const noteId = fullNode.data.note_id;
  2141. if (noteId) {
  2142. const canvasNode = initialNodes.find(n => n.data.note_id === noteId);
  2143. return canvasNode ? canvasNode.id : treeNodeId;
  2144. }
  2145. }
  2146. // 其他节点类型(Round/Step等):直接返回
  2147. return treeNodeId;
  2148. }
  2149. // 非简化模式,直接返回
  2150. return treeNodeId;
  2151. }, [data.fullData, fullNodes, initialNodes]);
  2152. const renderTree = useCallback((treeNodes, level = 0) => {
  2153. return treeNodes.map(node => {
  2154. // 使用 treeKey 来区分树中的不同实例
  2155. const isCollapsed = collapsedTreeNodes.has(node.treeKey);
  2156. const isSelected = selectedNodeId === node.id;
  2157. return (
  2158. <TreeNode
  2159. key={node.treeKey}
  2160. node={node}
  2161. level={level}
  2162. isCollapsed={isCollapsed}
  2163. isSelected={isSelected}
  2164. onToggle={() => {
  2165. setCollapsedTreeNodes(prev => {
  2166. const newSet = new Set(prev);
  2167. if (newSet.has(node.treeKey)) {
  2168. newSet.delete(node.treeKey);
  2169. } else {
  2170. newSet.add(node.treeKey);
  2171. }
  2172. return newSet;
  2173. });
  2174. }}
  2175. onSelect={() => {
  2176. // 将目录节点ID映射到画布节点ID
  2177. const treeNodeId = node.id;
  2178. const canvasNodeId = mapTreeNodeToCanvasNode(treeNodeId);
  2179. // 检查画布上是否存在这个节点
  2180. const canvasNodeExists = initialNodes.some(n => n.id === canvasNodeId);
  2181. if (!canvasNodeExists) {
  2182. console.warn(\`节点 \${canvasNodeId} 在画布上不存在(可能被简化了)\`);
  2183. return;
  2184. }
  2185. const nodeId = canvasNodeId;
  2186. // 展开所有祖先节点
  2187. const ancestorIds = [nodeId];
  2188. const findAncestors = (id) => {
  2189. initialEdges.forEach(edge => {
  2190. if (edge.target === id && !ancestorIds.includes(edge.source)) {
  2191. ancestorIds.push(edge.source);
  2192. findAncestors(edge.source);
  2193. }
  2194. });
  2195. };
  2196. findAncestors(nodeId);
  2197. // 如果节点或其祖先被隐藏,先恢复它们
  2198. setHiddenNodes(prev => {
  2199. const newSet = new Set(prev);
  2200. ancestorIds.forEach(id => newSet.delete(id));
  2201. return newSet;
  2202. });
  2203. setSelectedNodeId(nodeId);
  2204. // 获取选中节点的直接子节点
  2205. const childrenIds = [];
  2206. initialEdges.forEach(edge => {
  2207. if (edge.source === nodeId) {
  2208. childrenIds.push(edge.target);
  2209. }
  2210. });
  2211. setCollapsedNodes(prev => {
  2212. const newSet = new Set(prev);
  2213. // 展开所有祖先节点
  2214. ancestorIds.forEach(id => newSet.delete(id));
  2215. // 展开选中节点本身
  2216. newSet.delete(nodeId);
  2217. // 展开选中节点的直接子节点
  2218. childrenIds.forEach(id => newSet.delete(id));
  2219. return newSet;
  2220. });
  2221. // 延迟聚焦,等待节点展开和布局重新计算
  2222. setTimeout(() => {
  2223. fitView({
  2224. nodes: [{ id: nodeId }],
  2225. duration: 800,
  2226. padding: 0.3,
  2227. });
  2228. }, 300);
  2229. }}
  2230. >
  2231. {node.children && node.children.length > 0 && renderTree(node.children, level + 1)}
  2232. </TreeNode>
  2233. );
  2234. });
  2235. }, [collapsedTreeNodes, selectedNodeId, nodes, setCenter, initialEdges, setCollapsedNodes, fitView, mapTreeNodeToCanvasNode, initialNodes, setHiddenNodes]);
  2236. console.log('📊 Rendering with', nodes.length, 'visible nodes and', edges.length, 'visible edges');
  2237. if (nodes.length === 0) {
  2238. return (
  2239. <div style={{ padding: 50, color: 'red', fontSize: 20 }}>
  2240. ERROR: No nodes to display!
  2241. </div>
  2242. );
  2243. }
  2244. return (
  2245. <div style={{ width: '100vw', height: '100vh', background: '#f9fafb', display: 'flex', flexDirection: 'column' }}>
  2246. {/* 顶部面包屑导航栏 */}
  2247. <div style={{
  2248. minHeight: '48px',
  2249. maxHeight: '120px',
  2250. background: 'white',
  2251. borderBottom: '1px solid #e5e7eb',
  2252. display: 'flex',
  2253. alignItems: 'flex-start',
  2254. padding: '12px 24px',
  2255. zIndex: 1000,
  2256. boxShadow: '0 1px 3px rgba(0, 0, 0, 0.05)',
  2257. flexShrink: 0,
  2258. overflowY: 'auto',
  2259. }}>
  2260. <div style={{ width: '100%' }}>
  2261. {selectedNodeId ? (
  2262. <div style={{ fontSize: '12px', color: '#6b7280' }}>
  2263. {/* 面包屑导航 - 显示所有路径 */}
  2264. {(() => {
  2265. const selectedNode = nodes.find(n => n.id === selectedNodeId);
  2266. if (!selectedNode) return null;
  2267. // 找到所有从根节点到当前节点的路径
  2268. const findAllPaths = (targetId) => {
  2269. const paths = [];
  2270. const buildPath = (nodeId, currentPath) => {
  2271. const node = initialNodes.find(n => n.id === nodeId);
  2272. if (!node) return;
  2273. const newPath = [node, ...currentPath];
  2274. // 找到所有父节点
  2275. const parents = initialEdges.filter(e => e.target === nodeId).map(e => e.source);
  2276. if (parents.length === 0) {
  2277. // 到达根节点
  2278. paths.push(newPath);
  2279. } else {
  2280. // 递归处理所有父节点
  2281. parents.forEach(parentId => {
  2282. buildPath(parentId, newPath);
  2283. });
  2284. }
  2285. };
  2286. buildPath(targetId, []);
  2287. return paths;
  2288. };
  2289. const allPaths = findAllPaths(selectedNodeId);
  2290. // 去重:将路径转换为字符串进行比较
  2291. const uniquePaths = [];
  2292. const pathStrings = new Set();
  2293. allPaths.forEach(path => {
  2294. const pathString = path.map(n => n.id).join('->');
  2295. if (!pathStrings.has(pathString)) {
  2296. pathStrings.add(pathString);
  2297. uniquePaths.push(path);
  2298. }
  2299. });
  2300. return (
  2301. <div style={{ display: 'flex', flexDirection: 'column', gap: '6px' }}>
  2302. {uniquePaths.map((path, pathIndex) => (
  2303. <div key={pathIndex} style={{ display: 'flex', alignItems: 'center', gap: '6px', flexWrap: 'wrap' }}>
  2304. {pathIndex > 0 && <span style={{ color: '#d1d5db', marginRight: '4px' }}>或</span>}
  2305. {path.map((node, index) => {
  2306. // 获取节点的 score、strategy 和 isSelected
  2307. const nodeScore = node.data.score ? parseFloat(node.data.score) : 0;
  2308. const nodeStrategy = getPrimaryStrategy(node.data); // 使用智能提取函数
  2309. const strategyColor = getStrategyColor(nodeStrategy);
  2310. const nodeIsSelected = node.type === 'note' ? node.data.matchLevel !== 'unsatisfied' : node.data.isSelected !== false;
  2311. const nodeActualType = node.data.nodeType || node.type; // 获取实际节点类型
  2312. // 计算路径节点字体颜色:根据分数提升幅度判断
  2313. let pathFontColor = '#374151'; // 默认颜色
  2314. if (node.type === 'note') {
  2315. pathFontColor = node.data.matchLevel === 'unsatisfied' ? '#ef4444' : '#374151';
  2316. } else if (node.data.seed_score !== undefined) {
  2317. const parentScore = parseFloat(node.data.seed_score);
  2318. const gain = nodeScore - parentScore;
  2319. pathFontColor = gain >= 0.05 ? '#16a34a' : '#ef4444';
  2320. } else if (index > 0) {
  2321. const prevNode = path[index - 1];
  2322. const prevScore = prevNode.data.score ? parseFloat(prevNode.data.score) : 0;
  2323. const gain = nodeScore - prevScore;
  2324. pathFontColor = gain >= 0.05 ? '#16a34a' : '#ef4444';
  2325. } else if (node.data.isSelected === false) {
  2326. pathFontColor = '#ef4444';
  2327. }
  2328. return (
  2329. <React.Fragment key={node.id + '-' + index}>
  2330. <span
  2331. onClick={() => {
  2332. const nodeId = node.id;
  2333. // 找到所有祖先节点
  2334. const ancestorIds = [nodeId];
  2335. const findAncestors = (id) => {
  2336. initialEdges.forEach(edge => {
  2337. if (edge.target === id && !ancestorIds.includes(edge.source)) {
  2338. ancestorIds.push(edge.source);
  2339. findAncestors(edge.source);
  2340. }
  2341. });
  2342. };
  2343. findAncestors(nodeId);
  2344. // 如果节点或其祖先被隐藏,先恢复它们
  2345. setHiddenNodes(prev => {
  2346. const newSet = new Set(prev);
  2347. ancestorIds.forEach(id => newSet.delete(id));
  2348. return newSet;
  2349. });
  2350. // 展开目录树中到达该节点的路径
  2351. // 需要找到所有包含该节点的树路径的 treeKey,并展开它们的父节点
  2352. setCollapsedTreeNodes(prev => {
  2353. const newSet = new Set(prev);
  2354. // 清空所有折叠状态,让目录树完全展开到选中节点
  2355. // 这样可以确保选中节点在目录中可见
  2356. return new Set();
  2357. });
  2358. setSelectedNodeId(nodeId);
  2359. setTimeout(() => {
  2360. fitView({
  2361. nodes: [{ id: nodeId }],
  2362. duration: 800,
  2363. padding: 0.3,
  2364. });
  2365. }, 100);
  2366. }}
  2367. style={{
  2368. padding: '6px 8px',
  2369. borderRadius: '4px',
  2370. background: 'white',
  2371. border: index === path.length - 1 ? '2px solid #3b82f6' : '1px solid #d1d5db',
  2372. color: '#374151',
  2373. fontWeight: index === path.length - 1 ? '600' : '400',
  2374. width: '180px',
  2375. cursor: 'pointer',
  2376. transition: 'all 0.2s ease',
  2377. position: 'relative',
  2378. display: 'inline-flex',
  2379. flexDirection: 'column',
  2380. gap: '4px',
  2381. }}
  2382. onMouseEnter={(e) => {
  2383. e.currentTarget.style.opacity = '0.8';
  2384. }}
  2385. onMouseLeave={(e) => {
  2386. e.currentTarget.style.opacity = '1';
  2387. }}
  2388. title={\`\${node.data.title || node.id} (Score: \${nodeScore.toFixed(2)}, Strategy: \${nodeStrategy}, Selected: \${nodeIsSelected})\`}
  2389. >
  2390. {/* 上半部分:竖线 + 图标 + 文字 + 分数 */}
  2391. <div style={{
  2392. display: 'flex',
  2393. alignItems: 'center',
  2394. gap: '6px',
  2395. }}>
  2396. {/* 策略类型竖线 */}
  2397. <div style={{
  2398. width: '3px',
  2399. height: '16px',
  2400. background: strategyColor,
  2401. borderRadius: '2px',
  2402. flexShrink: 0,
  2403. }} />
  2404. {/* 节点文字 - 左侧 */}
  2405. <span style={{
  2406. flex: 1,
  2407. fontSize: '12px',
  2408. color: pathFontColor,
  2409. overflow: 'hidden',
  2410. textOverflow: 'ellipsis',
  2411. whiteSpace: 'nowrap',
  2412. }}>
  2413. {node.data.title || node.id}
  2414. </span>
  2415. {/* 域标识 - 右侧,挨着分数 */}
  2416. {(node.data.domain_type || node.data.domains_str || (node.data.domain_index !== null && node.data.domain_index !== undefined)) && (
  2417. <span style={{
  2418. fontSize: '12px',
  2419. color: '#fff',
  2420. background: '#6366f1',
  2421. padding: '2px 5px',
  2422. borderRadius: '3px',
  2423. flexShrink: 0,
  2424. fontWeight: '600',
  2425. marginLeft: '4px',
  2426. }}
  2427. title={
  2428. node.data.domain_type ? '域: ' + node.data.domain_type + ' (D' + node.data.domain_index + ')' :
  2429. node.data.domains_str ? '域: ' + node.data.domains_str :
  2430. '域 D' + node.data.domain_index
  2431. }
  2432. >
  2433. {node.data.domain_type || node.data.domains_str || ('D' + node.data.domain_index)}
  2434. </span>
  2435. )}
  2436. {/* 分数显示 - 步骤和轮次节点不显示分数 */}
  2437. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  2438. <span style={{
  2439. fontSize: '10px',
  2440. color: '#6b7280',
  2441. fontWeight: '500',
  2442. flexShrink: 0,
  2443. minWidth: '35px',
  2444. textAlign: 'right',
  2445. marginLeft: '4px',
  2446. }}>
  2447. {nodeScore.toFixed(2)}
  2448. </span>
  2449. )}
  2450. </div>
  2451. {/* 分数下划线 - 步骤和轮次节点不显示 */}
  2452. {nodeActualType !== 'step' && nodeActualType !== 'round' && (
  2453. <div style={{
  2454. width: (nodeScore * 100) + '%',
  2455. height: '2px',
  2456. background: getScoreColor(nodeScore),
  2457. borderRadius: '1px',
  2458. marginLeft: '9px',
  2459. }} />
  2460. )}
  2461. </span>
  2462. {index < path.length - 1 && <span style={{ color: '#9ca3af' }}>›</span>}
  2463. </React.Fragment>
  2464. )})}
  2465. </div>
  2466. ))}
  2467. </div>
  2468. );
  2469. })()}
  2470. </div>
  2471. ) : (
  2472. <div style={{ fontSize: '13px', color: '#9ca3af', textAlign: 'center' }}>
  2473. 选择一个节点查看路径
  2474. </div>
  2475. )}
  2476. </div>
  2477. </div>
  2478. {/* 主内容区:目录 + 画布 */}
  2479. <div style={{
  2480. display: 'flex',
  2481. flex: 1,
  2482. overflow: 'hidden',
  2483. cursor: isResizing ? 'col-resize' : 'default',
  2484. userSelect: isResizing ? 'none' : 'auto',
  2485. }}>
  2486. {/* 左侧目录树 */}
  2487. <div style={{
  2488. width: \`\${sidebarWidth}px\`,
  2489. background: 'white',
  2490. borderRight: '1px solid #e5e7eb',
  2491. display: 'flex',
  2492. flexDirection: 'column',
  2493. flexShrink: 0,
  2494. }}>
  2495. <div style={{
  2496. padding: '12px 16px',
  2497. borderBottom: '1px solid #e5e7eb',
  2498. display: 'flex',
  2499. justifyContent: 'space-between',
  2500. alignItems: 'center',
  2501. }}>
  2502. <span style={{
  2503. fontWeight: '600',
  2504. fontSize: '14px',
  2505. color: '#111827',
  2506. }}>
  2507. 节点目录
  2508. </span>
  2509. <div style={{ display: 'flex', gap: '6px' }}>
  2510. <button
  2511. onClick={() => {
  2512. setCollapsedTreeNodes(new Set());
  2513. }}
  2514. style={{
  2515. fontSize: '11px',
  2516. padding: '4px 8px',
  2517. borderRadius: '4px',
  2518. border: '1px solid #d1d5db',
  2519. background: 'white',
  2520. color: '#6b7280',
  2521. cursor: 'pointer',
  2522. fontWeight: '500',
  2523. }}
  2524. title="展开全部节点"
  2525. >
  2526. 全部展开
  2527. </button>
  2528. <button
  2529. onClick={() => {
  2530. const getAllTreeKeys = (nodes) => {
  2531. const keys = new Set();
  2532. const traverse = (node) => {
  2533. if (node.children && node.children.length > 0) {
  2534. keys.add(node.treeKey);
  2535. node.children.forEach(traverse);
  2536. }
  2537. };
  2538. nodes.forEach(traverse);
  2539. return keys;
  2540. };
  2541. setCollapsedTreeNodes(getAllTreeKeys(treeRoots));
  2542. }}
  2543. style={{
  2544. fontSize: '11px',
  2545. padding: '4px 8px',
  2546. borderRadius: '4px',
  2547. border: '1px solid #d1d5db',
  2548. background: 'white',
  2549. color: '#6b7280',
  2550. cursor: 'pointer',
  2551. fontWeight: '500',
  2552. }}
  2553. title="折叠全部节点"
  2554. >
  2555. 全部折叠
  2556. </button>
  2557. <button
  2558. onClick={copyTreeToClipboard}
  2559. style={{
  2560. fontSize: '11px',
  2561. padding: '4px 8px',
  2562. borderRadius: '4px',
  2563. border: '1px solid #3b82f6',
  2564. background: '#3b82f6',
  2565. color: 'white',
  2566. cursor: 'pointer',
  2567. fontWeight: '500',
  2568. transition: 'all 0.2s',
  2569. }}
  2570. onMouseEnter={(e) => e.currentTarget.style.background = '#2563eb'}
  2571. onMouseLeave={(e) => e.currentTarget.style.background = '#3b82f6'}
  2572. title="复制树形结构为文本格式"
  2573. >
  2574. 📋 复制树形结构
  2575. </button>
  2576. </div>
  2577. </div>
  2578. <div style={{
  2579. flex: 1,
  2580. overflowX: 'auto',
  2581. overflowY: 'auto',
  2582. padding: '8px',
  2583. }}>
  2584. <div style={{ minWidth: 'fit-content' }}>
  2585. {renderTree(treeRoots)}
  2586. </div>
  2587. </div>
  2588. </div>
  2589. {/* 可拖拽的分隔条 */}
  2590. <div
  2591. onMouseDown={handleMouseDown}
  2592. style={{
  2593. width: '4px',
  2594. cursor: 'col-resize',
  2595. background: isResizing ? '#3b82f6' : 'transparent',
  2596. transition: isResizing ? 'none' : 'background 0.2s',
  2597. flexShrink: 0,
  2598. position: 'relative',
  2599. }}
  2600. onMouseEnter={(e) => e.currentTarget.style.background = '#e5e7eb'}
  2601. onMouseLeave={(e) => {
  2602. if (!isResizing) e.currentTarget.style.background = 'transparent';
  2603. }}
  2604. >
  2605. {/* 拖拽提示线 */}
  2606. <div style={{
  2607. position: 'absolute',
  2608. top: '50%',
  2609. left: '50%',
  2610. transform: 'translate(-50%, -50%)',
  2611. width: '1px',
  2612. height: '40px',
  2613. background: '#9ca3af',
  2614. opacity: isResizing ? 1 : 0.3,
  2615. }} />
  2616. </div>
  2617. {/* 画布区域 */}
  2618. <div style={{ flex: 1, position: 'relative' }}>
  2619. {/* 右侧图例 */}
  2620. <div style={{
  2621. position: 'absolute',
  2622. top: '20px',
  2623. right: '20px',
  2624. background: 'white',
  2625. padding: '16px',
  2626. borderRadius: '12px',
  2627. boxShadow: '0 4px 12px rgba(0, 0, 0, 0.08)',
  2628. zIndex: 1000,
  2629. maxWidth: '260px',
  2630. border: '1px solid #e5e7eb',
  2631. }}>
  2632. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '12px' }}>
  2633. <h3 style={{ fontSize: '14px', fontWeight: '600', color: '#111827', margin: 0 }}>图例</h3>
  2634. <button
  2635. onClick={() => setFocusMode(!focusMode)}
  2636. style={{
  2637. fontSize: '11px',
  2638. padding: '4px 8px',
  2639. borderRadius: '4px',
  2640. border: '1px solid',
  2641. borderColor: focusMode ? '#3b82f6' : '#d1d5db',
  2642. background: focusMode ? '#3b82f6' : 'white',
  2643. color: focusMode ? 'white' : '#6b7280',
  2644. cursor: 'pointer',
  2645. fontWeight: '500',
  2646. }}
  2647. title={focusMode ? '关闭聚焦模式' : '开启聚焦模式'}
  2648. >
  2649. {focusMode ? '🎯 聚焦' : '📊 全图'}
  2650. </button>
  2651. </div>
  2652. <div style={{ fontSize: '12px' }}>
  2653. {/* 画布节点展开/折叠控制 */}
  2654. <div style={{ marginBottom: '12px', paddingBottom: '12px', borderBottom: '1px solid #f3f4f6' }}>
  2655. <div style={{ fontSize: '12px', fontWeight: '500', marginBottom: '8px', color: '#374151' }}>节点控制</div>
  2656. <div style={{ display: 'flex', gap: '6px' }}>
  2657. <button
  2658. onClick={() => {
  2659. setCollapsedNodes(new Set());
  2660. }}
  2661. style={{
  2662. fontSize: '11px',
  2663. padding: '4px 8px',
  2664. borderRadius: '4px',
  2665. border: '1px solid #d1d5db',
  2666. background: 'white',
  2667. color: '#6b7280',
  2668. cursor: 'pointer',
  2669. fontWeight: '500',
  2670. flex: 1,
  2671. }}
  2672. title="展开画布中所有节点的子节点"
  2673. >
  2674. 全部展开
  2675. </button>
  2676. <button
  2677. onClick={() => {
  2678. const allNodeIds = new Set(initialNodes.map(n => n.id));
  2679. setCollapsedNodes(allNodeIds);
  2680. }}
  2681. style={{
  2682. fontSize: '11px',
  2683. padding: '4px 8px',
  2684. borderRadius: '4px',
  2685. border: '1px solid #d1d5db',
  2686. background: 'white',
  2687. color: '#6b7280',
  2688. cursor: 'pointer',
  2689. fontWeight: '500',
  2690. flex: 1,
  2691. }}
  2692. title="折叠画布中所有节点的子节点"
  2693. >
  2694. 全部折叠
  2695. </button>
  2696. </div>
  2697. </div>
  2698. <div style={{ paddingTop: '12px', borderTop: '1px solid #f3f4f6' }}>
  2699. <div style={{ fontSize: '12px', fontWeight: '500', marginBottom: '8px', color: '#374151' }}>策略类型</div>
  2700. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2701. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#10b981', opacity: 0.7 }}></div>
  2702. <span style={{ color: '#6b7280', fontSize: '11px' }}>初始分词</span>
  2703. </div>
  2704. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2705. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#06b6d4', opacity: 0.7 }}></div>
  2706. <span style={{ color: '#6b7280', fontSize: '11px' }}>调用sug</span>
  2707. </div>
  2708. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2709. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#f59e0b', opacity: 0.7 }}></div>
  2710. <span style={{ color: '#6b7280', fontSize: '11px' }}>同义改写</span>
  2711. </div>
  2712. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2713. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#3b82f6', opacity: 0.7 }}></div>
  2714. <span style={{ color: '#6b7280', fontSize: '11px' }}>加词</span>
  2715. </div>
  2716. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2717. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#8b5cf6', opacity: 0.7 }}></div>
  2718. <span style={{ color: '#6b7280', fontSize: '11px' }}>抽象改写</span>
  2719. </div>
  2720. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2721. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#ec4899', opacity: 0.7 }}></div>
  2722. <span style={{ color: '#6b7280', fontSize: '11px' }}>基于部分匹配改进</span>
  2723. </div>
  2724. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2725. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#a855f7', opacity: 0.7 }}></div>
  2726. <span style={{ color: '#6b7280', fontSize: '11px' }}>结果分支-抽象改写</span>
  2727. </div>
  2728. <div style={{ display: 'flex', alignItems: 'center', margin: '6px 0' }}>
  2729. <div style={{ width: '20px', height: '2px', marginRight: '8px', background: '#fb923c', opacity: 0.7 }}></div>
  2730. <span style={{ color: '#6b7280', fontSize: '11px' }}>结果分支-同义改写</span>
  2731. </div>
  2732. </div>
  2733. <div style={{
  2734. marginTop: '12px',
  2735. paddingTop: '12px',
  2736. borderTop: '1px solid #f3f4f6',
  2737. fontSize: '11px',
  2738. color: '#9ca3af',
  2739. lineHeight: '1.5',
  2740. }}>
  2741. 💡 点击节点左上角 × 隐藏节点
  2742. </div>
  2743. {/* 隐藏节点列表 - 在图例内部 */}
  2744. {hiddenNodes.size > 0 && (
  2745. <div style={{
  2746. marginTop: '12px',
  2747. paddingTop: '12px',
  2748. borderTop: '1px solid #f3f4f6',
  2749. }}>
  2750. <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '8px' }}>
  2751. <h4 style={{ fontSize: '12px', fontWeight: '600', color: '#111827' }}>已隐藏节点</h4>
  2752. <button
  2753. onClick={() => setHiddenNodes(new Set())}
  2754. style={{
  2755. fontSize: '10px',
  2756. color: '#3b82f6',
  2757. background: 'none',
  2758. border: 'none',
  2759. cursor: 'pointer',
  2760. textDecoration: 'underline',
  2761. }}
  2762. >
  2763. 全部恢复
  2764. </button>
  2765. </div>
  2766. <div style={{ fontSize: '12px', maxHeight: '200px', overflow: 'auto' }}>
  2767. {Array.from(hiddenNodes).map(nodeId => {
  2768. const node = initialNodes.find(n => n.id === nodeId);
  2769. if (!node) return null;
  2770. return (
  2771. <div
  2772. key={nodeId}
  2773. style={{
  2774. display: 'flex',
  2775. justifyContent: 'space-between',
  2776. alignItems: 'center',
  2777. padding: '6px 8px',
  2778. margin: '4px 0',
  2779. background: '#f9fafb',
  2780. borderRadius: '6px',
  2781. fontSize: '11px',
  2782. }}
  2783. >
  2784. <span
  2785. style={{
  2786. flex: 1,
  2787. overflow: 'hidden',
  2788. textOverflow: 'ellipsis',
  2789. whiteSpace: 'nowrap',
  2790. color: '#374151',
  2791. }}
  2792. title={node.data.title || nodeId}
  2793. >
  2794. {node.data.title || nodeId}
  2795. </span>
  2796. <button
  2797. onClick={() => {
  2798. setHiddenNodes(prev => {
  2799. const newSet = new Set(prev);
  2800. newSet.delete(nodeId);
  2801. return newSet;
  2802. });
  2803. }}
  2804. style={{
  2805. marginLeft: '8px',
  2806. fontSize: '10px',
  2807. color: '#10b981',
  2808. background: 'none',
  2809. border: 'none',
  2810. cursor: 'pointer',
  2811. flexShrink: 0,
  2812. }}
  2813. >
  2814. 恢复
  2815. </button>
  2816. </div>
  2817. );
  2818. })}
  2819. </div>
  2820. </div>
  2821. )}
  2822. </div>
  2823. </div>
  2824. {/* React Flow 画布 */}
  2825. <ReactFlow
  2826. nodes={nodes}
  2827. edges={edges}
  2828. nodeTypes={nodeTypes}
  2829. fitView
  2830. fitViewOptions={{ padding: 0.2, duration: 500 }}
  2831. minZoom={0.1}
  2832. maxZoom={1.5}
  2833. nodesDraggable={true}
  2834. nodesConnectable={false}
  2835. elementsSelectable={true}
  2836. defaultEdgeOptions={{
  2837. type: 'smoothstep',
  2838. }}
  2839. proOptions={{ hideAttribution: true }}
  2840. onNodeClick={(event, clickedNode) => {
  2841. setSelectedNodeId(clickedNode.id);
  2842. }}
  2843. >
  2844. <Controls style={{ bottom: '20px', left: 'auto', right: '20px' }} />
  2845. <Background variant="dots" gap={20} size={1} color="#e5e7eb" />
  2846. </ReactFlow>
  2847. </div>
  2848. </div>
  2849. </div>
  2850. );
  2851. }
  2852. function App() {
  2853. return (
  2854. <ReactFlowProvider>
  2855. <FlowContent />
  2856. </ReactFlowProvider>
  2857. );
  2858. }
  2859. const root = createRoot(document.getElementById('root'));
  2860. root.render(<App />);
  2861. `;
  2862. fs.writeFileSync(reactComponentPath, reactComponent);
  2863. // 调试:保存临时组件副本用于检查
  2864. fs.writeFileSync(path.join(__dirname, 'debug_component.jsx'), reactComponent);
  2865. console.log('📝 已保存临时组件副本: debug_component.jsx');
  2866. // 使用 esbuild 打包
  2867. console.log('🎨 Building modern visualization...');
  2868. build({
  2869. entryPoints: [reactComponentPath],
  2870. bundle: true,
  2871. outfile: path.join(__dirname, 'bundle_v2.js'),
  2872. format: 'iife',
  2873. loader: {
  2874. '.css': 'css',
  2875. },
  2876. minify: false,
  2877. treeShaking: false, // 禁用tree shaking
  2878. ignoreAnnotations: true, // 忽略所有注解,防止纯函数优化
  2879. keepNames: true, // 保留函数和变量名
  2880. sourcemap: 'inline',
  2881. // 强制所有 React 引用指向同一个位置,避免多副本
  2882. alias: {
  2883. 'react': path.join(__dirname, 'node_modules/react'),
  2884. 'react-dom': path.join(__dirname, 'node_modules/react-dom'),
  2885. 'react/jsx-runtime': path.join(__dirname, 'node_modules/react/jsx-runtime'),
  2886. 'react/jsx-dev-runtime': path.join(__dirname, 'node_modules/react/jsx-dev-runtime'),
  2887. },
  2888. define: {
  2889. 'process.env.NODE_ENV': '"development"' // 使用开发模式,减少优化
  2890. },
  2891. }).then(() => {
  2892. // 读取打包后的 JS
  2893. const bundleJs = fs.readFileSync(path.join(__dirname, 'bundle_v2.js'), 'utf-8');
  2894. // 调试:检查bundle中是否包含评估UI代码
  2895. const hasEvalCode = bundleJs.includes('知识内容') || bundleJs.includes('is_knowledge');
  2896. console.log('📝 Bundle调试: 包含评估代码 =', hasEvalCode);
  2897. if (hasEvalCode) {
  2898. console.log(' ✓ 评估UI代码在bundle中');
  2899. } else {
  2900. console.log(' ⚠️ 评估UI代码不在bundle中,检查临时组件文件...');
  2901. const tempContent = fs.readFileSync(reactComponentPath, 'utf-8');
  2902. const hasTempEvalCode = tempContent.includes('知识内容');
  2903. console.log(' 临时组件文件包含评估代码 =', hasTempEvalCode);
  2904. }
  2905. // 读取 CSS
  2906. const cssPath = path.join(__dirname, 'node_modules/@xyflow/react/dist/style.css');
  2907. const css = fs.readFileSync(cssPath, 'utf-8');
  2908. // 生成最终 HTML
  2909. const html = `<!DOCTYPE html>
  2910. <html lang="zh-CN">
  2911. <head>
  2912. <meta charset="UTF-8">
  2913. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  2914. <title>查询图可视化</title>
  2915. <link rel="preconnect" href="https://fonts.googleapis.com">
  2916. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  2917. <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
  2918. <script src="https://unpkg.com/dagre@0.8.5/dist/dagre.min.js"></script>
  2919. <script>
  2920. // 过滤特定的 React 警告
  2921. const originalError = console.error;
  2922. console.error = (...args) => {
  2923. if (typeof args[0] === 'string' && args[0].includes('Each child in a list should have a unique "key" prop')) {
  2924. return;
  2925. }
  2926. originalError.apply(console, args);
  2927. };
  2928. </script>
  2929. <style>
  2930. * {
  2931. margin: 0;
  2932. padding: 0;
  2933. box-sizing: border-box;
  2934. }
  2935. body {
  2936. font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  2937. overflow: hidden;
  2938. -webkit-font-smoothing: antialiased;
  2939. -moz-osx-font-smoothing: grayscale;
  2940. }
  2941. #root {
  2942. width: 100vw;
  2943. height: 100vh;
  2944. }
  2945. ${css}
  2946. /* 自定义样式覆盖 */
  2947. .react-flow__edge-path {
  2948. stroke-linecap: round;
  2949. }
  2950. .react-flow__controls {
  2951. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  2952. border: 1px solid #e5e7eb;
  2953. border-radius: 8px;
  2954. }
  2955. .react-flow__controls-button {
  2956. border: none;
  2957. border-bottom: 1px solid #e5e7eb;
  2958. }
  2959. .react-flow__controls-button:hover {
  2960. background: #f9fafb;
  2961. }
  2962. </style>
  2963. </head>
  2964. <body>
  2965. <div id="root"></div>
  2966. <script>${bundleJs}</script>
  2967. </body>
  2968. </html>`;
  2969. // 调试:详细检查bundle和HTML内容
  2970. const bundleHas知识 = bundleJs.includes('知识内容');
  2971. const bundleHasIsKnowledge = bundleJs.includes('is_knowledge');
  2972. const bundleHasDataIsKnowledge = bundleJs.includes('data.is_knowledge');
  2973. console.log('📝 Bundle内容检查:');
  2974. console.log(' 包含 "知识内容":', bundleHas知识);
  2975. console.log(' 包含 "is_knowledge":', bundleHasIsKnowledge);
  2976. console.log(' 包含 "data.is_knowledge":', bundleHasDataIsKnowledge);
  2977. console.log(' Bundle长度:', bundleJs.length);
  2978. const htmlHas知识 = html.includes('知识内容');
  2979. const htmlHasIsKnowledge = html.includes('is_knowledge');
  2980. const htmlHasDataIsKnowledge = html.includes('data.is_knowledge');
  2981. console.log('📝 HTML内容检查:');
  2982. console.log(' 包含 "知识内容":', htmlHas知识);
  2983. console.log(' 包含 "is_knowledge":', htmlHasIsKnowledge);
  2984. console.log(' 包含 "data.is_knowledge":', htmlHasDataIsKnowledge);
  2985. console.log(' HTML长度:', html.length);
  2986. // 如果bundle有但HTML没有,保存用于调试
  2987. if ((bundleHas知识 || bundleHasDataIsKnowledge) && !htmlHas知识 && !htmlHasDataIsKnowledge) {
  2988. console.log(' ⚠️ Bundle中有评估代码但HTML中没有!');
  2989. fs.writeFileSync(path.join(__dirname, 'debug_bundle.js'), bundleJs);
  2990. console.log(' 已保存 debug_bundle.js 用于调试');
  2991. }
  2992. // 写入输出文件
  2993. fs.writeFileSync(outputFile, html);
  2994. // 调试:暂时保留bundle文件用于分析
  2995. console.log('📝 保留 bundle_v2.js 和 temp_flow_component_v2.jsx 用于调试');
  2996. // 清理临时文件(调试期间注释掉)
  2997. // fs.unlinkSync(reactComponentPath);
  2998. // fs.unlinkSync(path.join(__dirname, 'bundle_v2.js'));
  2999. console.log('✅ Visualization generated: ' + outputFile);
  3000. console.log('📊 Nodes: ' + Object.keys(data.nodes).length);
  3001. console.log('🔗 Edges: ' + data.edges.length);
  3002. }).catch(error => {
  3003. console.error('❌ Build error:', error);
  3004. process.exit(1);
  3005. });