UNPKG

talkshitgetdared

Version:

The truth will hurt. An open-source cursed-core engine for Truth & Dare prompts.

147 lines 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromptService = void 0; const index_1 = require("../data/index"); const index_2 = require("../utils/index"); const index_3 = require("../errors/index"); const PromptHistoryService_1 = require("./PromptHistoryService"); class PromptService { constructor(config) { this.config = config; this.dataLoader = new index_1.DataLoader(); this.historyService = new PromptHistoryService_1.PromptHistoryService(config.historySize || 50, config.enableHistory || false); } getTruth(options = {}) { return this.getPrompt('truth', options); } getDare(options = {}) { return this.getPrompt('dare', options); } getRandom(options = {}) { const type = index_2.RandomSelector.getRandomBoolean() ? 'truth' : 'dare'; return this.getPrompt(type, options); } getPrompt(type, options, respectHistory = true) { const language = options.language || this.config.defaultLanguage; const mode = options.mode || this.config.defaultMode; try { let prompts = this.dataLoader.loadPrompts(language, mode, type); if (options.difficulty) { prompts = prompts.filter((prompt) => prompt.difficulty === options.difficulty); } if (options.category) { prompts = prompts.filter((prompt) => prompt.category === options.category); } if (respectHistory && this.historyService.isEnabled()) { prompts = prompts.filter((prompt) => !this.historyService.hasPromptBeenUsed(prompt.id)); } if (prompts.length === 0) { throw new index_3.TruthOrDareError(`No prompts found matching the specified criteria for ${language} ${mode} ${type}`, 'NO_MATCHING_PROMPTS'); } const selectedPrompt = index_2.RandomSelector.getRandomElement(prompts); if (this.historyService.isEnabled()) { this.historyService.addToHistory(selectedPrompt.id); } return { prompt: selectedPrompt, type, language, mode, }; } catch (error) { return this.handleFallback(type, language, mode, error); } } handleFallback(type, requestedLanguage, requestedMode, originalError) { if (requestedLanguage !== this.config.defaultLanguage || requestedMode !== this.config.defaultMode) { try { const prompts = this.dataLoader.loadPrompts(this.config.defaultLanguage, this.config.defaultMode, type); const selectedPrompt = index_2.RandomSelector.getRandomElement(prompts); return { prompt: selectedPrompt, type, language: this.config.defaultLanguage, mode: this.config.defaultMode, }; } catch (fallbackError) { throw new index_3.TruthOrDareError(`Failed to get ${type} prompt. Original error: ${originalError instanceof Error ? originalError.message : 'Unknown error'}. Fallback error: ${fallbackError instanceof Error ? fallbackError.message : 'Unknown error'}`, 'FALLBACK_FAILED'); } } if (originalError instanceof Error) { throw originalError; } throw new index_3.TruthOrDareError('Unknown error occurred', 'UNKNOWN_ERROR'); } getAvailableLanguages() { return this.dataLoader.getAvailableLanguages(); } getAvailableModes(language) { return this.dataLoader.getAvailableModes(language); } clearCache() { this.dataLoader.clearCache(); } getCacheStats() { return this.dataLoader.getCacheStats(); } updateConfig(newConfig) { this.config = { ...this.config, ...newConfig }; if (newConfig.dataPath) { this.dataLoader = new index_1.DataLoader(); } if (newConfig.enableHistory !== undefined) { this.historyService.setEnabled(newConfig.enableHistory); } if (newConfig.historySize !== undefined) { this.historyService.setMaxSize(newConfig.historySize); } } getBatch(options) { const count = options.count; if (count < 1) { throw new index_3.TruthOrDareError('Batch count must be at least 1', 'INVALID_BATCH_COUNT'); } const ensureUnique = options.ensureUnique !== false; const language = options.language || this.config.defaultLanguage; const mode = options.mode || this.config.defaultMode; const prompts = []; const usedIds = new Set(); for (let i = 0; i < count; i++) { try { let result; let attempts = 0; const maxAttempts = 100; do { const type = options.type || (index_2.RandomSelector.getRandomBoolean() ? 'truth' : 'dare'); result = this.getPrompt(type, options, true); attempts++; if (attempts > maxAttempts) { throw new index_3.TruthOrDareError('Could not find enough unique prompts for batch operation', 'INSUFFICIENT_UNIQUE_PROMPTS'); } } while (ensureUnique && usedIds.has(result.prompt.id)); usedIds.add(result.prompt.id); prompts.push(result); } catch (error) { if (prompts.length > 0) { break; } throw error; } } return { prompts, count: prompts.length, language, mode, }; } getHistoryService() { return this.historyService; } } exports.PromptService = PromptService; //# sourceMappingURL=PromptService.js.map