Skip to content

Commit e403618

Browse files
fix: load config from user-level ~/.config/opencode/oh-my-opencode.json (#17)
1 parent d34154b commit e403618

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

src/index.ts

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,77 @@ import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig } from "./config";
4141
import { log } from "./shared/logger";
4242
import * as fs from "fs";
4343
import * as path from "path";
44+
import * as os from "os";
45+
46+
function loadConfigFromPath(configPath: string): OhMyOpenCodeConfig | null {
47+
try {
48+
if (fs.existsSync(configPath)) {
49+
const content = fs.readFileSync(configPath, "utf-8");
50+
const rawConfig = JSON.parse(content);
51+
const result = OhMyOpenCodeConfigSchema.safeParse(rawConfig);
52+
53+
if (!result.success) {
54+
log(`Config validation error in ${configPath}:`, result.error.issues);
55+
return null;
56+
}
57+
58+
log(`Config loaded from ${configPath}`, { agents: result.data.agents });
59+
return result.data;
60+
}
61+
} catch (err) {
62+
log(`Error loading config from ${configPath}:`, err);
63+
}
64+
return null;
65+
}
66+
67+
function mergeConfigs(base: OhMyOpenCodeConfig, override: OhMyOpenCodeConfig): OhMyOpenCodeConfig {
68+
return {
69+
...base,
70+
...override,
71+
agents: override.agents !== undefined
72+
? { ...(base.agents ?? {}), ...override.agents }
73+
: base.agents,
74+
disabled_agents: [
75+
...new Set([...(base.disabled_agents ?? []), ...(override.disabled_agents ?? [])])
76+
],
77+
disabled_mcps: [
78+
...new Set([...(base.disabled_mcps ?? []), ...(override.disabled_mcps ?? [])])
79+
],
80+
};
81+
}
4482

4583
function loadPluginConfig(directory: string): OhMyOpenCodeConfig {
46-
const configPaths = [
47-
path.join(directory, "oh-my-opencode.json"),
48-
path.join(directory, ".oh-my-opencode.json"),
84+
// User-level config paths
85+
const userConfigPaths = [
86+
path.join(os.homedir(), ".config", "opencode", "oh-my-opencode.json"),
4987
];
5088

51-
for (const configPath of configPaths) {
52-
try {
53-
if (fs.existsSync(configPath)) {
54-
const content = fs.readFileSync(configPath, "utf-8");
55-
const rawConfig = JSON.parse(content);
56-
const result = OhMyOpenCodeConfigSchema.safeParse(rawConfig);
89+
// Project-level config paths (higher precedence)
90+
const projectConfigPaths = [
91+
path.join(directory, ".opencode", "oh-my-opencode.json"),
92+
];
5793

58-
if (!result.success) {
59-
log(`Config validation error in ${configPath}:`, result.error.issues);
60-
return {};
61-
}
94+
// Load user config first
95+
let config: OhMyOpenCodeConfig = {};
96+
for (const configPath of userConfigPaths) {
97+
const userConfig = loadConfigFromPath(configPath);
98+
if (userConfig) {
99+
config = userConfig;
100+
break;
101+
}
102+
}
62103

63-
return result.data;
64-
}
65-
} catch {
66-
// Ignore parse errors, use defaults
104+
// Override with project config
105+
for (const configPath of projectConfigPaths) {
106+
const projectConfig = loadConfigFromPath(configPath);
107+
if (projectConfig) {
108+
config = mergeConfigs(config, projectConfig);
109+
break;
67110
}
68111
}
69112

70-
return {};
113+
log("Final merged config", { agents: config.agents, disabled_agents: config.disabled_agents, disabled_mcps: config.disabled_mcps });
114+
return config;
71115
}
72116

73117
const OhMyOpenCodePlugin: Plugin = async (ctx) => {

0 commit comments

Comments
 (0)