AI 程式設計教學中文版
官方教學中文版Agent 工作流

Search Tool

基於 Cursor 官方 Semantic & Agentic Search 文件解釋 Instant Grep、semantic search、indexing、privacy 和 Explore subagent。

📖 本篇術語速查表
英文 / 縮寫中文一句話解釋
Search Tool搜尋工具Agent 在程式碼庫裡檢索的能力。
精準檢索precise用準確條件縮小範圍。
上下文補充context檢索結果補給 Agent。

不想讀完?把下面這段提示詞丟給 AI 幫你跑完——幫你用 Cursor 的搜尋工具精準定位程式碼、補全上下文。

你是 Cursor 搜尋工具使用顧問,幫我用搜尋工具精準定位程式碼、給 Agent 補全相關上下文。

【角色】
你熟悉搜尋工具怎麼用準確條件檢索、怎麼把結果作為上下文補給 Agent、怎麼避免噪音。

【輸入】
- 我想找的程式碼 / 資訊:___
- 已知的線索(檔案 / 符號 / 關鍵詞):___
- 程式碼庫規模:___
- 找它要做什麼:___

【工作流程】
1. 把需求轉成精準檢索條件
2. 縮小範圍、過濾噪音
3. 把相關結果作為上下文
4. 給據結果推進的下一步

【輸出規範】
▌一、檢索條件
▌二、縮小範圍的方法
▌三、結果作上下文的用法
▌四、下一步

【硬約束】
- 檢索精準,不一次拉一堆無關結果
- 大庫注意效能,分批檢索
- 結果需對照確認,不盲信
- 不臆斷程式碼意圖
- 不確定的標註需進一步查
- 給的條件具體可用
- 給的每條結論都要落到具體可照做的步驟或示例,不停留在「建議」「考慮一下」這類沒法直接執行的空泛表述

Cursor Agent 的質量很大程度取決於它能不能找到正確上下文。官方 Search 文件說明,Agent 會組合多種 search tools:精確匹配用 Instant Grep,不知道名字時用 semantic search,複雜探索時連用搜尋、讀檔案和追引用。

閱讀目標:讀完本章,你應該能判斷什麼時候給 Agent 精確符號,什麼時候描述行為,並知道 indexing 和 .cursorignore 如何影響結果。

官方文件把搜尋分成兩類。

搜尋方式適合例子
Instant Grep已知函式名、變數名、錯誤字串、regex patternPaymentFailedErrorimport.*PaymentService
Semantic Search不知道準確名字,只知道行為或概念“where do we handle authentication?”

Instant Grep 支援 full regex 和 word-boundary matching。Semantic search 則依賴程式碼庫 indexing(索引),把程式碼切成 chunks(語義片段),生成 vector embeddings(向量嵌入,把文本轉成高維向量便於按相似度檢索),再按語義匹配。

flowchart TD
  Prompt["搜尋需求"] --> Known{"知道精確符號或字串"}
  Known -->|是| Grep["Instant Grep"]
  Known -->|否| Semantic["Semantic Search"]
  Semantic --> Entry["找到入口"]
  Entry --> Grep2["Grep 追引用"]
  Grep --> Read["Read files"]
  Grep2 --> Read
  Read --> Context["形成上下文"]

2. Indexing 怎麼工作

官方說明:

  • 開啟 workspace 後自動開始 indexing。
  • Cursor 把程式碼切成 meaningful chunks。
  • 每個 chunk 轉成 vector embedding。
  • Semantic search 在 80% completion 後可用。
  • index 每 5 分鐘自動同步一次,只處理 changed files。

變化處理:

ChangeAction
New files自動加入 index
Modified files刪除舊 embeddings,建立新 embeddings
Deleted files從 index 移除

可以在 Cursor Settings > Indexing 檢視狀態或觸發 re-index。Help Center 也說明可從 status bar 看進度,並透過 command palette 搜尋 Reindex。

3. ignore files 會影響搜尋質量

官方說明 Cursor indexes all files except ignore files,例如 .gitignore.cursorignore

為了提高搜尋質量,應排除:

  • node_modules
  • dist
  • build artifacts
  • generated files
  • 大型內容檔案
  • 與任務無關的產物目錄

如果 Agent 老是找到無關上下文,不一定是模型問題。先檢查 index 狀態和 .cursorignore

4. 隱私和安全邊界

官方 Search 文件說明:

  • File paths 傳送到 Cursor servers 前會加密。
  • Code content 不以 plaintext 儲存。
  • Indexing 時 code content 在記憶體中使用,然後丟棄。
  • Embeddings 建立時不存 filenames 或 source code。
  • Agent 搜尋時,Cursor 檢索 embeddings,並在 client side 解密 chunks。
  • 6 周不活動後 indexed codebases 會刪除;重新開啟專案會觸發 re-indexing。
深讀:為什麼先搜尋現有模式再改程式碼

真實專案裡,很多 bug 來自“沒找現有模式就新建一套”。Cursor 的 semantic search 和 grep 組合正是為了解決這個問題:先找到專案已有認證、錯誤處理、資料訪問、UI 元件模式,再按本地約定改。

好的 prompt 不是“幫我做登入”,而是“先找現有登入、表單校驗、API 請求、錯誤展示模式,再給最小實現計劃”。

5. Explore subagent

官方文件說明,Agent 可以自動使用 Explore subagent。它在自己的 context window 中執行,使用更快模型執行大量並行搜尋,只把相關 findings 返回主 conversation。

適合:

  • 大程式碼庫 broad search。
  • 多模組影響面盤點。
  • 找所有 validation、permission、billing、routing 入口。
  • 不想把主上下文塞滿 raw file contents 的場景。

你也可以直接要求:

Use a subagent to find all the places we validate user input.

本章自檢

完成本章後,用這 3 個問題檢查自己是否真正理解:

  1. Instant Grep 和 semantic search 分別適合什麼 prompt?
  2. Indexing 未完成或 .cursorignore 不合理,會怎樣影響 Agent?
  3. Explore subagent 為什麼能減少主 conversation 的上下文壓力?

透過標準:你能寫出一條先搜尋現有模式、再計劃、最後才修改的 Agent prompt。

官方來源

接下來去哪

本頁目錄