UNPKG

@summarisation/summarise

Version:

cli for summariser

159 lines (158 loc) 6.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateConfig = exports.validationTransform = exports.validateReport = exports.validateTika = exports.validateSchema = exports.validateNonfunctionals = exports.validateAi = exports.validateDirectory = exports.configToThrottling = exports.abortIfDirectoryDoesNotExist = void 0; const node_fs_1 = __importDefault(require("node:fs")); async function abortIfDirectoryDoesNotExist(dir, message) { try { await node_fs_1.default.promises.access(dir); } catch (e) { console.error(message); process.exit(2); } } exports.abortIfDirectoryDoesNotExist = abortIfDirectoryDoesNotExist; function configToThrottling(config) { return { tokensPer100ms: config.throttlingPerHour / 360000, max: config.throttlingPerHour, current: config.throttlingPerHour }; } exports.configToThrottling = configToThrottling; function validateNeeded(s, name, type = 'string') { if (!s) return [`${name} is not defined`]; if (typeof s !== type) return [`${name} is not a ${type}`]; return []; } function validateDirectory(directories) { if (!directories || typeof directories !== 'object') return ['Directories is not an object']; const errors = []; errors.push(...validateNeeded(directories.inputs, 'directories.inputs')); errors.push(...validateNeeded(directories.tika, 'directories.tika')); errors.push(...validateNeeded(directories.text, 'directories.text')); errors.push(...validateNeeded(directories.summary, 'directories.summary')); return errors; } exports.validateDirectory = validateDirectory; function validateAi(ai) { if (!ai || typeof ai !== 'object') return ['ai is not an object']; const errors = []; errors.push(...validateNeeded(ai.type, 'ai.type')); if (errors.length === 0 && ai.type !== 'openai') return ['Invalid AI type. Currently only openai allowed']; errors.push(...validateNeeded(ai.url, 'ai.url')); errors.push(...validateNeeded(ai.model, 'ai.model')); errors.push(...validateNeeded(ai.token, 'ai.token')); return errors; } exports.validateAi = validateAi; function validateNonfunctionals(nonfunctionals) { if (!nonfunctionals || typeof nonfunctionals !== 'object') return ['Nonfunctionals is not an object']; const errors = []; errors.push(...validateNeeded(nonfunctionals.throttlingPerHour, 'nonfunctionals.throttlingPerHour', 'number')); errors.push(...validateNeeded(nonfunctionals.concurrent, 'nonfunctionals.concurrent', 'number')); errors.push(...validateRetry(nonfunctionals.retry)); return errors; } exports.validateNonfunctionals = validateNonfunctionals; function validateRetry(retry) { if (retry === undefined) return []; //it's OK not to have one if (typeof retry !== 'object') return ['retry is not an object']; const errors = []; errors.push(...validateNeeded(retry.initialInterval, 'retry.initialInterval', 'number')); errors.push(...validateNeeded(retry.maximumInterval, 'retry.maximumInterval', 'number')); errors.push(...validateNeeded(retry.maximumAttempts, 'retry.maximumAttempts', 'number')); return errors; } function validateSchema(schema, name) { if (!schema || typeof schema !== 'object') return [`${name} is not an object`]; const errors = []; errors.push(...validateNeeded(schema.type, `${name}.type`)); if (errors.length === 0 && schema.type !== 'inline') return [`${name}.type (${schema.type}) is invalid.Currently only inline allowed`]; if (schema.value === undefined) errors.push(`${name}.value is not defined`); return errors; } exports.validateSchema = validateSchema; function validateTika(tika) { if (!tika || typeof tika !== 'object') return ['Tika is not an object']; const errors = []; errors.push(...validateNeeded(tika.type, 'tika.type')); if (errors.length === 0 && tika.type !== 'jar') return ['Invalid tika type. Currently only jar allowed']; errors.push(...validateNeeded(tika.jar, 'tika.jar')); return errors; } exports.validateTika = validateTika; function validateReport(report) { if (!report || typeof report !== 'object') return ['Report is not an object']; const errors = []; if (report.fields === undefined) return ['report.fields is not defined']; if (typeof report.fields !== 'object') return ['report.fields is not an object']; if (report.categories === undefined) return ['report.categories is not defined']; if (!Array.isArray(report.categories)) return ['report.categories is not an array']; for (const [key, value] of Object.entries(report.fields)) { if (typeof value !== 'object') return [`report.fields.${key} is not an object`]; if (value.type === undefined) return [`report.fields.${key}.type is not defined`]; if (value.type !== 'enum' && value.type !== 'number') return [`report.fields.${key}.type is not valid`]; if (value.type === 'enum' && value.enum === undefined) return [`report.fields.${key}.enum is not defined`]; if (value.type === 'enum' && !Array.isArray(value.enum)) return [`report.fields.${key}.enum is not an array`]; } return errors; } exports.validateReport = validateReport; function validationTransform(tx) { if (!tx || typeof tx !== 'object') return ['transform is not an object']; const errors = []; if (tx.type === undefined) return ['transform.type is not defined']; const legal = ['onePerFile', 'onePerPage']; if (!legal.includes(tx.type)) return [`transform.type (${tx.type}) is not valid. Valid values are ${legal.join(', ')}`]; errors.push(...validateSchema(tx.schema, 'transform.schema')); errors.push(...validateNeeded(tx.prompt, 'transform.prompt')); return errors; } exports.validationTransform = validationTransform; const validateConfig = (s) => { const errors = []; if (typeof s !== 'object') { errors.push('Config is not an object'); return errors; } errors.push(...validateDirectory(s.directories)); errors.push(...validateAi(s.ai)); errors.push(...validateTika(s.tika)); errors.push(...validateNonfunctionals(s.nonfunctionals)); errors.push(...validateReport(s.report)); errors.push(...validationTransform(s.transform)); if (errors.length > 0) return errors; return s; }; exports.validateConfig = validateConfig;