e0609655ba
Fixes #255 - Add getClaudeConfigDir() utility function that respects CLAUDE_CONFIG_DIR env var - Update all hardcoded ~/.claude paths to use the new utility - Add comprehensive tests for getClaudeConfigDir() - Maintain backward compatibility with default ~/.claude when env var is not set Files updated: - src/shared/claude-config-dir.ts (new utility) - src/shared/claude-config-dir.test.ts (tests) - src/hooks/claude-code-hooks/config.ts - src/hooks/claude-code-hooks/todo.ts - src/hooks/claude-code-hooks/transcript.ts - src/features/claude-code-command-loader/loader.ts - src/features/claude-code-agent-loader/loader.ts - src/features/claude-code-skill-loader/loader.ts - src/features/claude-code-mcp-loader/loader.ts - src/tools/session-manager/constants.ts - src/tools/slashcommand/tools.ts Co-authored-by: sisyphus-dev-ai <sisyphus-dev-ai@users.noreply.github.com>
91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { existsSync, readdirSync, readFileSync } from "fs"
|
|
import { join, basename } from "path"
|
|
import type { AgentConfig } from "@opencode-ai/sdk"
|
|
import { parseFrontmatter } from "../../shared/frontmatter"
|
|
import { isMarkdownFile } from "../../shared/file-utils"
|
|
import { getClaudeConfigDir } from "../../shared"
|
|
import type { AgentScope, AgentFrontmatter, LoadedAgent } from "./types"
|
|
|
|
function parseToolsConfig(toolsStr?: string): Record<string, boolean> | undefined {
|
|
if (!toolsStr) return undefined
|
|
|
|
const tools = toolsStr.split(",").map((t) => t.trim()).filter(Boolean)
|
|
if (tools.length === 0) return undefined
|
|
|
|
const result: Record<string, boolean> = {}
|
|
for (const tool of tools) {
|
|
result[tool.toLowerCase()] = true
|
|
}
|
|
return result
|
|
}
|
|
|
|
function loadAgentsFromDir(agentsDir: string, scope: AgentScope): LoadedAgent[] {
|
|
if (!existsSync(agentsDir)) {
|
|
return []
|
|
}
|
|
|
|
const entries = readdirSync(agentsDir, { withFileTypes: true })
|
|
const agents: LoadedAgent[] = []
|
|
|
|
for (const entry of entries) {
|
|
if (!isMarkdownFile(entry)) continue
|
|
|
|
const agentPath = join(agentsDir, entry.name)
|
|
const agentName = basename(entry.name, ".md")
|
|
|
|
try {
|
|
const content = readFileSync(agentPath, "utf-8")
|
|
const { data, body } = parseFrontmatter<AgentFrontmatter>(content)
|
|
|
|
const name = data.name || agentName
|
|
const originalDescription = data.description || ""
|
|
|
|
const formattedDescription = `(${scope}) ${originalDescription}`
|
|
|
|
const config: AgentConfig = {
|
|
description: formattedDescription,
|
|
mode: "subagent",
|
|
prompt: body.trim(),
|
|
}
|
|
|
|
const toolsConfig = parseToolsConfig(data.tools)
|
|
if (toolsConfig) {
|
|
config.tools = toolsConfig
|
|
}
|
|
|
|
agents.push({
|
|
name,
|
|
path: agentPath,
|
|
config,
|
|
scope,
|
|
})
|
|
} catch {
|
|
continue
|
|
}
|
|
}
|
|
|
|
return agents
|
|
}
|
|
|
|
export function loadUserAgents(): Record<string, AgentConfig> {
|
|
const userAgentsDir = join(getClaudeConfigDir(), "agents")
|
|
const agents = loadAgentsFromDir(userAgentsDir, "user")
|
|
|
|
const result: Record<string, AgentConfig> = {}
|
|
for (const agent of agents) {
|
|
result[agent.name] = agent.config
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function loadProjectAgents(): Record<string, AgentConfig> {
|
|
const projectAgentsDir = join(process.cwd(), ".claude", "agents")
|
|
const agents = loadAgentsFromDir(projectAgentsDir, "project")
|
|
|
|
const result: Record<string, AgentConfig> = {}
|
|
for (const agent of agents) {
|
|
result[agent.name] = agent.config
|
|
}
|
|
return result
|
|
}
|