UNPKG

@sconedev/ai_toolkit

Version:

Simplify AI integration in web apps with local and offline model support

128 lines (127 loc) 4.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getFeatureRegistry = getFeatureRegistry; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); class FeatureRegistryImpl { constructor() { this.features = new Map(); this.installedFeatures = new Set(); // Use different storage methods for Node.js and browser environments if (typeof window === 'undefined') { try { this.configPath = path_1.default.join(process.cwd(), '.aitoolkit-features'); // Load installed features if (fs_1.default.existsSync(this.configPath)) { const installedFeatureIds = fs_1.default.readFileSync(this.configPath, 'utf8') .split('\n') .filter(Boolean); installedFeatureIds.forEach(id => this.installedFeatures.add(id)); } } catch (error) { // Fail gracefully if file operations aren't available this.configPath = ''; console.warn('Feature registry could not access filesystem. Feature tracking disabled.'); } } else { // In browser, use localStorage for feature tracking this.configPath = ''; try { const stored = localStorage.getItem('aitoolkit-features'); if (stored) { const installedFeatureIds = JSON.parse(stored); installedFeatureIds.forEach((id) => this.installedFeatures.add(id)); } } catch (error) { console.warn('Feature registry could not access localStorage. Feature tracking disabled.'); } } // Register built-in features this.registerBuiltInFeatures(); } registerFeature(feature) { this.features.set(feature.id, feature); } getFeature(id) { return this.features.get(id); } getAllFeatures() { return Array.from(this.features.values()); } getInstalledFeatures() { return Array.from(this.installedFeatures); } setInstalledFeature(id) { this.installedFeatures.add(id); this.saveInstalledFeatures(); } removeInstalledFeature(id) { this.installedFeatures.delete(id); this.saveInstalledFeatures(); } saveInstalledFeatures() { const installedFeatureIds = Array.from(this.installedFeatures); if (typeof window === 'undefined' && this.configPath) { try { fs_1.default.writeFileSync(this.configPath, installedFeatureIds.join('\n'), 'utf8'); } catch (error) { console.warn('Could not save installed features to disk.'); } } else if (typeof window !== 'undefined') { try { localStorage.setItem('aitoolkit-features', JSON.stringify(installedFeatureIds)); } catch (error) { console.warn('Could not save installed features to localStorage.'); } } } registerBuiltInFeatures() { // Register core features this.registerFeature({ id: 'core', name: 'Core AI Functionality', description: 'Basic AI chat and text generation capabilities', dependencies: ['openai'], peerDependencies: [], isNodeOnly: false, isBrowserOnly: false }); this.registerFeature({ id: 'pdf-processing', name: 'PDF Processing', description: 'Extract text from PDFs and summarize content with AI', dependencies: [], peerDependencies: ['pdf-parse', 'pdfjs-dist'], isNodeOnly: false, isBrowserOnly: false }); this.registerFeature({ id: 'image-generation', name: 'Image Generation', description: 'Generate images using AI', dependencies: [], peerDependencies: [], isNodeOnly: false, isBrowserOnly: false }); // Install core by default this.setInstalledFeature('core'); } } // Singleton instance let registry; function getFeatureRegistry() { if (!registry) { registry = new FeatureRegistryImpl(); } return registry; }