Perintah Slash dalam SDK
Perintah slash menyediakan cara untuk mengontrol sesi Claude Code dengan perintah khusus yang dimulai dengan /. Perintah-perintah ini dapat dikirim melalui SDK untuk melakukan tindakan seperti menghapus riwayat percakapan, memadatkan pesan, atau mendapatkan bantuan.
Menemukan Perintah Slash yang Tersedia
Claude Agent SDK menyediakan informasi tentang perintah slash yang tersedia dalam pesan inisialisasi sistem. Akses informasi ini ketika sesi Anda dimulai:
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Hello Claude",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "init") {
console.log("Available slash commands:", message.slash_commands);
// Example output: ["/compact", "/clear", "/help"]
}
}import asyncio
from claude_agent_sdk import query
async def main():
async for message in query(
prompt="Hello Claude",
options={"max_turns": 1}
):
if message.type == "system" and message.subtype == "init":
print("Available slash commands:", message.slash_commands)
# Example output: ["/compact", "/clear", "/help"]
asyncio.run(main())Mengirim Perintah Slash
Kirim perintah slash dengan menyertakannya dalam string prompt Anda, sama seperti teks biasa:
import { query } from "@anthropic-ai/claude-agent-sdk";
// Send a slash command
for await (const message of query({
prompt: "/compact",
options: { maxTurns: 1 }
})) {
if (message.type === "result") {
console.log("Command executed:", message.result);
}
}Perintah Slash Umum
/compact - Memadatkan Riwayat Percakapan
/compact - Memadatkan Riwayat PercakapanPerintah /compact mengurangi ukuran riwayat percakapan Anda dengan merangkum pesan-pesan lama sambil mempertahankan konteks penting:
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "/compact",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "compact_boundary") {
console.log("Compaction completed");
console.log("Pre-compaction tokens:", message.compact_metadata.pre_tokens);
console.log("Trigger:", message.compact_metadata.trigger);
}
}/clear - Menghapus Percakapan
/clear - Menghapus PercakapanPerintah /clear memulai percakapan baru dengan menghapus semua riwayat sebelumnya:
import { query } from "@anthropic-ai/claude-agent-sdk";
// Clear conversation and start fresh
for await (const message of query({
prompt: "/clear",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "init") {
console.log("Conversation cleared, new session started");
console.log("Session ID:", message.session_id);
}
}Membuat Perintah Slash Kustom
Selain menggunakan perintah slash bawaan, Anda dapat membuat perintah kustom Anda sendiri yang tersedia melalui SDK. Perintah kustom didefinisikan sebagai file markdown dalam direktori tertentu, mirip dengan cara subagen dikonfigurasi.
Lokasi File
Perintah slash kustom disimpan dalam direktori yang ditentukan berdasarkan cakupannya:
- Perintah proyek:
.claude/commands/- Hanya tersedia dalam proyek saat ini - Perintah pribadi:
~/.claude/commands/- Tersedia di semua proyek Anda
Format File
Setiap perintah kustom adalah file markdown di mana:
- Nama file (tanpa ekstensi
.md) menjadi nama perintah - Konten file mendefinisikan apa yang dilakukan perintah
- Frontmatter YAML opsional menyediakan konfigurasi
Contoh Dasar
Buat .claude/commands/refactor.md:
Refactor the selected code to improve readability and maintainability.
Focus on clean code principles and best practices.Ini membuat perintah /refactor yang dapat Anda gunakan melalui SDK.
Dengan Frontmatter
Buat .claude/commands/security-check.md:
---
allowed-tools: Read, Grep, Glob
description: Run security vulnerability scan
model: claude-3-5-sonnet-20241022
---
Analyze the codebase for security vulnerabilities including:
- SQL injection risks
- XSS vulnerabilities
- Exposed credentials
- Insecure configurationsMenggunakan Perintah Kustom dalam SDK
Setelah didefinisikan dalam filesystem, perintah kustom secara otomatis tersedia melalui SDK:
import { query } from "@anthropic-ai/claude-agent-sdk";
// Use a custom command
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);
}
}
// Custom commands appear in the slash_commands list
for await (const message of query({
prompt: "Hello",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "init") {
// Will include both built-in and custom commands
console.log("Available commands:", message.slash_commands);
// Example: ["/compact", "/clear", "/help", "/refactor", "/security-check"]
}
}Fitur Lanjutan
Argumen dan Placeholder
Perintah kustom mendukung argumen dinamis menggunakan placeholder:
Buat .claude/commands/fix-issue.md:
---
argument-hint: [issue-number] [priority]
description: Fix a GitHub issue
---
Fix issue #$1 with priority $2.
Check the issue description and implement the necessary changes.Gunakan dalam SDK:
import { query } from "@anthropic-ai/claude-agent-sdk";
// Pass arguments to custom command
for await (const message of query({
prompt: "/fix-issue 123 high",
options: { maxTurns: 5 }
})) {
// Command will process with $1="123" and $2="high"
if (message.type === "result") {
console.log("Issue fixed:", message.result);
}
}Eksekusi Perintah Bash
Perintah kustom dapat mengeksekusi perintah bash dan menyertakan outputnya:
Buat .claude/commands/git-commit.md:
---
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`
## Task
Create a git commit with appropriate message based on the changes.Referensi File
Sertakan konten file menggunakan prefix @:
Buat .claude/commands/review-config.md:
---
description: Review configuration files
---
Review the following configuration files for issues:
- Package config: @package.json
- TypeScript config: @tsconfig.json
- Environment config: @.env
Check for security issues, outdated dependencies, and misconfigurations.Organisasi dengan Namespacing
Organisir perintah dalam subdirektori untuk struktur yang lebih baik:
.claude/commands/
├── frontend/
│ ├── component.md # Creates /component (project:frontend)
│ └── style-check.md # Creates /style-check (project:frontend)
├── backend/
│ ├── api-test.md # Creates /api-test (project:backend)
│ └── db-migrate.md # Creates /db-migrate (project:backend)
└── review.md # Creates /review (project)Subdirektori muncul dalam deskripsi perintah tetapi tidak mempengaruhi nama perintah itu sendiri.
Contoh Praktis
Perintah Code Review
Buat .claude/commands/code-review.md:
---
allowed-tools: Read, Grep, Glob, Bash(git diff:*)
description: Comprehensive code review
---
## Changed Files
!`git diff --name-only HEAD~1`
## Detailed Changes
!`git diff HEAD~1`
## Review Checklist
Review the above changes for:
1. Code quality and readability
2. Security vulnerabilities
3. Performance implications
4. Test coverage
5. Documentation completeness
Provide specific, actionable feedback organized by priority.Perintah Test Runner
Buat .claude/commands/test.md:
---
allowed-tools: Bash, Read, Edit
argument-hint: [test-pattern]
description: Run tests with optional pattern
---
Run tests matching pattern: $ARGUMENTS
1. Detect the test framework (Jest, pytest, etc.)
2. Run tests with the provided pattern
3. If tests fail, analyze and fix them
4. Re-run to verify fixesGunakan perintah-perintah ini melalui SDK:
import { query } from "@anthropic-ai/claude-agent-sdk";
// Run code review
for await (const message of query({
prompt: "/code-review",
options: { maxTurns: 3 }
})) {
// Process review feedback
}
// Run specific tests
for await (const message of query({
prompt: "/test auth",
options: { maxTurns: 5 }
})) {
// Handle test results
}Lihat Juga
- Perintah Slash - Dokumentasi perintah slash lengkap
- Subagen dalam SDK - Konfigurasi berbasis filesystem serupa untuk subagen
- Referensi TypeScript SDK - Dokumentasi API lengkap
- Ikhtisar SDK - Konsep SDK umum
- Referensi CLI - Antarmuka baris perintah