UNPKG

@nanocollective/nanocoder

Version:

A local-first CLI coding agent that brings the power of agentic coding tools like Claude Code and Gemini CLI to local models or controlled APIs like OpenRouter

84 lines 2.5 kB
import { getLogger } from '../utils/logging/index.js'; import { clearModelCache, fetchModels, isModelsCached } from './model-fetcher.js'; export class ModelDatabase { static instance; cachedModels = null; fetchPromise = null; static getInstance() { if (!ModelDatabase.instance) { ModelDatabase.instance = new ModelDatabase(); } return ModelDatabase.instance; } /** * Get all models - returns cached data if available, empty array otherwise. * Use getAllModelsAsync() for fetching fresh data. */ getAllModels() { if (this.cachedModels) { return this.cachedModels; } // Trigger background fetch if not already in progress if (!this.fetchPromise) { void this.refreshModelsAsync(); } // Return empty array - caller should use async method for fresh data return []; } /** * Get all models asynchronously - fetches from APIs. */ async getAllModelsAsync() { // If we have cached models and they're still fresh, return them if (this.cachedModels && isModelsCached()) { return this.cachedModels; } // If fetch is in progress, wait for it if (this.fetchPromise) { return this.fetchPromise; } // Start fetch and wait return this.refreshModels(); } /** * Force refresh models from APIs */ async refreshModels() { this.fetchPromise = fetchModels(); try { this.cachedModels = await this.fetchPromise; return this.cachedModels; } finally { this.fetchPromise = null; } } /** * Internal async refresh that doesn't block */ async refreshModelsAsync() { if (this.fetchPromise) return; this.fetchPromise = fetchModels(); try { this.cachedModels = await this.fetchPromise; } catch (error) { const logger = getLogger(); logger.debug('Failed to fetch models', { error }); } finally { this.fetchPromise = null; } } /** * Clear cache to force fresh fetch on next call */ clearCache() { this.cachedModels = null; this.fetchPromise = null; clearModelCache(); } } export const modelDatabase = ModelDatabase.getInstance(); //# sourceMappingURL=model-database.js.map