yaml-config/example.ts
Lilith 195b2c6587 🔧 update imports to @lilith namespace
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 01:32:37 -08:00

121 lines
3.2 KiB
TypeScript

/**
* Example usage of @lilith/yaml-config
*
* This demonstrates how to use the package in a real application.
*/
import { createConfigLoader, z, commonSchemas } from './src/index.js';
// Define your application's configuration schema
const appConfigSchema = z.object({
app: z.object({
name: z.string(),
version: z.string(),
id: z.string(),
}),
ports: z.object({
dev: commonSchemas.port,
llamacpp: commonSchemas.port,
chatterbox: commonSchemas.port,
piper: commonSchemas.port,
redis: commonSchemas.port,
agents: z.object({
base: commonSchemas.port,
lilith: commonSchemas.port,
quinn: commonSchemas.port,
}),
}),
services: z.object({
llamacpp: z.object({
enabled: z.boolean(),
autoStart: z.boolean(),
endpoint: commonSchemas.url,
serverPath: z.string().optional(),
modelPath: z.string().optional(),
}),
chatterbox: z.object({
enabled: z.boolean(),
autoStart: z.boolean(),
endpoint: commonSchemas.url,
path: z.string(),
}),
redis: z.object({
enabled: z.boolean(),
autoStart: z.boolean(),
url: commonSchemas.url,
serverPath: z.string().optional(),
}),
}),
model: z.object({
provider: z.string(),
temperature: z.number().min(0).max(2),
topP: z.number().min(0).max(1),
maxTokens: commonSchemas.positiveInt,
}),
system: z.object({
minimizeToTray: z.boolean().default(true),
closeToTray: z.boolean().default(true),
launchOnStartup: z.boolean().default(false),
}),
});
// Create the config loader
const loader = createConfigLoader({
path: './config.yaml',
schema: appConfigSchema,
envPrefix: 'APP_',
fallbackPaths: [
'./config.example.yaml',
'~/.config/myapp/config.yaml',
],
watch: true,
onReload: (config) => {
console.log('Configuration reloaded!');
console.log('New temperature:', config.model.temperature);
},
});
// Load configuration
console.log('Loading configuration...');
const config = loader.load();
// Type-safe access
console.log('\nLoaded configuration:');
console.log('App name:', config.app.name);
console.log('Development port:', config.ports.dev);
console.log('LlamaCPP endpoint:', config.services.llamacpp.endpoint);
console.log('Model temperature:', config.model.temperature);
console.log('Close to tray:', config.system.closeToTray);
// Environment variable override example
console.log('\nEnvironment overrides:');
console.log('Set APP_MODEL_TEMPERATURE=0.9 to override temperature');
console.log('Set APP_PORTS_DEV=3000 to override dev port');
// Demonstrate config path
console.log('\nConfiguration file:', loader.getConfigPath());
// Type checking examples
type AppConfig = z.infer<typeof appConfigSchema>;
function updateModelTemperature(config: AppConfig, temp: number): void {
if (temp >= 0 && temp <= 2) {
config.model.temperature = temp;
console.log(`Temperature updated to ${temp}`);
}
}
updateModelTemperature(config, 0.8);
// Cleanup
process.on('SIGINT', () => {
console.log('\nStopping config watcher...');
loader.stopWatching();
process.exit(0);
});
console.log('\nWatching for config changes... (Press Ctrl+C to exit)');