UNPKG

@zhanghongping/json-sage-workflow-cli

Version:

An intelligent JSON processing workflow system with improved error handling and configuration

85 lines (84 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateSchema = generateSchema; exports.saveSchema = saveSchema; exports.loadSchema = loadSchema; const error_handler_1 = require("../utils/error-handler"); const DEFAULT_CONFIG = { retry: { maxRetries: 3, initialDelay: 1000, maxDelay: 10000, shouldRetry: (error) => true }, errorMessages: { jsonParse: 'Error parsing JSON data. Please ensure the input is valid JSON.', commandError: 'Error executing command. Please check your input and try again.', fileNotFound: 'File not found. Please check the file path and try again.', invalidConfig: 'Invalid configuration. Please check your config file.', networkError: 'Network error occurred. Please check your connection and try again.', timeoutError: 'Operation timed out. Please try again.', validationError: 'Validation error occurred. Please check your input.', optimizationError: 'Error during optimization process.', unknownError: 'An unexpected error occurred. Please try again.' } }; async function generateSchema(description, options) { try { const result = await (0, error_handler_1.retryWithBackoff)(async () => { const parseResult = (0, error_handler_1.safeJsonParse)(description); if (!parseResult.success) { throw parseResult.error; } const executeResult = await (0, error_handler_1.safeExecute)(async () => { // TODO: Implement schema generation logic return { schema: { type: "object", properties: {} } }; }); if (!executeResult.success || !executeResult.data) { throw executeResult.error || new Error('Failed to generate schema'); } return executeResult.data; }, DEFAULT_CONFIG.retry); if (!result || !result.schema) { throw new Error('Invalid schema generated'); } return result.schema; } catch (error) { throw new Error(`Failed to generate schema: ${error.message}`); } } async function saveSchema(schema, filePath, format = true) { try { const absolutePath = resolve(filePath); const content = JSON.stringify(schema, null, format ? 2 : 0); await (0, error_handler_1.safeExecute)(async () => { writeFileSync(absolutePath, content, 'utf8'); }, DEFAULT_CONFIG.errorMessages.fileNotFound); } catch (error) { throw new Error(`Failed to save schema: ${error.message}`); } } async function loadSchema(filePath) { try { const absolutePath = resolve(filePath); const content = await (0, error_handler_1.safeExecute)(async () => readFileSync(absolutePath, 'utf8'), DEFAULT_CONFIG.errorMessages.fileNotFound); if (!content.success || !content.data) { throw new Error(DEFAULT_CONFIG.errorMessages.fileNotFound); } const parseResult = (0, error_handler_1.safeJsonParse)(content.data); if (!parseResult.success) { throw parseResult.error; } return parseResult.data; } catch (error) { throw new Error(`Failed to load schema: ${error.message}`); } }