/** * 应用设置(settings.json,与 config.json 同目录) * 目前包含 OCR 识别方式配置: * ocr.provider: 'local' | 'online' * ocr.online: { url, apiKey, model } 线上 OCR(OpenAI 兼容 /v1/chat/completions) */ const fs = require('fs'); const path = require('path'); const { getConfigDir } = require('./config'); const DEFAULTS = { ocr: { provider: 'local', // local = 本地 tesseract;online = 线上 OCR 接口 online: { url: 'https://ai.88531.cn/v1', // OpenAI 兼容接口(含 /v1),默认 ai.88531.cn apiKey: '', model: 'mimo-v2.6-flash' // 默认视觉模型 } }, telemetry: true, // CardAuth 心跳/版本/公告上报开关 // AI 生图(内置 NewAPI:Pollinations + CF Workers AI;或自定义 OpenAI 兼容提供商) imagegen: { provider: 'newapi', // 'newapi' | 'custom' newapi: { url: 'http://192.168.68.19:3099/v1', apiKey: '', // 留空则用服务器环境变量 NEWAPI_KEY model: 'flux-schnell' // 默认生图模型(弹窗不再选模型,统一在设置里配) }, custom: { url: '', apiKey: '', model: '' } }, // AI 视频:一句话/文本/网页/文档 → 带字幕配音的成片 // provider='inherit' 复用上面的 AI 生图配置;'custom' 用下面自定义的文本模型 videoai: { provider: 'inherit', custom: { url: '', model: '', apiKey: '' } } }; function settingsPath() { return path.join(getConfigDir(), 'settings.json'); } function getSettings() { try { const p = settingsPath(); if (!fs.existsSync(p)) return JSON.parse(JSON.stringify(DEFAULTS)); const data = JSON.parse(fs.readFileSync(p, 'utf8')); // 深合并默认值,防旧文件缺字段 return { ocr: { provider: (data.ocr && data.ocr.provider) === 'online' ? 'online' : 'local', online: { url: (data.ocr && data.ocr.online && data.ocr.online.url) || DEFAULTS.ocr.online.url, apiKey: (data.ocr && data.ocr.online && data.ocr.online.apiKey) || '', model: (data.ocr && data.ocr.online && data.ocr.online.model) || DEFAULTS.ocr.online.model } }, telemetry: data.telemetry !== false, imagegen: { provider: (data.imagegen && data.imagegen.provider) === 'custom' ? 'custom' : 'newapi', newapi: { url: (data.imagegen && data.imagegen.newapi && data.imagegen.newapi.url) || DEFAULTS.imagegen.newapi.url, model: (data.imagegen && data.imagegen.newapi && data.imagegen.newapi.model) || DEFAULTS.imagegen.newapi.model, apiKey: (data.imagegen && data.imagegen.newapi && data.imagegen.newapi.apiKey) || '' }, custom: { url: (data.imagegen && data.imagegen.custom && data.imagegen.custom.url) || '', apiKey: (data.imagegen && data.imagegen.custom && data.imagegen.custom.apiKey) || '', model: (data.imagegen && data.imagegen.custom && data.imagegen.custom.model) || '' } }, videoai: { provider: (data.videoai && data.videoai.provider) === 'custom' ? 'custom' : 'inherit', custom: { url: (data.videoai && data.videoai.custom && data.videoai.custom.url) || '', model: (data.videoai && data.videoai.custom && data.videoai.custom.model) || '', apiKey: (data.videoai && data.videoai.custom && data.videoai.custom.apiKey) || '' } } }; } catch (e) { console.error('[Settings] 读取失败,使用默认:', e.message); return JSON.parse(JSON.stringify(DEFAULTS)); } } function saveSettings(settings) { const p = settingsPath(); const merged = getSettings(); if (settings.ocr) { merged.ocr.provider = settings.ocr.provider === 'online' ? 'online' : 'local'; if (settings.ocr.online) { // apiKey 传空/掩码 = 保持原值 const incomingKey = settings.ocr.online.apiKey; if (typeof incomingKey === 'string' && incomingKey.trim() && !incomingKey.includes('***')) { merged.ocr.online.apiKey = incomingKey.trim(); } if (typeof settings.ocr.online.url === 'string') merged.ocr.online.url = settings.ocr.online.url.trim(); if (typeof settings.ocr.online.model === 'string') merged.ocr.online.model = settings.ocr.online.model.trim(); } } if (typeof settings.telemetry === 'boolean') merged.telemetry = settings.telemetry; if (settings.imagegen) { const ig = settings.imagegen; if (ig.provider === 'custom' || ig.provider === 'newapi') merged.imagegen.provider = ig.provider; if (ig.newapi) { if (typeof ig.newapi.url === 'string') merged.imagegen.newapi.url = ig.newapi.url.trim(); if (typeof ig.newapi.model === 'string') merged.imagegen.newapi.model = ig.newapi.model.trim() || 'flux-schnell'; const k = ig.newapi.apiKey; // 空串/掩码 = 保持原值(与 OCR 行为一致) if (typeof k === 'string' && k.trim() && !k.includes('***')) { merged.imagegen.newapi.apiKey = k.trim(); } } if (ig.custom) { if (typeof ig.custom.url === 'string') merged.imagegen.custom.url = ig.custom.url.trim(); if (typeof ig.custom.model === 'string') merged.imagegen.custom.model = ig.custom.model.trim(); const ck = ig.custom.apiKey; if (typeof ck === 'string' && ck.trim() && !ck.includes('***')) { merged.imagegen.custom.apiKey = ck.trim(); } } } if (settings.videoai) { const va = settings.videoai; if (va.provider === 'custom' || va.provider === 'inherit') merged.videoai.provider = va.provider; if (va.custom) { if (typeof va.custom.url === 'string') merged.videoai.custom.url = va.custom.url.trim(); if (typeof va.custom.model === 'string') merged.videoai.custom.model = va.custom.model.trim(); const vk = va.custom.apiKey; // 空串/掩码 = 保持原密钥 if (typeof vk === 'string' && vk.trim() && !vk.includes('***')) { merged.videoai.custom.apiKey = vk.trim(); } } } fs.writeFileSync(p, JSON.stringify(merged, null, 2)); return merged; } /** 掩码展示用:sk-abcdef123456 -> sk-a***56 */ function maskKey(key) { if (!key) return ''; if (key.length <= 8) return '***'; return `${key.slice(0, 4)}***${key.slice(-4)}`; } /** * 解析 AI 视频使用的文本大模型配置 * - 设置-AI 视频里选了「自定义」且填了地址 → 用自定义 * - 否则复用 AI 生图配置(内置 NewAPI / 自定义提供商) * 普通文本模型(chat/completions)即可,全流程由后端写分镜、TTS 配音、合成字幕。 */ function resolveVideoGen(settings) { const s = settings || getSettings(); const va = (s && s.videoai) || {}; const ig = (s && s.imagegen) || {}; const defModel = process.env.NEWAPI_TEXT_MODEL || 'mimo-v2.6-flash'; if (va.provider === 'custom' && va.custom && va.custom.url) { return { url: String(va.custom.url).replace(/\/+$/, ''), model: va.custom.model || defModel, apiKey: va.custom.apiKey || process.env.NEWAPI_KEY || '' }; } if (ig.provider === 'custom' && ig.custom) { return { url: String(ig.custom.url || '').replace(/\/+$/, ''), model: ig.custom.model || defModel, apiKey: ig.custom.apiKey || process.env.NEWAPI_KEY || '' }; } return { url: String((ig.newapi && ig.newapi.url) || 'http://192.168.68.19:3099/v1').replace(/\/+$/, ''), model: defModel, apiKey: ((ig.newapi && ig.newapi.apiKey) || process.env.NEWAPI_KEY || '') }; } module.exports = { getSettings, saveSettings, maskKey, settingsPath, resolveVideoGen };