คู่มือการสำรวจ Codebase
วิธีสำรวจและศึกษา source code ของ Claude Code
เริ่มต้นอย่างรวดเร็ว
นี่คือ read-only reference codebase — ไม่มี build system หรือ test suite เป้าหมายคือการทำความเข้าใจวิธีสร้าง AI coding assistant ระดับ production
การปฐมนิเทศ
| อะไร | ที่ไหน |
|---|---|
| CLI entrypoint | src/main.tsx |
| Core LLM engine | src/QueryEngine.ts (~46K lines) |
| Tool definitions | src/Tool.ts (~29K lines) |
| Command registry | src/commands.ts (~25K lines) |
| Tool registry | src/tools.ts |
| Context collection | src/context.ts |
| Tool implementations ทั้งหมด | src/tools/ (40 subdirectories) |
| Command implementations ทั้งหมด | src/commands/ (~85 subdirectories + 15 files) |
การค้นหาสิ่งต่างๆ
”Tool X ทำงานอย่างไร?”
- ไปที่
src/tools/{ToolName}/ - การ implement หลักอยู่ใน
{ToolName}.tsหรือ.tsx - UI rendering อยู่ใน
UI.tsx - System prompt contribution อยู่ใน
prompt.ts
ตัวอย่าง — ทำความเข้าใจ BashTool:
src/tools/BashTool/
├── BashTool.ts ← Core execution logic
├── UI.tsx ← วิธี bash output render ใน terminal
├── prompt.ts ← สิ่งที่ system prompt บอกเกี่ยวกับ bash
└── ...
“Command X ทำงานอย่างไร?”
- ตรวจสอบ
src/commands/{command-name}/(directory) หรือsrc/commands/{command-name}.ts(file) - มองหา function
getPromptForCommand()(PromptCommands) หรือการ implement โดยตรง (LocalCommands)
“Feature X ทำงานอย่างไร?”
| Feature | เริ่มต้นที่นี่ |
|---|---|
| Permissions | src/hooks/toolPermission/ |
| IDE bridge | src/bridge/bridgeMain.ts |
| MCP client | src/services/mcp/ |
| Plugin system | src/plugins/ + src/services/plugins/ |
| Skills | src/skills/ |
| Voice input | src/voice/ + src/services/voice.ts |
| Multi-agent | src/coordinator/ |
| Memory | src/memdir/ |
| Authentication | src/services/oauth/ |
| Config schemas | src/schemas/ |
| State management | src/state/ |
”API call ไหลผ่านอย่างไร?”
ติดตามจาก user input ไปยัง API response:
src/main.tsx ← CLI parsing
→ src/replLauncher.tsx ← REPL session start
→ src/QueryEngine.ts ← Core engine
→ src/services/api/ ← Anthropic SDK client
→ (Anthropic API) ← HTTP/streaming
← Tool use response
→ src/tools/{ToolName}/ ← Tool execution
← Tool result
→ (feed back to API) ← ต่อ loop
Code Patterns ที่ควรรู้จัก
buildTool() — Tool Factory
ทุก tool ใช้ pattern นี้:
export const MyTool = buildTool({
name: 'MyTool',
inputSchema: z.object({ ... }),
async call(args, context) { ... },
async checkPermissions(input, context) { ... },
})
Feature Flag Gates
import { feature } from 'bun:bundle'
if (feature('VOICE_MODE')) {
// โค้ดนี้จะถูกตัดออกตอน build time ถ้า VOICE_MODE ปิด
}
Anthropic-Internal Gates
if (process.env.USER_TYPE === 'ant') {
// Features เฉพาะพนักงาน Anthropic
}
Index Re-exports
ส่วนใหญ่ directories มี index.ts ที่ re-export public API:
// src/tools/BashTool/index.ts
export { BashTool } from './BashTool.js'
Lazy Dynamic Imports
Module หนักๆ โหลดเฉพาะเมื่อต้องการ:
const { OpenTelemetry } = await import('./heavy-module.js')
ESM พร้อม .js Extensions
Bun convention — imports ทั้งหมดใช้ .js extensions แม้จะเป็นไฟล์ .ts:
import { something } from './utils.js' // จริงๆ import utils.ts
Key Files ตามขนาด
ไฟล์ที่ใหญ่ที่สุดมี logic มากที่สุดและน่าศึกษา:
| ไฟล์ | บรรทัด | ภายในมีอะไร |
|---|---|---|
QueryEngine.ts | ~46K | Streaming, tool loops, retries, token counting |
Tool.ts | ~29K | Tool types, buildTool, permission models |
commands.ts | ~25K | Command registry, conditional loading |
main.tsx | — | CLI parser, startup optimization |
context.ts | — | OS, shell, git, user context assembly |
เส้นทางการศึกษา
เส้นทาง 1: “Tool ทำงาน end-to-end อย่างไร?”
- อ่าน
src/Tool.ts— ทำความเข้าใจ interfacebuildTool - เลือก tool ง่ายๆ เช่น
FileReadToolในsrc/tools/FileReadTool/ - ติดตามวิธีที่
QueryEngine.tsเรียก tools ระหว่าง tool loop - ดูวิธีตรวจสอบ permissions ใน
src/hooks/toolPermission/
เส้นทาง 2: “UI ทำงานอย่างไร?”
- อ่าน
src/screens/REPL.tsx— หน้าจอหลัก - สำรวจ
src/components/— เลือก components สองสามอัน - ดู
src/hooks/useTextInput.ts— วิธีรับ user input - ตรวจสอบ
src/ink/— Ink renderer wrapper
เส้นทาง 3: “IDE integration ทำงานอย่างไร?”
- เริ่มที่
src/bridge/bridgeMain.ts - ติดตาม
bridgeMessaging.tsสำหรับ message protocol - ดู
bridgePermissionCallbacks.tsสำหรับวิธี route permissions ไปยัง IDE - ตรวจสอบ
replBridge.tsสำหรับ REPL session bridging
เส้นทาง 4: “Plugins ขยาย Claude Code อย่างไร?”
- อ่าน
src/types/plugin.ts— plugin API surface - ดู
src/services/plugins/— วิธีโหลด plugins - ตรวจสอบ
src/plugins/builtinPlugins.ts— ตัวอย่าง built-in - ดู
src/plugins/bundled/— bundled plugin code
เส้นทาง 5: “MCP ทำงานอย่างไร?”
- อ่าน
src/services/mcp/— MCP client - ดู
src/tools/MCPTool/— วิธีเรียก MCP tools - ตรวจสอบ
src/entrypoints/mcp.ts— Claude Code ในฐานะ MCP server - ดู
src/skills/mcpSkillBuilders.ts— skills จาก MCP
ใช้ MCP Server สำหรับการสำรวจ
Repo นี้มี standalone MCP server (mcp-server/) ที่ให้ MCP-compatible client สำรวจ source code ได้ ดู MCP Server README สำหรับการตั้งค่า
เมื่อเชื่อมต่อแล้ว สามารถถาม AI assistant เพื่อสำรวจ source:
- “BashTool ทำงานอย่างไร?”
- “ค้นหาที่ที่มีการตรวจสอบ permissions”
- “แสดงไฟล์ทั้งหมดใน bridge directory”
- “อ่าน QueryEngine.ts บรรทัด 1-100”
Grep Patterns
grep/ripgrep patterns ที่มีประโยชน์สำหรับการค้นหา:
# ค้นหา tool definitions ทั้งหมด
rg "buildTool\(" src/tools/
# ค้นหา command definitions ทั้งหมด
rg "satisfies Command" src/commands/
# ค้นหาการใช้ feature flag
rg "feature\(" src/
# ค้นหา Anthropic-internal gates
rg "USER_TYPE.*ant" src/
# ค้นหา React hooks ทั้งหมด
rg "^export function use" src/hooks/
# ค้นหา Zod schemas ทั้งหมด
rg "z\.object\(" src/schemas/
# ค้นหา system prompt contributions ทั้งหมด
rg "prompt\(" src/tools/*/prompt.ts
# ค้นหา permission rule patterns
rg "checkPermissions" src/tools/
ดูเพิ่มเติม
- Architecture — การออกแบบระบบโดยรวม
- Tools Reference — Catalog tool ทั้งหมด
- Commands Reference — Slash commands ทั้งหมด
- Subsystems Guide — การวิเคราะห์เชิงลึกเกี่ยวกับ Bridge, MCP, Permissions ฯลฯ