import { query } from "@anthropic-ai/claude-agent-sdk";// Build up conversation history firsttry { for await (const message of query({ prompt: "What does the README in this directory cover?", options: { maxTurns: 2 } })) { if (message.type === "result" && message.subtype === "success") { console.log(message.result); } }} catch (error) { // A single-shot query() throws after yielding an error result, // so the follow-up query below still runs. console.error(`Session ended with an error: ${error}`);}// Send a slash command as a follow-up to the same conversationfor await (const message of query({ prompt: "/compact", options: { continue: true, maxTurns: 1 }})) { if (message.type === "result") { console.log("Command executed, result subtype:", message.subtype); // Example output: Command executed, result subtype: success }}
Refactor the selected code to improve readability and maintainability.Focus on clean code principles and best practices.
这创建了 /refactor 命令,您可以通过 SDK 使用它。
带有 Frontmatter
创建 .claude/commands/security-check.md:
Copy
---allowed-tools: Read, Grep, Globdescription: Run security vulnerability scanmodel: claude-opus-4-8---Analyze the codebase for security vulnerabilities including:- SQL injection risks- XSS vulnerabilities- Exposed credentials- Insecure configurations
在 SDK 中使用自定义命令
一旦在文件系统中定义,自定义命令就会自动通过 SDK 可用:
Copy
import { query } from "@anthropic-ai/claude-agent-sdk";// Use a custom commandtry { for await (const message of query({ prompt: "/refactor src/auth/login.ts", options: { maxTurns: 3 } })) { if (message.type === "assistant") { console.log("Refactoring suggestions:", message.message); } }} catch (error) { // A single-shot query() throws after yielding an error result, // so the second query below still runs. console.error(`Session ended with an error: ${error}`);}// Custom commands appear in the slash_commands listfor await (const message of query({ prompt: "Hello", options: { maxTurns: 1 }})) { if (message.type === "system" && message.subtype === "init") { console.log("Available commands:", message.slash_commands); // Includes built-in commands plus bundled skills and your custom commands, for example: // ["clear", "compact", "context", "usage", "code-review", "verify", "refactor", "security-check", ...] }}
高级功能
参数和占位符
自定义命令支持使用占位符的动态参数:
创建 .claude/commands/fix-issue.md:
Copy
---argument-hint: [issue-number] [priority]description: Fix a GitHub issue---Fix issue #$0 with priority $1.Check the issue description and implement the necessary changes.
在 SDK 中使用:
Copy
import { query } from "@anthropic-ai/claude-agent-sdk";// Pass arguments to custom commandfor await (const message of query({ prompt: "/fix-issue 123 high", options: { maxTurns: 5 }})) { // Command will process with $0="123" and $1="high" if (message.type === "result" && message.subtype === "success") { console.log("Issue fixed:", message.result); }}
Bash 命令执行
自定义命令可以执行 bash 命令并包含其输出:
创建 .claude/commands/git-commit.md:
Copy
---allowed-tools: Bash(git add *), Bash(git status *), Bash(git commit *)description: Create a git commit---## Context- Current status: !`git status`- Current diff: !`git diff HEAD`## TaskCreate a git commit with appropriate message based on the changes.
文件引用
使用 @ 前缀包含文件内容:
创建 .claude/commands/review-config.md:
Copy
---description: Review configuration files---Review the following configuration files for issues:- Package config: @package.json- TypeScript config: @tsconfig.json- Environment config: @.envCheck for security issues, outdated dependencies, and misconfigurations.
Claude Code 包含捆绑的 code-review 和 verify skills。如果您以其中之一的名称命名自定义命令,例如 .claude/commands/code-review.md,您的命令会覆盖捆绑的 skill,slash_commands 列表中该名称仅出现一次。
测试运行器命令
创建 .claude/commands/test.md:
Copy
---allowed-tools: Bash, Read, Editargument-hint: [test-pattern]description: Run tests with optional pattern---Run tests matching pattern: $ARGUMENTS1. Detect the test framework (Jest, pytest, etc.)2. Run tests with the provided pattern3. If tests fail, analyze and fix them4. Re-run to verify fixes
通过 SDK 使用这些命令:
Copy
import { query } from "@anthropic-ai/claude-agent-sdk";// Run code reviewtry { for await (const message of query({ prompt: "/review-pr", options: { maxTurns: 3 } })) { // Process review feedback }} catch (error) { // A single-shot query() throws after yielding an error result, // so the second query below still runs. console.error(`Session ended with an error: ${error}`);}// Run specific testsfor await (const message of query({ prompt: "/test auth", options: { maxTurns: 5 }})) { // Handle test results}