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() // 创作模式数据:{ version: { postId: data } } const creationPatterns = {} // 创作模式版本列表 const creationPatternVersions = [] 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()) // 读取创作模式数据(多版本) const parentDir = path.dirname(postGraphDir) const allDirs = fs.readdirSync(parentDir) // 只匹配 creation_pattern_v 加数字的目录(如 v2, v3, v4) const patternDirs = allDirs.filter(d => /^creation_pattern_v\d+$/.test(d)) .sort((a, b) => { // 按版本号排序 v2, v3, v4... const va = parseInt(a.replace('creation_pattern_v', '')) || 0 const vb = parseInt(b.replace('creation_pattern_v', '')) || 0 return va - vb }) console.log('创作模式版本目录:', patternDirs) for (const dirName of patternDirs) { const version = dirName.replace('creation_pattern_', '') // 'v2', 'v3', 'v4' const dirPath = path.join(parentDir, dirName) if (!fs.statSync(dirPath).isDirectory()) continue creationPatternVersions.push(version) creationPatterns[version] = {} const patternFiles = fs.readdirSync(dirPath).filter(f => f.endsWith('_创作模式.json')) console.log(`创作模式 [${version}] 文件数:`, patternFiles.length) for (const file of patternFiles) { const filePath = path.join(dirPath, file) const patternData = JSON.parse(fs.readFileSync(filePath, 'utf-8')) const postId = patternData.帖子详情?.postId if (postId) { creationPatterns[version][postId] = patternData console.log(` 加载: ${postId}`) } } } } 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()), __CREATION_PATTERNS__: JSON.stringify(creationPatterns), __CREATION_PATTERN_VERSIONS__: JSON.stringify(creationPatternVersions) }, build: { target: 'esnext', outDir: 'dist', assetsInlineLimit: 100000000, chunkSizeWarningLimit: 100000000, cssCodeSplit: false, rollupOptions: { output: { inlineDynamicImports: true } } } })