Skip to content

Agentic Coding:AI Agent 驅動的編碼模式

2025 年最大的變化之一是 Agentic Coding 的成熟。AI 不再只是補全代碼的工具,而是能自主完成複雜任務的 Agent。來分享一些實戰模式。

Agentic vs 傳統 AI 輔助

傳統 AI 輔助(2023-2024):
  - Tab 補全:你寫一行,AI 補一行
  - Chat 對話:你問一個問題,AI 回答
  - 人類主導,AI 輔助

Agentic Coding(2025):
  - AI Agent 能理解整個項目上下文
  - 能自主規劃多步任務
  - 能調用工具(終端、文件系統、瀏覽器)
  - 人類定目標,AI 執行 + 人類審核

模式一:Spec-Driven Development

markdown
# 任務:實現用户收藏功能

## 需求
用户可以在產品詳情頁收藏產品,在個人中心查看收藏列表。

## 數據模型
- Favorite 表:id, userId, productId, createdAt
- 關聯 Product 和 User 表

## API
- POST /api/favorites - 添加收藏
- DELETE /api/favorites/:id - 取消收藏
- GET /api/favorites - 獲取收藏列表

## 前端
- ProductDetail 頁面添加收藏按鈕
- Favorites 頁面展示收藏列表
- 使用 optimistic UI 更新

## 約束
- 每個用户最多收藏 100 個產品
- 未登錄時引導登錄
bash
# 在 Claude Code 中執行
claude "按照 SPEC.md 實現用户收藏功能"
# Agent 會:
# 1. 創建數據庫 migration
# 2. 實現 API 路由
# 3. 創建前端組件
# 4. 編寫測試
# 5. 更新文檔

模式二:TDD with Agent

bash
# 先寫測試,讓 Agent 實現功能

# 1. 人類寫測試用例
cat > tests/search.test.ts << 'EOF'
import { describe, it, expect } from "vitest";
import { search } from "../lib/search";

describe("search", () => {
  it("should find exact match", () => {
    const results = search([{ name: "iPhone" }], "iPhone");
    expect(results).toHaveLength(1);
  });

  it("should support fuzzy search", () => {
    const results = search([{ name: "iPhone 15" }], "ipone");
    expect(results).toHaveLength(1);
  });

  it("should highlight match keywords", () => {
    const results = search([{ name: "iPhone 15 Pro" }], "iPhone");
    expect(results[0].highlight).toBe("<mark>iPhone</mark> 15 Pro");
  });

  it("should rank by relevance", () => {
    const items = [
      { name: "iPhone 15 Pro Max" },
      { name: "iPhone 15" },
      { name: "iPhone Case" },
    ];
    const results = search(items, "iPhone 15");
    expect(results[0].name).toBe("iPhone 15");
  });
});
EOF

# 2. 讓 Agent 實現功能,直到所有測試通過
claude "實現 lib/search.ts,讓 tests/search.test.ts 中的所有測試通過"

模式三:Refactoring Agent

bash
# 大規模重構任務

# 1. 把 class 組件遷移到函數組件
claude "將 src/components/ 下所有 class 組件遷移到函數組件 + hooks,
       保持 props 接口不變,保持功能不變"

# 2. 從 Redux 遷移到 Zustand
claude "將 src/store/ 從 Redux 遷移到 Zustand,
       保持所有 action 和 selector 的 API 不變"

# 3. CSS Modules 遷移到 Tailwind
claude "將 src/styles/ 下的 CSS Modules 遷移到 Tailwind CSS,
       直接寫在組件的 className 中"

模式四:Bug Fix Agent

bash
# 復現 bug 後讓 Agent 修復

# 1. 提供錯誤信息
claude "修復以下錯誤:
  TypeError: Cannot read properties of undefined (reading 'map')
  at ProductList (src/components/ProductList.tsx:15)

  復現步驟:
  1. 訪問 /products
  2. 網絡請求失敗時頁面崩潰
  3. 預期:顯示空狀態,不是崩潰"

# Agent 會:
# 1. 定位到 ProductList.tsx:15
# 2. 發現 products 可能為 undefined
# 3. 添加 loading 和 error 狀態處理
# 4. 可能還會建議添加 error boundary

模式五:Migration Agent

bash
# 依賴升級

# Next.js 14 → 15
claude "將項目從 Next.js 14 升級到 15,
       處理所有 breaking changes,
       運行測試確保功能不變"

# React 18 → 19
claude "將項目從 React 18 升級到 19,
       將 forwardRef 改為直接 ref prop,
       將 useFormState 改為 useActionState"

# Tailwind 3 → 4
claude "將項目從 Tailwind CSS 3 升級到 4,
       將 tailwind.config.js 遷移到 CSS @theme 塊"

Agent 的邊界

Agent 擅長的:
  ✓ 有明確規範的重複性工作
  ✓ 代碼遷移和重構
  ✓ 測試生成
  ✓ Bug 修復(有明確的錯誤信息)
  ✓ 文檔生成

Agent 不擅長的:
  ✗ 需要深入業務理解的決策
  ✗ 性能優化(需要分析實際數據)
  ✗ 架構設計(需要前瞻性思考)
  ✗ 安全敏感代碼(認證、加密)
  ✗ 創造性的 UI/UX 設計

小結

  • Agentic Coding 是 2025 年最重要的開發模式變化
  • Spec-Driven Development 讓開發流程更結構化
  • TDD + Agent 是高質量代碼的保證
  • Agent 擅長遷移、重構、測試生成,不擅長業務創新
  • 人類的角色從 "寫代碼" 轉變為 "定義需求 + 審核代碼"

MIT Licensed