Chuyển đến nội dung chính

Bài 9: Context Management — Cung cấp ngữ cảnh cho AI

Vai trò của context window. Cách attach files, folders, selections. #file, #selection, #editor references. Workspace indexing. Codebase-aware prompting. Tips giữ context gọn và hiệu quả. Khi nào cần new session vs continue session.

💻 Lập trình — Bài 9 Bài 9: Context Management — Cung cấp ngữ cảnh cho AI

Vibe Coding với GitHub Copilot: Từ Cơ bản đến Nâng cao

Phần 3: Prompt Engineering cho Code

xdev.asia

1. Context là gì và tại sao quan trọng?

AI model chỉ "thấy" được những gì nằm trong context window — lượng text mà nó xử lý cho mỗi request. Nếu bạn không cung cấp đủ context, AI sẽ "đoán" và thường đoán sai.

Context window: [System prompt + Custom instructions + Your prompt + Attached files + History]
                ↑ tất cả phải fit trong giới hạn tokens của model

Context tốt = Code tốt. Đây là nguyên tắc #1 của Vibe Coding hiệu quả.

2. Các loại context trong Copilot

2.1. Implicit Context (tự động)

Copilot tự động thu thập:

  • File đang active trong editor
  • Selection hiện tại (nếu có)
  • Các tab đang mở
  • Workspace structure (file/folder tree)
  • Diagnostics (errors, warnings)
  • Terminal output gần nhất

2.2. Explicit Context (bạn cung cấp)

Bạn chủ động attach:

  • #file: attach file cụ thể
  • #selection: đoạn code đang select
  • #editor: toàn bộ visible area trong editor
  • #codebase: search semantic trong workspace
  • #terminalLastCommand: output của command vừa chạy
  • Drag & drop files vào chat
  • Images: attach screenshots (Mar 2026 feature)

3. Sử dụng Context Variables

3.1. #file — Attach file cụ thể

// Trong Chat view:
Look at #file:src/models/User.ts and #file:src/services/AuthService.ts
and create a password reset flow following the same patterns.

Khi gõ #file:, VS Code hiển thị file picker để bạn chọn.

3.2. #selection — Code đang select

// Select đoạn code trong editor, rồi trong Chat:
Explain #selection and suggest how to optimize it for performance.

3.3. #codebase — Tìm kiếm semantic trong workspace

// Copilot tự tìm files liên quan trong codebase:
#codebase How is authentication implemented in this project?
Show me the auth middleware and token verification logic.

3.4. #terminalLastCommand — Output terminal

// Sau khi chạy test fail trong terminal:
#terminalLastCommand Fix the failing tests. Analyze the error output
and make the necessary code changes.

4. Workspace Indexing

VS Code tự động index workspace để Copilot hiểu codebase:

  • Semantic index: hiểu ý nghĩa code (functions, classes, relationships)
  • Symbol index: tên functions, variables, types
  • File structure: biết project structure

Khi Agent chạy, nó có thể dùng tools như search, listDir, read để chủ động khám phá codebase — không chỉ dựa vào context bạn cung cấp.

5. Chiến lược Context hiệu quả

5.1. Mở đúng files trước khi prompt

// Trước khi viết prompt "Create UserService":
// Mở các files liên quan trong editor tabs:
Tab 1: models/User.ts        → Copilot biết User schema
Tab 2: services/AuthService.ts → Copilot biết coding pattern
Tab 3: routes/authRoutes.ts   → Copilot biết route conventions
Tab 4: tests/auth.test.ts     → Copilot biết testing pattern

5.2. Reference existing patterns

// Thay vì mô tả pattern từ đầu:
Create a ProductService following the exact same pattern as
#file:src/services/UserService.ts — same error handling,
same repository pattern, same validation approach.

5.3. Cung cấp constraints rõ ràng

// Context about project conventions:
Our project uses:
- Prisma for database (schema at #file:prisma/schema.prisma)
- Zod for validation (see pattern in #file:src/schemas/userSchema.ts)
- Custom AppError class for errors (#file:src/utils/AppError.ts)

Now create the OrderService with CRUD operations.

6. Khi nào New Session vs Continue Session?

Tình huống Hành động Lý do
Tiếp tục cùng feature Continue Giữ context + history
Feature hoàn toàn mới New session Context sạch, không bị nhiễu
Context bị quá dài New session + summary Tránh context overflow
Agent đi sai hướng New session + rephrase Reset mistakes in history
Debug issue liên quan Continue AI cần biết fix trước đó

Tip: Summary khi tạo new session

// Khi context quá dài, tạo new session với summary:
Context from previous session:
- We're building a task management app with Next.js 15 + Prisma
- Backend API is done (auth, CRUD for tasks, projects)
- Database schema is finalized
- Currently working on frontend

Continue from: Create the Task list page component with filtering and sorting capabilities.

7. Images as Context (2026 Feature)

Từ tháng 3/2026, bạn có thể attach images vào agent sessions:

  • Screenshot của UI design → "Build this UI"
  • Screenshot của error → "Fix this error"
  • Diagram/wireframe → "Implement this architecture"
  • Figma export → "Convert this design to React components"

8. Anti-patterns cần tránh

  • Dump toàn bộ codebase: quá nhiều context = noise, AI bị confuse
  • Không cung cấp context nào: AI phải đoán everything
  • Attach file không liên quan: waste context window
  • Continue session quá dài: context degrade, AI "quên" thông tin đầu
  • Copy-paste error không format: gây confuse cho AI

9. Tổng kết

Context management là kỹ năng quyết định chất lượng Vibe Coding:

  • ✅ Mở đúng files → implicit context tốt
  • ✅ Dùng #file, #selection, #codebase → explicit context chính xác
  • ✅ Reference existing patterns → output nhất quán
  • ✅ New session khi context bloated → tránh degrade
  • ✅ Attach images cho UI tasks → visual context

Bài tiếp theo sẽ cover Prompt Patterns nâng cao — slash commands, chat participants, và cách xây dựng prompt library cá nhân.