Initial
This commit is contained in:
commit
c552956516
23 files changed
+5193
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { z } from 'zod';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import YAML from 'yaml';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
export const ConfigSchema = z.object({
|
||||
reddit: z.object({
|
||||
subreddits: z.array(z.string()).min(1),
|
||||
pollIntervalMs: z.number().default(120000),
|
||||
userAgent: z.string().default('RedditBot/1.0'),
|
||||
targetGenders: z.array(z.enum(['male', 'female', 'other', 'unknown'])).default(['female']),
|
||||
}),
|
||||
openrouter: z.object({
|
||||
apiKey: z.string(),
|
||||
model: z.string().default('google/gemini-3-flash-preview'),
|
||||
imageAnalysisPrompt: z.string(),
|
||||
messageGenerationPrompt: z.string(),
|
||||
}),
|
||||
rateLimit: z.object({
|
||||
minDelayBetweenMessagesMs: z.number().default(300000),
|
||||
maxMessagesPerHour: z.number().default(8),
|
||||
jitterMs: z.number().default(60000),
|
||||
}),
|
||||
browser: z.object({
|
||||
sessionDir: z.string().default('./data/browser-session'),
|
||||
headless: z.boolean().default(false),
|
||||
dryRun: z.boolean().default(false),
|
||||
}),
|
||||
database: z.object({
|
||||
path: z.string().default('./data/reddit-bot.db'),
|
||||
}),
|
||||
web: z.object({
|
||||
enabled: z.boolean().default(true),
|
||||
port: z.number().default(3000),
|
||||
}),
|
||||
});
|
||||
|
||||
export type Config = z.infer<typeof ConfigSchema>;
|
||||
|
||||
export function loadConfig(): Config {
|
||||
const configPath = path.resolve(process.cwd(), 'config.yaml');
|
||||
|
||||
if (!fs.existsSync(configPath)) {
|
||||
throw new Error(`Config file not found at ${configPath}`);
|
||||
}
|
||||
|
||||
const configFile = fs.readFileSync(configPath, 'utf-8');
|
||||
const rawConfig = YAML.parse(configFile);
|
||||
|
||||
// Inject API key from environment
|
||||
if (!rawConfig.openrouter) {
|
||||
rawConfig.openrouter = {};
|
||||
}
|
||||
rawConfig.openrouter.apiKey = process.env.OPENROUTER_API_KEY || rawConfig.openrouter.apiKey;
|
||||
|
||||
if (!rawConfig.openrouter.apiKey) {
|
||||
throw new Error('OPENROUTER_API_KEY environment variable is required');
|
||||
}
|
||||
|
||||
const result = ConfigSchema.safeParse(rawConfig);
|
||||
|
||||
if (!result.success) {
|
||||
const errors = result.error.issues
|
||||
.map((e) => ` - ${String(e.path.join('.'))}: ${e.message}`)
|
||||
.join('\n');
|
||||
throw new Error(`Invalid configuration:\n${errors}`);
|
||||
}
|
||||
|
||||
return result.data;
|
||||
}
|
||||
Reference in new issue
Block a user