feat: persist settings and enrich conversation context

This commit is contained in:
jjbrunton committed 2026-08-08 18:18:28 +01:00
1 parent a36723a325
commit 2c100d72b6
84 files changed
+2248 -644

No files matched your search

+20 -1
View File
@@ -157,7 +157,20 @@ export const ConfigSchema = z.object({
export type Config = z.infer<typeof ConfigSchema>;
export function loadConfig(): Config {
export interface ConfigPersistence {
load(environment?: NodeJS.ProcessEnv): Config | null;
save(config: Config): void;
}
/**
* Loads the database-backed configuration. A legacy YAML file is imported
* exactly once when the database has not yet been seeded, which makes the
* migration safe for existing installations.
*/
export function loadConfig(store?: ConfigPersistence): Config {
const storedConfig = store?.load(process.env);
if (storedConfig) return storedConfig;
const configPath = path.resolve(process.cwd(), 'config.yaml');
if (!fs.existsSync(configPath)) {
@@ -173,6 +186,11 @@ export function loadConfig(): Config {
}
rawConfig.openrouter.apiKey = process.env.OPENROUTER_API_KEY || rawConfig.openrouter.apiKey;
if (!rawConfig.database) rawConfig.database = {};
// The database must be located before application settings can be read, so
// its path is an environment-owned bootstrap value rather than app config.
rawConfig.database.path = process.env.DATABASE_PATH ?? './data/reddit-bot.db';
if (process.env.WEB_DASHBOARD_TOKEN !== undefined) {
if (!rawConfig.web) rawConfig.web = {};
rawConfig.web.authToken = process.env.WEB_DASHBOARD_TOKEN;
@@ -195,5 +213,6 @@ export function loadConfig(): Config {
throw new Error('WEB_DASHBOARD_TOKEN environment variable or web.authToken configuration is required when the dashboard is enabled');
}
store?.save(result.data);
return result.data;
}