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

第 9 課:情境管理-為 AI 提供情境

上下文視窗的作用。如何附加文件、資料夾、選擇。 #檔、#選擇、#編輯器參考。工作區索引。程式碼庫感知提示。保持上下文簡潔有效的技巧。什麼時候需要新的會話與繼續會話。

💻 程式設計 — 第 9 課 第 9 課:情境管理-提供語言 AI場景

使用 GitHub Copilot 進行 Vibe 編碼:從基礎知識到高級

第 3 部分:快速程式碼工程

亞洲開發網

1. 什麼是上下文?為什麼它很重要?

AI模型只能「看到」裡面的東西 上下文視窗 — 每個請求處理的文字量。如果你沒有提供足夠的上下文,AI就會“猜測”,而且經常猜錯。

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

良好的上下文=良好的程式碼。 這是有效 Vibe 編碼的第一原則。

2. Copilot 中的上下文類型

2.1.隱式上下文(自動)

Copilot 自動收集:

  • 文件處於活動狀態 在編輯器中
  • 選擇 目前(如果有)
  • 打開選項卡
  • 工作區結構 (文件/資料夾樹)
  • 診斷 (錯誤、警告)
  • 端子輸出 最近的

2.2.顯式上下文(您提供)

您主動附加:

  • #文件: 附上具體文件
  • #選擇: 正在選擇的代碼
  • #編輯器:編輯器中的整個可見區域
  • #程式碼庫:在工作區中搜尋語義
  • #terminalLastCommand:剛剛運行的命令的輸出
  • 拖放 文件進入聊天室
  • 圖片:附上螢幕截圖(2026 年 3 月專題)

3.使用上下文變數

3.1. #file — 附加特定文件

// 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.

打字時 #文件:,VS Code 顯示檔案選擇器供您選擇。

3.2. #selection — 程式碼正在選擇

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

3.3. #codebase — 在工作區中搜尋語意

// 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 — 輸出終端

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

4. 工作區索引

VS Code 自動索引工作區,以便 Copilot 了解程式碼庫:

  • 語意索引:理解程式碼的意義(函數、類別、關係)
  • 符號索引:函數名、變數、型別
  • 文件結構: 了解專案結構

Agent運行時可以使用諸如 搜尋。搜尋, 列表目錄, 讀 主動探索程式碼庫——而不僅僅是基於您提供的上下文。

5. 有效的情境策略

5.1.在出現提示之前開啟正確的文件

// 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.參考現有模式

// 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.提供明確的約束

// 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 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. 圖像作為背景(2026 年專題)

從 2026 年 3 月起,您可以 附加影像 轉至代理會話:

  • UI設計截圖→“建立這個UI”
  • 錯誤的螢幕截圖→“修復此錯誤”
  • 圖表/線框→“實現此架構”
  • Figma 匯出→“將此設計轉換為 React 元件”

8. 應避免反模式

  • 轉儲整個程式碼庫:太多背景 = 噪音,AI 很困惑
  • 未提供上下文:人工智慧必須猜測一切
  • 附加文件不相關: 浪費上下文視窗
  • 繼續會話時間太長:上下文退化,AI“忘記”第一個訊息
  • 複製貼上錯誤,沒有格式化:為人工智慧帶來混亂

9. 總結

情境管理是一項技能 決定 Vibe 編碼的質量:

  • ✅ 開啟正確的檔案 → 良好的隱式上下文
  • ✅ 正確使用#file、#selection、#codebase → 明確上下文
  • ✅ 參考現有模式 → 一致的輸出
  • ✅ 當上下文臃腫時新會話 → 避免降級
  • ✅ 為 UI 任務附加圖像 → 視覺上下文

下一首歌曲將是翻唱歌曲 進階提示模式 — 斜槓指令、聊天參與者以及如何建立自己的提示庫。