Kode CLI 自定义命令(Custom Commands)完全指南:用 Markdown 构建可复用斜杠命令工作流
AI 应用开发工具AI AgentCLI【免费下载链接】Kode-CLIKode CLI — Design for post-human workflows. One unit agent for every human computer task.项目地址https://gitcode.com/gh_mirrors/ko/Kode-CLI点击查看免费下载Kode CLI 允许你把任何可复用的提示词、检查清单或工作流模板封装成一条/命令通过 Markdown 文件定义、即时加载、随项目分发。本文以 docs/custom-commands.md 为骨架结合仓库内 customCommands.ts 源码、命令调度层与测试用例完整讲解命令目录布局、Frontmatter 参数、参数替换、动态内容Bash 执行与文件引用、与内置命令系统的集成方式以及常见排障手段。读完本文你可以为 Kode CLI 编写全局或项目级自定义命令并理解其从磁盘扫描到对话展开的完整调用链。自定义命令是什么Kode CLI 支持通过放置在.claude/commands/或.kode/commands/目录中的 Markdown 文件定义自定义斜杠命令。每条命令由一段「YAML Frontmatter 元数据 Markdown 提示词内容」构成本质上是把一段经过精心设计的提示词prompt固化下来在输入/命令名时展开成完整上下文送入对话实现可复用的工作流。从源码结构看自定义命令在命令体系中属于type: prompt类型。在 src/commands/index.ts 中getCommands()把三类命令合并为一个整体export const getCommands memoize(async (): PromiseCommand[] { const [mcpCommands, customCommands] await Promise.all([ getMCPCommands(), loadCustomCommands(), ]) return [...mcpCommands, ...customCommands, ...COMMANDS()].filter( _ _.isEnabled, ) })也就是说自定义命令与内置命令/help、/config、/clear等以及 MCP 命令一起参与统一路由执行前还会经过同一个hasCommand/getCommand解析逻辑见 src/commands/index.ts。这条合并链路是实现「自定义命令与内置命令无缝共存」的底层证据。命令存放目录全局与项目两个作用域自定义命令从多个位置加载.claude与.kode两个目录系统完全等价向后兼容全局命令所有项目可用~/.claude/commands/~/.kode/commands/项目命令仅当前项目可用./.claude/commands/./.kode/commands/源码中这四处路径在 customCommands.ts 中被逐一计算const projectLegacyCommandsDir join(cwd, .claude, commands) const userLegacyCommandsDir join(homedir(), .claude, commands) const projectKodeCommandsDir join(cwd, .kode, commands) const userKodeCommandsDir join(userKodeBaseDir, commands)其中用户级.kode基目录由getKodeBaseDir()提供可通过KODE_CONFIG_DIR环境变量调整测试用例中即通过该变量重定向用户目录见 agent-skills-compat.test.ts。getCustomCommandDirectories()会返回全部八个路径含 skills 目录/help界面会直接展示这些加载路径。作用域语义项目命令面向当前代码库、可随仓库提交分发全局命令面向个人通用工作流。loadCustomCommands()返回的每个命令都会标记scope: user | project并在去重时以userFacingName()为键保证唯一性命令按「文件命令 → skill 目录命令 → 插件命令」的顺序聚合后再过滤isEnabled见 customCommands.ts。提示项目命令在部分覆盖场景下具有更高优先级——docs/develop/modules/custom-commands.md 中的命令注册逻辑展示了对同名冲突的处理当命令名重复时scope project的命令会覆盖其它来源。命令文件格式与 Frontmatter 完整参数每条命令是一个带 YAML Frontmatter 的 Markdown 文件--- name: command-name description: Brief description of what this command does aliases: [alias1, alias2] enabled: true hidden: false progressMessage: Custom progress message... argNames: [arg1, arg2] --- Command prompt content goes here. You can reference arguments using {arg1} and {arg2} placeholders.Frontmatter 参数总表原文档核心表字段类型默认值说明namestring文件名命令名用作/namedescriptionstring自动生成在帮助中显示的描述aliasesstring[][]替代命令名enabledbooleantrue命令是否可用hiddenbooleanfalse是否从帮助输出中隐藏progressMessagestring自动生成执行期间显示的消息argNamesstring[][]用于替换的命名参数源码扩展参数原文档未覆盖但仓库已实现从 customCommands.ts 的CustomCommandFrontmatter接口可以看到实际支持的字段比文档表格更丰富字段类型作用allowed-toolsstring[]限制该命令执行时可用的工具列表argument-hintstring提示模型调用该命令时的参数示例when_to_usestring说明该命令何时适用供模型判断versionstring命令版本号modelstring指定执行模型inherit表示继承当前模型maxThinkingTokens/max-thinking-tokensnumber限制思考 token 数disable-model-invocationboolean禁止模型通过工具主动调用该命令这些字段在 SlashCommandTool.tsx 中被消费allowed-tools会写入上下文的commandAllowedTools以约束工具集model经过normalizeCommandModelName归一化haiku→quick、sonnet→task、opus→maindisable-model-invocation会让模型工具调用校验直接失败。Frontmatter 解析实现源码使用gray-matter配合js-yaml的JSON_SCHEMA解析 FrontmattercustomCommands.tsexport function parseFrontmatter(content: string): { frontmatter: CustomCommandFrontmatter content: string } { const yamlSchema (yaml as any).JSON_SCHEMA const parsed matter(content, { engines: { yaml: { parse: (input: string) yaml.load(input, yamlSchema ? { schema: yamlSchema } : undefined) ?? {}, }, }, }) return { frontmatter: (parsed.data ?? {}) as CustomCommandFrontmatter, content: parsed.content ?? , } }当description缺省时extractDescriptionFromMarkdown会从正文首行提取若首行是 Markdown 标题# ...取标题文字否则取首行正文并截断至 120 字符见 customCommands.ts。enabled/hidden等布尔值经toBoolean归一化1/true/yes/on均视为真值兼容不同写法。从零编写三个可直接运行的完整示例1. 简单命令文件~/.claude/commands/status.md或~/.kode/commands/status.md--- name: status description: Show project status aliases: [st, info] --- Provide a comprehensive project status including: - Current git status and recent commits - Modified files summary - Test results and build status - Development environment state Please analyze the current state and provide a structured status report.使用/status或/st或/info2. 参数化命令文件./.claude/commands/deploy.md或./.kode/commands/deploy.md--- name: deploy description: Generate deployment checklist aliases: [ship] argNames: [environment, version] progressMessage: Preparing deployment for {environment}... --- Generate deployment checklist for {environment} environment, version {version}: **Pre-deployment:** - Code review completion - All tests passing - Security scans completed **Deployment steps:** 1. Backup current state 2. Deploy to {environment} 3. Verify deployment 4. Monitor metrics **Post-deployment:** - Health checks - Performance monitoring - Documentation updates Please provide environment-specific recommendations and validate the deployment readiness.使用/deploy staging v2.1.03. 安全审计命令文件~/.claude/commands/security.md或~/.kode/commands/security.md--- name: security-audit description: Perform comprehensive security analysis aliases: [sec, audit] enabled: true progressMessage: Conducting security analysis... --- Perform a comprehensive security analysis: **Vulnerability Assessment:** - OWASP Top 10 vulnerabilities - Dependency vulnerabilities - Input validation issues - Authentication/authorization flaws **Code Security Review:** - Hardcoded secrets detection - Insecure data handling - Error handling security - Access control issues **Infrastructure Security:** - Environment configuration - Network security - SSL/TLS configuration - Container security Please provide prioritized security recommendations with remediation steps.使用/security-audit或/sec或/audit高级特性参数替换{argName}与$ARGUMENTS当定义argNames后可以在正文中使用{argName}占位符--- argNames: [framework, feature] --- Implement {feature} using {framework} framework: 1. Setup {framework} project structure 2. Create {feature} component 3. Add {framework}-specific configuration 4. Write tests for {feature}调用时按位置依次代入/implement react auth会把{framework}替换为react、{feature}替换为auth。源码中的getPromptForCommand(args)实际执行的是另一种更通用的策略见 customCommands.ts若正文包含$ARGUMENTS占位符则把整段参数原样替换进$ARGUMENTS若正文没有$ARGUMENTS则把参数以ARGUMENTS: text的形式追加到提示词末尾Skill 类命令还会在正文前注入Base directory for this skill: 目录上下文。也就是说$ARGUMENTS是官方推荐的参数注入格式而argNames{name}适合需要显式、有序占位的场景。二者可以按需混用。动态内容Bash 执行!与文件引用path命令正文支持两类动态指令在展开前被实时求值Bash 命令执行用!command包裹执行结果stdout/stderr会替换占位。例如Review the following pull request changes: !git diff main...HEAD .github/pull_request_template.md实现位于executeBashCommandscustomCommands.ts正则匹配!([^])以当前工作目录为cwd、5 秒超时执行命令输出为空时回退为(no output)失败时替换为(error executing: cmd)。同样的语法也适用于普通对话输入——userInput.tsx 在用户消息包含!或时会动态导入executeBashCommands与 mention 处理逻辑做预展开。文件引用以路径形式引用项目内文件加载后以代码块形式嵌入## File: src/file.js ...对应实现 resolveFileReferences[customCommands.ts](https://link.gitcode.com/i/cca864e078644ab066fee4acb26d9846#L59-L99)会跳过 agent- 前缀的引用文件不存在时替换为 (file not found: path)。 ### 进度消息progressMessage 用 progressMessage 定制命令执行时展示的状态文案支持引用参数 markdown --- progressMessage: Analyzing {framework} codebase for {feature} implementation... ---执行路径上userInput.tsx 会把该字段拼进元消息command-message{name} is {progressMessage}…/command-message让界面上能看到「正在执行什么」。条件显示hidden用hidden: true隐藏尚未完成或仅供内部使用的命令--- hidden: true # Wont appear in /help output ---被隐藏的命令依然可以被直接调用只是不出现在/help的命令清单中Help.tsx 先过滤!cmd.isHidden再渲染。命名空间用子目录组织命令命令文件支持放在commands目录的任意子目录中并用:分隔产生命名空间。从nameForCommandFile的实现可以推断customCommands.tscommands/test.md→/testcommands/dev/build.md→/dev:buildcommands/ci/github/deploy.md→/ci:github:deploy这为按模块/团队组织大量命令提供了原生的分组能力。插件命令的名称则会追加插件名前缀pluginName:localName插件内的commands/与skills/目录由 pluginRuntime.ts 收集后交给loadPluginCommandsFromDir加载。工具权限限制allowed-tools--- name: analyze-only description: Analyze without making changes allowed-tools: [file_read, grep, glob] ---allowed-tools既可以是[file_read, grep, glob]这样的数组也可以是空格分隔的字符串如Read Bash(git:*) Bash(jq:*)测试用例 agent-skills-compat.test.ts 验证了这种写法会被拆分为[Read, Bash(git:*), Bash(jq:*)]。parseAllowedTools对两种写法都做了兼容处理。该限制在 SlashCommandTool.tsx 中通过contextModifier写入commandAllowedTools从而约束本次展开上下文内的工具使用。模型与推理控制model、maxThinkingTokens--- model: sonnet maxThinkingTokens: 20000 ---model指定命令使用的模型inherit表示继承会话模型maxThinkingTokens限制思考预算。两者都通过 SlashCommandTool 的contextModifier注入到后续请求上下文中从而让不同命令可以按需切换模型与推理强度。禁止模型主动调用disable-model-invocation--- disable-model-invocation: true ---设置后模型通过SlashCommandTool或SkillTool主动调用该命令会被校验拦截SlashCommandTool.tsx但用户仍然可以在对话中直接输入/命令名手动触发——适合那些必须由用户显式确认才执行的敏感流程。最佳实践命令命名使用带连字符的描述性名称code-review、performance-audit提供有意义的别名[cr, review]之于code-review名称保持简洁清晰内容结构开头写明目标clear objectives使用结构化的列表与章节包含具体的上下文请求specific context requests结尾给出可执行的要求actionable requests参数使用为参数化命令定义清晰的argNames使用描述性占位符名{environment}、{version}当参数可省略时在正文中给出默认行为说明组织方式用子目录对相关命令分组配合命名空间特性使用一致的命名模式全局命令保持通用性general-purpose项目命令聚焦于具体代码库与 Kode 主系统的集成统一命令路由自定义命令经loadCustomCommands()加载后与 MCP 命令、内置命令在 getCommands 中合并去重用户输入以/开头时进入斜杠命令解析流程userInput.tsxprompt类型命令通过getPromptForCommand(args)展开为消息序列并携带isCustomCommand标记。帮助系统/help会渲染独立的Custom Commands区块列出/命令名 - 描述 (aliases: ...) [scope]同时展示所有加载目录与Skills loaded from信息并提示「Use /refresh-commands to reload after changes」Help.tsx。模型可主动执行自定义命令自定义命令不仅是用户手动输入的工具还会出现在模型侧SlashCommandTool与SkillToolSkillTool.tsx会枚举可用自定义命令/skill生成包含名称、参数提示argument-hint、适用时机when_to_use的清单注入模型提示使模型可以在合适时机主动发起/review-pr 123之类的调用。清单受字符预算限制默认 15000可用环境变量SLASH_COMMAND_TOOL_CHAR_BUDGET调整见 SlashCommandTool.tsx。缓存与性能loadCustomCommands使用memoize缓存缓存键由「当前目录 各命令目录是否存在 一分钟时间窗」构成customCommands.ts因此同一会话内命令只扫描解析一次新增/修改文件后需要手动刷新。刷新入口有两个内置命令/refresh-commandsrefresh-commands.ts调用reloadCustomCommands()清空缓存并统计加载数量安装插件时configureSessionPlugins也会自动触发reloadCustomCommands()与getCommands.cache.clear()pluginRuntime.ts。另外扫描过程带有 3 秒超时AbortController避免因命令目录异常阻塞会话启动。排障与常见问题命令没有加载确认文件扩展名是.md检查 Frontmatter 语法YAML 格式确认目录存在~/.claude/commands/、~/.kode/commands/、./.claude/commands/或./.kode/commands/检查文件权限是否可读语法错误YAML Frontmatter 必须格式正确数组可以写成[item1, item2]也可以用多行- item形式布尔值应写true/false不要加引号含特殊字符的字符串应加引号性能命令按会话缓存每个会话只加载一次命令数量过大可能影响启动时间可用enabled: false临时禁用命令而无需删除文件小结自定义命令是 Kode CLI 将「提示词工程」沉淀为可复用资产的核心机制一个 Markdown 文件即可完成命令定义、参数化、动态内容求值、工具权限约束与模型选择并可自由选择全局/项目/插件作用域发布。从 docs/custom-commands.md 到 customCommands.ts 的实现再到 userInput.tsx、SlashCommandTool.tsx 与 Help.tsx 的消费链路你可以沿着这些源码路径继续深入按需构建自己的命令库。赞分享AI 应用开发工具AI AgentCLI【免费下载链接】Kode-CLIKode CLI — Design for post-human workflows. One unit agent for every human computer task.项目地址https://gitcode.com/gh_mirrors/ko/Kode-CLI点击查看免费下载相关推荐forgecode 斜杠命令入门用 YAML Frontmatter 定义可复用 Prompt 的自定义命令forgecode 斜杠命令入门用 YAML Frontmatter 定义可复用 Prompt 的自定义命令 导读 本文围绕 forgecode面向 Cla人工智能AI Agent代码智能体AI 应用CLI开发工具gsd-2 扩展开发指南使用 Custom Commands 为用户提供可交互的斜杠命令gsd 2 扩展开发指南使用 Custom Commands 为用户提供可交互的斜杠命令 本篇文章面向 gsd 2pi coding agent的扩展开发人工智能AI Agent代码智能体Agent 编排CLIAI 应用Atuin AI 斜杠命令Slash Commands完全指南内置命令与技能命令的注册、调度与使用Atuin AI 斜杠命令Slash Commands完全指南内置命令与技能命令的注册、调度与使用 Atuin AI 是随 Atuin 一起提供的终端内CLI后端数据库上一篇WPScan 动态指纹识别实战从 themeisle-companion 的 CHANGELOG.md 解析 WordPress 插件版本下一篇如何同时搜索多个数据库Open Glean Scope切换与Collections子租户机制详解创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考