UNPKG

convokit

Version:

A flexible TypeScript framework for ingesting, processing, and exporting chat/conversation data for LLM training and analysis.

88 lines 4.25 kB
import fs from 'fs/promises'; import path from 'path'; let CONFIG_FILE = 'convokit.config.json'; if (process.env.CK_CONFIG_FILE) { CONFIG_FILE = process.env.CK_CONFIG_FILE; } let _config = null; /** * Loads and validates ConvoKit configuration from a JSON file and environment variables. * @throws Error if required configuration is missing or invalid. */ export async function loadConfig() { if (_config) return _config; // Load config file if present let fileConfig = {}; try { const raw = await fs.readFile(path.resolve(process.cwd(), CONFIG_FILE), 'utf8'); fileConfig = JSON.parse(raw); } catch { // ignore missing or invalid file } // Merge env vars and file settings const env = process.env; // Helper to parse boolean env vars function parseEnvBool(varName, defaultValue) { return env[varName] === undefined ? defaultValue : env[varName] === 'true'; } // Parse TARGET_USERS env var as JSON array if provided let envTargetUsers = []; if (env.TARGET_USERS) { try { const parsed = JSON.parse(env.TARGET_USERS); if (Array.isArray(parsed)) envTargetUsers = parsed; } catch { // ignore parse errors } } const config = { inputDataDirName: fileConfig.inputDataDirName || env.CK_INPUT_DATA_DIR_NAME, outputDataDirName: fileConfig.outputDataDirName || env.CK_OUTPUT_DATA_DIR_NAME || 'output_data', targetUsers: fileConfig.targetUsers || envTargetUsers, sampleSize: Number(fileConfig.sampleSize || env.CK_SAMPLE_SIZE || 5000), systemPrompt: fileConfig.systemPrompt || env.CK_SYSTEM_PROMPT || '', minImportanceChat: Number(fileConfig.minImportanceChat || env.CK_MIN_IMPORTANCE_CHAT || 120), minImportanceMessage: Number(fileConfig.minImportanceMessage || env.CK_MIN_IMPORTANCE_MESSAGE || 100), enableDebugging: fileConfig.enableDebugging !== undefined ? fileConfig.enableDebugging : parseEnvBool('CK_ENABLE_DEBUGGING', false), enablePerformanceStats: fileConfig.enablePerformanceStats !== undefined ? fileConfig.enablePerformanceStats : parseEnvBool('CK_ENABLE_PERFORMANCE_STATS', true), shouldMergeConsecutiveMessages: fileConfig.shouldMergeConsecutiveMessages !== undefined ? fileConfig.shouldMergeConsecutiveMessages : parseEnvBool('CK_SHOULD_MERGE_CONSECUTIVE_MESSAGES', false), enableWarnings: fileConfig.enableWarnings !== undefined ? fileConfig.enableWarnings : parseEnvBool('CK_ENABLE_WARNINGS', true), anonymizeProviderConversationIds: fileConfig.anonymizeProviderConversationIds !== undefined ? fileConfig.anonymizeProviderConversationIds : parseEnvBool('CK_ANONYMIZE_PROVIDER_CONVERSATION_IDS', false), localProvidersDir: fileConfig.localProvidersDir || env.CK_LOCAL_PROVIDERS_DIR, localPluginsDir: fileConfig.localPluginsDir || env.CK_LOCAL_PLUGINS_DIR, }; // Validate required fields const missing = []; if (!config.inputDataDirName) missing.push('inputDataDirName or environment variable INPUT_DATA_DIR_NAME'); if (!Array.isArray(config.targetUsers) || config.targetUsers.length === 0) missing.push('targetUsers or environment variable TARGET_USERS'); if (!config.sampleSize || isNaN(config.sampleSize)) missing.push('sampleSize or environment variable SAMPLE_SIZE'); if (!config.systemPrompt) missing.push('systemPrompt or environment variable SYSTEM_PROMPT'); if (isNaN(config.minImportanceChat)) missing.push('minImportanceChat must be a number'); if (isNaN(config.minImportanceMessage)) missing.push('minImportanceMessage must be a number'); if (missing.length) { throw new Error(`Missing or invalid configuration: ${missing.join(', ')}`); } _config = config; return _config; } /** * Retrieves the loaded ConvoKit configuration synchronously. * @throws Error if configuration is not loaded yet. */ export function getConfig() { if (!_config) { throw new Error('ConvoKit configuration has not been loaded. Call loadConfig() first.'); } return _config; } //# sourceMappingURL=ConvoKitConfig.js.map