Claude Code: คู่มือการสำรวจ Codebase

🇹🇭 Thai claude-codeexplorationguidethai
📋 Table of Contents (9 sections)
  1. คู่มือการสำรวจ Codebase
  2. เริ่มต้นอย่างรวดเร็ว
  3. การค้นหาสิ่งต่างๆ
  4. Code Patterns ที่ควรรู้จัก
  5. Key Files ตามขนาด
  6. เส้นทางการศึกษา
  7. ใช้ MCP Server สำหรับการสำรวจ
  8. Grep Patterns
  9. ดูเพิ่มเติม

คู่มือการสำรวจ Codebase

วิธีสำรวจและศึกษา source code ของ Claude Code


เริ่มต้นอย่างรวดเร็ว

นี่คือ read-only reference codebase — ไม่มี build system หรือ test suite เป้าหมายคือการทำความเข้าใจวิธีสร้าง AI coding assistant ระดับ production

การปฐมนิเทศ

อะไรที่ไหน
CLI entrypointsrc/main.tsx
Core LLM enginesrc/QueryEngine.ts (~46K lines)
Tool definitionssrc/Tool.ts (~29K lines)
Command registrysrc/commands.ts (~25K lines)
Tool registrysrc/tools.ts
Context collectionsrc/context.ts
Tool implementations ทั้งหมดsrc/tools/ (40 subdirectories)
Command implementations ทั้งหมดsrc/commands/ (~85 subdirectories + 15 files)

การค้นหาสิ่งต่างๆ

”Tool X ทำงานอย่างไร?”

  1. ไปที่ src/tools/{ToolName}/
  2. การ implement หลักอยู่ใน {ToolName}.ts หรือ .tsx
  3. UI rendering อยู่ใน UI.tsx
  4. 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 ทำงานอย่างไร?”

  1. ตรวจสอบ src/commands/{command-name}/ (directory) หรือ src/commands/{command-name}.ts (file)
  2. มองหา function getPromptForCommand() (PromptCommands) หรือการ implement โดยตรง (LocalCommands)

“Feature X ทำงานอย่างไร?”

Featureเริ่มต้นที่นี่
Permissionssrc/hooks/toolPermission/
IDE bridgesrc/bridge/bridgeMain.ts
MCP clientsrc/services/mcp/
Plugin systemsrc/plugins/ + src/services/plugins/
Skillssrc/skills/
Voice inputsrc/voice/ + src/services/voice.ts
Multi-agentsrc/coordinator/
Memorysrc/memdir/
Authenticationsrc/services/oauth/
Config schemassrc/schemas/
State managementsrc/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~46KStreaming, tool loops, retries, token counting
Tool.ts~29KTool types, buildTool, permission models
commands.ts~25KCommand registry, conditional loading
main.tsxCLI parser, startup optimization
context.tsOS, shell, git, user context assembly

เส้นทางการศึกษา

เส้นทาง 1: “Tool ทำงาน end-to-end อย่างไร?”

  1. อ่าน src/Tool.ts — ทำความเข้าใจ interface buildTool
  2. เลือก tool ง่ายๆ เช่น FileReadTool ใน src/tools/FileReadTool/
  3. ติดตามวิธีที่ QueryEngine.ts เรียก tools ระหว่าง tool loop
  4. ดูวิธีตรวจสอบ permissions ใน src/hooks/toolPermission/

เส้นทาง 2: “UI ทำงานอย่างไร?”

  1. อ่าน src/screens/REPL.tsx — หน้าจอหลัก
  2. สำรวจ src/components/ — เลือก components สองสามอัน
  3. ดู src/hooks/useTextInput.ts — วิธีรับ user input
  4. ตรวจสอบ src/ink/ — Ink renderer wrapper

เส้นทาง 3: “IDE integration ทำงานอย่างไร?”

  1. เริ่มที่ src/bridge/bridgeMain.ts
  2. ติดตาม bridgeMessaging.ts สำหรับ message protocol
  3. ดู bridgePermissionCallbacks.ts สำหรับวิธี route permissions ไปยัง IDE
  4. ตรวจสอบ replBridge.ts สำหรับ REPL session bridging

เส้นทาง 4: “Plugins ขยาย Claude Code อย่างไร?”

  1. อ่าน src/types/plugin.ts — plugin API surface
  2. ดู src/services/plugins/ — วิธีโหลด plugins
  3. ตรวจสอบ src/plugins/builtinPlugins.ts — ตัวอย่าง built-in
  4. ดู src/plugins/bundled/ — bundled plugin code

เส้นทาง 5: “MCP ทำงานอย่างไร?”

  1. อ่าน src/services/mcp/ — MCP client
  2. ดู src/tools/MCPTool/ — วิธีเรียก MCP tools
  3. ตรวจสอบ src/entrypoints/mcp.ts — Claude Code ในฐานะ MCP server
  4. ดู 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 ฯลฯ
← Back to claudecodeanalysis