import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import { viteSingleFile } from 'vite-plugin-singlefile' import fs from 'fs' import path from 'path' // 人设图谱数据路径由 Python 通过环境变量传入 const personaDataPath = process.env.GRAPH_DATA_PATH if (!personaDataPath) { console.error('错误: 请设置 GRAPH_DATA_PATH 环境变量') process.exit(1) } console.log('人设图谱数据文件:', personaDataPath) // 读取人设图谱 JSON 数据 const personaGraphData = JSON.parse(fs.readFileSync(personaDataPath, 'utf-8')) console.log('人设节点数:', Object.keys(personaGraphData.nodes || {}).length) // 帖子图谱数据路径(可选,目录) const postGraphDir = process.env.POST_GRAPH_DIR let postGraphList = [] // 推导图谱目录(与 post_graph 同级的 node_origin_analysis 目录) const derivationGraphDir = postGraphDir ? path.join(path.dirname(postGraphDir), 'node_origin_analysis') : null if (postGraphDir && fs.existsSync(postGraphDir)) { console.log('帖子图谱目录:', postGraphDir) const files = fs.readdirSync(postGraphDir).filter(f => f.endsWith('_帖子图谱.json')) console.log('帖子图谱文件数:', files.length) // 读取推导图谱(如果存在) const derivationGraphs = new Map() if (derivationGraphDir && fs.existsSync(derivationGraphDir)) { console.log('推导图谱目录:', derivationGraphDir) const derivationFiles = fs.readdirSync(derivationGraphDir).filter(f => f.endsWith('_推导图谱.json')) console.log('推导图谱文件数:', derivationFiles.length) for (const file of derivationFiles) { const filePath = path.join(derivationGraphDir, file) const derivationData = JSON.parse(fs.readFileSync(filePath, 'utf-8')) const postId = derivationData.meta?.postId if (postId) { derivationGraphs.set(postId, derivationData) } } } // 读取所有帖子图谱 for (const file of files) { const filePath = path.join(postGraphDir, file) const postData = JSON.parse(fs.readFileSync(filePath, 'utf-8')) // 合并推导图谱(如果存在) const postId = postData.meta?.postId if (postId && derivationGraphs.has(postId)) { const derivation = derivationGraphs.get(postId) console.log(`合并推导图谱到帖子 ${postId}:`, derivation.meta?.stats) // 合并节点(组合节点) for (const [nodeId, node] of Object.entries(derivation.nodes || {})) { if (!postData.nodes[nodeId]) { postData.nodes[nodeId] = node } } // 合并边(推导边和组成边) for (const [edgeId, edge] of Object.entries(derivation.edges || {})) { if (!postData.edges[edgeId]) { postData.edges[edgeId] = edge } } } postGraphList.push(postData) } // 按创建时间降序排序 postGraphList.sort((a, b) => { const dateA = a.meta?.postDetail?.create_time || 0 const dateB = b.meta?.postDetail?.create_time || 0 return dateB - dateA }) } else { console.log('未设置帖子图谱目录或目录不存在') } export default defineConfig({ plugins: [vue(), viteSingleFile()], define: { // 将数据注入为全局常量 __GRAPH_DATA__: JSON.stringify(personaGraphData), __POST_GRAPH_LIST__: JSON.stringify(postGraphList) }, build: { target: 'esnext', outDir: 'dist', assetsInlineLimit: 100000000, chunkSizeWarningLimit: 100000000, cssCodeSplit: false, rollupOptions: { output: { inlineDynamicImports: true } } } })