fix(agent-loader): load opencode global agents from both config roots

This commit is contained in:
jollyxenon
2026-05-09 00:26:45 +08:00
parent 71025daeba
commit 850dc3a4cf
2 changed files with 50 additions and 7 deletions
@@ -55,12 +55,26 @@ const NO_FRONTMATTER_AGENT = `Just a prompt with no frontmatter.`;
describe("claude-code-agent-loader", () => {
const dirs: string[] = [];
const originalEnv = {
OPENCODE_CONFIG_DIR: process.env.OPENCODE_CONFIG_DIR,
XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME,
}
afterEach(() => {
for (const dir of dirs) {
rmSync(dir, { recursive: true, force: true });
}
dirs.length = 0;
if (originalEnv.OPENCODE_CONFIG_DIR === undefined) {
delete process.env.OPENCODE_CONFIG_DIR
} else {
process.env.OPENCODE_CONFIG_DIR = originalEnv.OPENCODE_CONFIG_DIR
}
if (originalEnv.XDG_CONFIG_HOME === undefined) {
delete process.env.XDG_CONFIG_HOME
} else {
process.env.XDG_CONFIG_HOME = originalEnv.XDG_CONFIG_HOME
}
});
function trackDir(dir: string): string {
@@ -204,6 +218,29 @@ describe("claude-code-agent-loader", () => {
const result = loadOpencodeGlobalAgents()
expect(result).toEqual({})
})
test("loads agents from both the custom and default opencode config directories", () => {
const root = trackDir(mkdtempSync(join(tmpdir(), "agent-loader-opencode-global-")))
const defaultAgentsDir = join(root, "xdg", "opencode", "agents")
const customAgentsDir = join(root, "custom-opencode", "agents")
mkdirSync(defaultAgentsDir, { recursive: true })
mkdirSync(customAgentsDir, { recursive: true })
writeFileSync(join(defaultAgentsDir, "default-agent.md"), BASIC_AGENT, "utf-8")
writeFileSync(
join(customAgentsDir, "custom-agent.md"),
`---\nname: custom-agent\ndescription: Custom agent\n---\nFrom custom config.`,
"utf-8",
)
process.env.XDG_CONFIG_HOME = join(root, "xdg")
process.env.OPENCODE_CONFIG_DIR = join(root, "custom-opencode")
const result = loadOpencodeGlobalAgents()
expect(Object.keys(result).sort()).toEqual(["custom-agent", "test-agent"])
})
})
describe("tools parsing", () => {