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 // 推导图谱数据:{ postId: { v3: data, v4: data, ... } } const derivationGraphs = {} // 所有可用的版本 const derivationVersions = new Set() if (postGraphDir && fs.existsSync(postGraphDir)) { console.log('帖子图谱目录:', postGraphDir) const files = fs.readdirSync(postGraphDir).filter(f => f.endsWith('_帖子图谱.json')) console.log('帖子图谱文件数:', files.length) // 读取所有推导图谱(所有版本) if (derivationGraphDir && fs.existsSync(derivationGraphDir)) { console.log('推导图谱目录:', derivationGraphDir) // 匹配所有推导图谱文件:*_推导图谱.json 或 *_推导图谱_v*.json const derivationFiles = fs.readdirSync(derivationGraphDir).filter(f => f.includes('_推导图谱') && 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 const version = derivationData.meta?.version || 'default' if (postId) { if (!derivationGraphs[postId]) { derivationGraphs[postId] = {} } derivationGraphs[postId][version] = derivationData derivationVersions.add(version) console.log(`加载推导图谱: ${postId} [${version}]`, derivationData.meta?.stats) } } } // 读取所有帖子图谱(不合并推导数据,由前端动态合并) for (const file of files) { const filePath = path.join(postGraphDir, file) const postData = JSON.parse(fs.readFileSync(filePath, 'utf-8')) 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 }) console.log('推导图谱版本:', Array.from(derivationVersions).sort()) } else { console.log('未设置帖子图谱目录或目录不存在') } export default defineConfig({ plugins: [vue(), viteSingleFile()], define: { // 将数据注入为全局常量 __GRAPH_DATA__: JSON.stringify(personaGraphData), __POST_GRAPH_LIST__: JSON.stringify(postGraphList), __DERIVATION_GRAPHS__: JSON.stringify(derivationGraphs), __DERIVATION_VERSIONS__: JSON.stringify(Array.from(derivationVersions).sort()) }, build: { target: 'esnext', outDir: 'dist', assetsInlineLimit: 100000000, chunkSizeWarningLimit: 100000000, cssCodeSplit: false, rollupOptions: { output: { inlineDynamicImports: true } } } })