claude-flow-novice
Version:
Claude Flow Novice - Advanced orchestration platform for multi-agent AI workflows with CFN Loop architecture Includes Local RuVector Accelerator and all CFN skills for complete functionality.
101 lines (100 loc) • 3.92 kB
JavaScript
import * as fs from "fs/promises";
import * as path from "path";
import { fileURLToPath } from "url";
import Ajv from "ajv";
import * as lodash from "lodash";
// ESM-compatible __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let ConfigManager = class ConfigManager {
static _instance = null;
configPath;
schemaPath;
ajv;
constructor(){
this.configPath = path.join(process.env.HOME || "", ".claude-flow-config.json");
this.schemaPath = path.join(__dirname, "../../.claude/skills/config-management/config.json");
this.ajv = new Ajv();
}
static getInstance() {
if (!ConfigManager._instance) {
ConfigManager._instance = new ConfigManager();
}
return ConfigManager._instance;
}
async readConfig() {
try {
const configContent = await fs.readFile(this.configPath, "utf-8");
return JSON.parse(configContent);
} catch (error) {
// If config doesn't exist, create from schema
return this.resetToDefaults();
}
}
async writeConfig(config) {
const schemaContent = await fs.readFile(this.schemaPath, "utf-8");
const schema = JSON.parse(schemaContent);
const validate = this.ajv.compile(schema);
if (!validate(config)) {
throw new Error("Invalid configuration: " + this.ajv.errorsText(validate.errors));
}
await fs.writeFile(this.configPath, JSON.stringify(config, null, 2), "utf-8");
}
async getValue(keyPath) {
const config = await this.readConfig();
const value = lodash.get(config, keyPath);
if (value === undefined) {
// Check if it's a custom key path not in the schema
const customConfig = await this.readCustomConfig();
return lodash.get(customConfig, keyPath);
}
return value;
}
async readCustomConfig() {
try {
const customConfigPath = path.join(process.env.HOME || "", ".claude-flow-custom-config.json");
const customConfigContent = await fs.readFile(customConfigPath, "utf-8");
return JSON.parse(customConfigContent);
} catch (error) {
// If custom config doesn't exist or can't be read, return empty object
return {};
}
}
async set(key, value) {
const config = await this.readConfig();
// Type assertion to handle full object
if (typeof value === "object" && value !== null) {
config[key] = value;
} else {
throw new Error("Invalid configuration value");
}
await this.writeConfig(config);
}
async getAll() {
return this.readConfig();
}
async resetToDefaults() {
const schemaContent = await fs.readFile(this.schemaPath, "utf-8");
const schema = JSON.parse(schemaContent);
// Extract default values from the schema
const defaultConfig = {
redis: {
host: schema.properties.redis.properties.host.default,
port: schema.properties.redis.properties.port.default
},
agent: {
default_strategy: schema.properties.agent.properties.default_strategy.default,
max_concurrent_agents: schema.properties.agent.properties.max_concurrent_agents.default,
log_level: schema.properties.agent.properties.log_level.default
},
security: {
enabled: schema.properties.security.properties.enabled.default,
max_retry_attempts: schema.properties.security.properties.max_retry_attempts.default
}
};
await this.writeConfig(defaultConfig);
return defaultConfig;
}
};
export default ConfigManager;
//# sourceMappingURL=config-manager.js.map