| |
| |
| |
| |
|
|
| import { readFileSync, existsSync } from 'fs'; |
| import { writeJsonAtomic } from '../fs-atomic.js'; |
| import { join } from 'path'; |
| import { config, log } from '../config.js'; |
|
|
| const ACCESS_FILE = join(config.dataDir, 'model-access.json'); |
|
|
| |
| const _config = { |
| mode: 'all', |
| list: [], |
| }; |
|
|
| |
| try { |
| if (existsSync(ACCESS_FILE)) { |
| Object.assign(_config, JSON.parse(readFileSync(ACCESS_FILE, 'utf-8'))); |
| } |
| } catch (e) { |
| log.error('Failed to load model-access.json:', e.message); |
| } |
|
|
| function save() { |
| try { |
| writeJsonAtomic(ACCESS_FILE, _config); |
| } catch (e) { |
| log.error('Failed to save model-access.json:', e.message); |
| } |
| } |
|
|
| export function getModelAccessConfig() { |
| return { ..._config }; |
| } |
|
|
| export function setModelAccessMode(mode) { |
| if (!['all', 'allowlist', 'blocklist'].includes(mode)) return; |
| _config.mode = mode; |
| save(); |
| } |
|
|
| export function setModelAccessList(list) { |
| _config.list = Array.isArray(list) ? list : []; |
| save(); |
| } |
|
|
| export function addModelToList(modelId) { |
| if (!_config.list.includes(modelId)) { |
| _config.list.push(modelId); |
| save(); |
| } |
| } |
|
|
| export function removeModelFromList(modelId) { |
| _config.list = _config.list.filter(m => m !== modelId); |
| save(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function siblingsForAllowlist(modelId) { |
| const sibs = []; |
| if (modelId.endsWith('-thinking')) { |
| sibs.push(modelId.slice(0, -'-thinking'.length)); |
| } else { |
| sibs.push(modelId + '-thinking'); |
| } |
| return sibs; |
| } |
|
|
| |
| |
| |
| |
| export function isModelAllowed(modelId) { |
| if (_config.mode === 'all') return { allowed: true }; |
|
|
| if (_config.mode === 'allowlist') { |
| if (_config.list.includes(modelId)) return { allowed: true }; |
| |
| |
| |
| |
| |
| |
| for (const sib of siblingsForAllowlist(modelId)) { |
| if (_config.list.includes(sib)) return { allowed: true }; |
| } |
| return { allowed: false, reason: `模型 ${modelId} 不在允許清單中` }; |
| } |
|
|
| if (_config.mode === 'blocklist') { |
| if (_config.list.includes(modelId)) { |
| return { allowed: false, reason: `模型 ${modelId} 已被封鎖` }; |
| } |
| |
| |
| |
| for (const sib of siblingsForAllowlist(modelId)) { |
| if (_config.list.includes(sib)) { |
| return { allowed: false, reason: `模型 ${modelId} 已被封鎖` }; |
| } |
| } |
| return { allowed: true }; |
| } |
|
|
| return { allowed: true }; |
| } |
|
|