2026-01-01 12:34:40 +01:00
|
|
|
import { existsSync, readdirSync, readFileSync, realpathSync, type Dirent } from "fs"
|
2025-12-09 15:48:13 +09:00
|
|
|
import { join, basename } from "path"
|
|
|
|
|
import { parseFrontmatter } from "../../shared/frontmatter"
|
|
|
|
|
import { sanitizeModelField } from "../../shared/model-sanitizer"
|
2025-12-13 14:23:04 +09:00
|
|
|
import { isMarkdownFile } from "../../shared/file-utils"
|
2025-12-26 23:28:33 +09:00
|
|
|
import { getClaudeConfigDir } from "../../shared"
|
2026-01-01 12:34:40 +01:00
|
|
|
import { log } from "../../shared/logger"
|
2025-12-09 15:48:13 +09:00
|
|
|
import type { CommandScope, CommandDefinition, CommandFrontmatter, LoadedCommand } from "./types"
|
|
|
|
|
|
2026-01-01 12:34:40 +01:00
|
|
|
function loadCommandsFromDir(
|
|
|
|
|
commandsDir: string,
|
|
|
|
|
scope: CommandScope,
|
|
|
|
|
visited: Set<string> = new Set(),
|
|
|
|
|
prefix: string = ""
|
|
|
|
|
): LoadedCommand[] {
|
2025-12-09 15:48:13 +09:00
|
|
|
if (!existsSync(commandsDir)) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-01 12:34:40 +01:00
|
|
|
let realPath: string
|
|
|
|
|
try {
|
|
|
|
|
realPath = realpathSync(commandsDir)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log(`Failed to resolve command directory: ${commandsDir}`, error)
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (visited.has(realPath)) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
visited.add(realPath)
|
|
|
|
|
|
|
|
|
|
let entries: Dirent[]
|
|
|
|
|
try {
|
|
|
|
|
entries = readdirSync(commandsDir, { withFileTypes: true })
|
|
|
|
|
} catch (error) {
|
|
|
|
|
log(`Failed to read command directory: ${commandsDir}`, error)
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-09 15:48:13 +09:00
|
|
|
const commands: LoadedCommand[] = []
|
|
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
2026-01-01 12:34:40 +01:00
|
|
|
if (entry.isDirectory()) {
|
|
|
|
|
if (entry.name.startsWith(".")) continue
|
|
|
|
|
const subDirPath = join(commandsDir, entry.name)
|
|
|
|
|
const subPrefix = prefix ? `${prefix}:${entry.name}` : entry.name
|
|
|
|
|
commands.push(...loadCommandsFromDir(subDirPath, scope, visited, subPrefix))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-09 15:48:13 +09:00
|
|
|
if (!isMarkdownFile(entry)) continue
|
|
|
|
|
|
|
|
|
|
const commandPath = join(commandsDir, entry.name)
|
2026-01-01 12:34:40 +01:00
|
|
|
const baseCommandName = basename(entry.name, ".md")
|
|
|
|
|
const commandName = prefix ? `${prefix}:${baseCommandName}` : baseCommandName
|
2025-12-09 15:48:13 +09:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const content = readFileSync(commandPath, "utf-8")
|
|
|
|
|
const { data, body } = parseFrontmatter<CommandFrontmatter>(content)
|
|
|
|
|
|
|
|
|
|
const wrappedTemplate = `<command-instruction>
|
|
|
|
|
${body.trim()}
|
|
|
|
|
</command-instruction>
|
|
|
|
|
|
|
|
|
|
<user-request>
|
|
|
|
|
$ARGUMENTS
|
|
|
|
|
</user-request>`
|
|
|
|
|
|
|
|
|
|
const formattedDescription = `(${scope}) ${data.description || ""}`
|
|
|
|
|
|
2025-12-14 12:24:59 +09:00
|
|
|
const isOpencodeSource = scope === "opencode" || scope === "opencode-project"
|
2025-12-09 15:48:13 +09:00
|
|
|
const definition: CommandDefinition = {
|
|
|
|
|
name: commandName,
|
|
|
|
|
description: formattedDescription,
|
|
|
|
|
template: wrappedTemplate,
|
|
|
|
|
agent: data.agent,
|
2025-12-14 12:24:59 +09:00
|
|
|
model: sanitizeModelField(data.model, isOpencodeSource ? "opencode" : "claude-code"),
|
2025-12-09 15:48:13 +09:00
|
|
|
subtask: data.subtask,
|
|
|
|
|
argumentHint: data["argument-hint"],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commands.push({
|
|
|
|
|
name: commandName,
|
|
|
|
|
path: commandPath,
|
|
|
|
|
definition,
|
|
|
|
|
scope,
|
|
|
|
|
})
|
2026-01-01 12:34:40 +01:00
|
|
|
} catch (error) {
|
|
|
|
|
log(`Failed to parse command: ${commandPath}`, error)
|
2025-12-09 15:48:13 +09:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return commands
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function commandsToRecord(commands: LoadedCommand[]): Record<string, CommandDefinition> {
|
|
|
|
|
const result: Record<string, CommandDefinition> = {}
|
|
|
|
|
for (const cmd of commands) {
|
2025-12-31 03:13:29 +09:00
|
|
|
const { name: _name, argumentHint: _argumentHint, ...openCodeCompatible } = cmd.definition
|
|
|
|
|
result[cmd.name] = openCodeCompatible as CommandDefinition
|
2025-12-09 15:48:13 +09:00
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function loadUserCommands(): Record<string, CommandDefinition> {
|
2025-12-26 23:28:33 +09:00
|
|
|
const userCommandsDir = join(getClaudeConfigDir(), "commands")
|
2025-12-09 15:48:13 +09:00
|
|
|
const commands = loadCommandsFromDir(userCommandsDir, "user")
|
|
|
|
|
return commandsToRecord(commands)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function loadProjectCommands(): Record<string, CommandDefinition> {
|
|
|
|
|
const projectCommandsDir = join(process.cwd(), ".claude", "commands")
|
|
|
|
|
const commands = loadCommandsFromDir(projectCommandsDir, "project")
|
|
|
|
|
return commandsToRecord(commands)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function loadOpencodeGlobalCommands(): Record<string, CommandDefinition> {
|
2025-12-26 23:28:33 +09:00
|
|
|
const { homedir } = require("os")
|
2025-12-09 15:48:13 +09:00
|
|
|
const opencodeCommandsDir = join(homedir(), ".config", "opencode", "command")
|
|
|
|
|
const commands = loadCommandsFromDir(opencodeCommandsDir, "opencode")
|
|
|
|
|
return commandsToRecord(commands)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function loadOpencodeProjectCommands(): Record<string, CommandDefinition> {
|
|
|
|
|
const opencodeProjectDir = join(process.cwd(), ".opencode", "command")
|
|
|
|
|
const commands = loadCommandsFromDir(opencodeProjectDir, "opencode-project")
|
|
|
|
|
return commandsToRecord(commands)
|
|
|
|
|
}
|