fix: load config from user-level ~/.config/opencode/oh-my-opencode.json (#17)

This commit is contained in:
Nguyen Quang Huy
2025-12-12 06:29:32 +07:00
committed by GitHub
parent d34154bc68
commit e4036185f0

View File

@@ -41,14 +41,9 @@ import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig } from "./config";
import { log } from "./shared/logger"; import { log } from "./shared/logger";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import * as os from "os";
function loadPluginConfig(directory: string): OhMyOpenCodeConfig { function loadConfigFromPath(configPath: string): OhMyOpenCodeConfig | null {
const configPaths = [
path.join(directory, "oh-my-opencode.json"),
path.join(directory, ".oh-my-opencode.json"),
];
for (const configPath of configPaths) {
try { try {
if (fs.existsSync(configPath)) { if (fs.existsSync(configPath)) {
const content = fs.readFileSync(configPath, "utf-8"); const content = fs.readFileSync(configPath, "utf-8");
@@ -57,17 +52,66 @@ function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
if (!result.success) { if (!result.success) {
log(`Config validation error in ${configPath}:`, result.error.issues); log(`Config validation error in ${configPath}:`, result.error.issues);
return {}; return null;
} }
log(`Config loaded from ${configPath}`, { agents: result.data.agents });
return result.data; return result.data;
} }
} catch { } catch (err) {
// Ignore parse errors, use defaults log(`Error loading config from ${configPath}:`, err);
}
return null;
}
function mergeConfigs(base: OhMyOpenCodeConfig, override: OhMyOpenCodeConfig): OhMyOpenCodeConfig {
return {
...base,
...override,
agents: override.agents !== undefined
? { ...(base.agents ?? {}), ...override.agents }
: base.agents,
disabled_agents: [
...new Set([...(base.disabled_agents ?? []), ...(override.disabled_agents ?? [])])
],
disabled_mcps: [
...new Set([...(base.disabled_mcps ?? []), ...(override.disabled_mcps ?? [])])
],
};
}
function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
// User-level config paths
const userConfigPaths = [
path.join(os.homedir(), ".config", "opencode", "oh-my-opencode.json"),
];
// Project-level config paths (higher precedence)
const projectConfigPaths = [
path.join(directory, ".opencode", "oh-my-opencode.json"),
];
// Load user config first
let config: OhMyOpenCodeConfig = {};
for (const configPath of userConfigPaths) {
const userConfig = loadConfigFromPath(configPath);
if (userConfig) {
config = userConfig;
break;
} }
} }
return {}; // Override with project config
for (const configPath of projectConfigPaths) {
const projectConfig = loadConfigFromPath(configPath);
if (projectConfig) {
config = mergeConfigs(config, projectConfig);
break;
}
}
log("Final merged config", { agents: config.agents, disabled_agents: config.disabled_agents, disabled_mcps: config.disabled_mcps });
return config;
} }
const OhMyOpenCodePlugin: Plugin = async (ctx) => { const OhMyOpenCodePlugin: Plugin = async (ctx) => {