UNPKG

seraph-agent

Version:

An extremely lightweight, SRE autonomous AI agent for seamless integration with common observability tasks.

138 lines (137 loc) 5.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.loadConfig = loadConfig; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const defaultConfig = { port: 8080, workers: 4, apiKey: process.env.SERAPH_API_KEY || null, serverApiKey: process.env.SERVER_API_KEY || null, llm: { provider: 'gemini', }, alertManager: { url: 'http://localhost:9093/api/v2/alerts' // Default for Prometheus Alertmanager }, preFilters: [], rateLimit: { window: 60000, // 1 minute maxRequests: 100, }, recentLogsMaxSizeMb: 10, // 10MB }; async function loadConfig() { const configPath = path.join(process.cwd(), 'seraph.config.json'); let userConfig = {}; try { await fs.promises.access(configPath); userConfig = JSON.parse(await fs.promises.readFile(configPath, 'utf-8')); } catch (error) { if (error.code !== 'ENOENT') { console.error("Error reading or parsing 'seraph.config.json'.", error); } } const config = { ...defaultConfig, ...userConfig, llm: { ...defaultConfig.llm, ...userConfig.llm }, alertManager: { ...defaultConfig.alertManager, ...userConfig.alertManager }, rateLimit: { ...defaultConfig.rateLimit, ...userConfig.rateLimit }, }; // Set API key from environment variables if not already set if (!config.apiKey) { switch (config.llm?.provider) { case 'gemini': config.apiKey = process.env.GEMINI_API_KEY || null; break; case 'anthropic': config.apiKey = process.env.ANTHROPIC_API_KEY || null; break; case 'openai': config.apiKey = process.env.OPENAI_API_KEY || null; break; default: config.apiKey = process.env.SERAPH_API_KEY || null; break; } } // Validate configuration values if (typeof config.port !== 'number' || config.port <= 0) { throw new Error('Invalid configuration: port must be a positive number.'); } if (typeof config.workers !== 'number' || config.workers <= 0) { throw new Error('Invalid configuration: workers must be a positive number.'); } if (userConfig.apiKey !== undefined && typeof userConfig.apiKey !== 'string' && userConfig.apiKey !== null) { throw new Error('Invalid configuration: apiKey must be a string or null.'); } if (userConfig.serverApiKey !== undefined && typeof userConfig.serverApiKey !== 'string' && userConfig.serverApiKey !== null) { throw new Error('Invalid configuration: serverApiKey must be a string or null.'); } if (config.llm) { const validProviders = ['gemini', 'anthropic', 'openai']; if (!validProviders.includes(config.llm.provider)) { throw new Error(`Invalid configuration: llm.provider must be one of ${validProviders.join(', ')}.`); } if (userConfig.llm?.model !== undefined && typeof userConfig.llm.model !== 'string') { throw new Error('Invalid configuration: llm.model must be a string.'); } } if (config.alertManager) { if (userConfig.alertManager?.url !== undefined && typeof userConfig.alertManager.url !== 'string') { throw new Error('Invalid configuration: alertManager.url must be a string.'); } } if (config.preFilters) { if (!Array.isArray(config.preFilters) || !config.preFilters.every(f => typeof f === 'string')) { throw new Error('Invalid configuration: preFilters must be an array of strings.'); } } if (config.rateLimit) { if (typeof config.rateLimit.window !== 'number' || config.rateLimit.window <= 0) { throw new Error('Invalid configuration: rateLimit.window must be a positive number.'); } if (typeof config.rateLimit.maxRequests !== 'number' || config.rateLimit.maxRequests <= 0) { throw new Error('Invalid configuration: rateLimit.maxRequests must be a positive number.'); } } if (userConfig.recentLogsMaxSizeMb !== undefined && (typeof userConfig.recentLogsMaxSizeMb !== 'number' || userConfig.recentLogsMaxSizeMb <= 0)) { throw new Error('Invalid configuration: recentLogsMaxSizeMb must be a positive number.'); } return config; }