代理模式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本文說明 Gemini Code Assist 的代理程式模式。
代理程式模式適用於 VS Code 和 IntelliJ 整合式開發環境 (IDE)。如要開始使用代理程式模式,請參閱將代理程式即時通訊當成結對程式設計師。
VS Code 中的代理模式是由 Gemini CLI 支援。
IntelliJ 中的代理程式模式不會使用 Gemini CLI。
使用代理程式模式,你可以執行下列操作:
- 詢問程式碼相關問題。
- 運用背景資訊和內建工具,改善生成的內容。
- 設定 MCP 伺服器,擴充代理程式的功能。
- 取得多步驟複雜工作的解決方案。
- 根據設計文件、問題和
TODO
註解生成程式碼。
- 在執行期間,您可以透過留言、編輯及核准計畫和工具使用情形,控制代理程式的行為。
代理程式模式的運作方式
在代理程式模式中,系統會將提示連同可用工具清單傳送至 Gemini API。Gemini API 會處理提示並傳回回覆。回覆內容可能是直接答案,也可能是要求使用可用工具。
當代理程式要求使用工具時,會準備使用工具,並檢查是否允許在有或沒有明確權限的情況下使用工具:
- 如果工具要求會修改檔案系統,或對任何資源執行變動作業,Gemini 會要求你允許該作業,除非你已將 Gemini 設定為一律允許該工具或工具。
- 唯讀工具要求可能不會在完成工作前要求權限。
系統要求允許使用工具時,您可以選擇允許或拒絕這項操作。服務專員也可能會提供選項,讓您一律允許使用工具或伺服器,或允許類似作業。詳情請參閱「一律允許代理程式動作」。
獲得或自行授予工具使用權後,代理程式會使用工具完成必要動作,並將動作結果傳回 Gemini API。Gemini 會處理工具動作的結果,並生成其他回覆。這類行動和評估的循環會持續進行,直到工作完成為止。
如果是複雜工作,Gemini 可能會顯示高階計畫,請先核准。開始流程前,你可以在對話中微調計畫並提出問題。如果對企劃書感到滿意,可以核准。核准計畫後,代理程式就會開始處理第一個工作,並在執行計畫時視需要要求您提供說明或權限。
代理模式脈絡
背景資訊可讓代理程式根據特定提示生成更優質的回覆。你可以從 IDE 中的檔案、本機系統資料夾中的檔案、工具回應和提示詳細資料取得情境資訊。
視 IDE 和設定而定,代理程式可用的情境可能有所不同。
以下分頁詳細說明如何為不同 IDE 收集內容。
VS Code
在代理程式模式中,Gemini Code Assist 通常可透過下列方式取得內容:
- IDE 工作區中的資訊。
- 來自 grep、終端機、檔案讀取或檔案寫入等內建工具的工具回應。
- Google 搜尋回覆。
- 提示或工具提供的特定網址內容。
- 以 Markdown 建立的脈絡檔案。
服務專員記憶體
VS Code 中的 Gemini Code Assist 代理模式會運用 Gemini CLI 記憶體探索服務,尋找並載入可為代理提供情境資訊的 GEMINI.md
檔案。記憶體探索服務會從目前的工作目錄開始,依階層搜尋這些檔案,然後向上移動至專案根目錄和主目錄。也會搜尋子目錄。
您可以建立全域、專案層級和元件層級的情境檔案,這些檔案會合併,為模型提供最相關的資訊。
您可以使用 /memory show
指令查看所有已載入 GEMINI.md
檔案的合併內容,並使用 /memory refresh
指令重新載入這些檔案。
IntelliJ
在代理程式模式中,Gemini Code Assist 通常可透過下列方式取得內容:
- IDE 專案中的資訊,包括檔案、索引符號,以及專案中符號的使用情形。
- 來自 grep、檔案讀取或檔案寫入等內建工具的工具回應。
- IntelliJ 版本管控。
- 已設定的 MCP 伺服器和工具
- 以 Markdown 建立的脈絡檔案。
您可以在代理程式模式的聊天提示區域,透過內容抽屜查看代理程式可用的內容。
工具是廣泛的服務類別,代理程式可用於回應提示時的背景資訊和動作。代理程式可透過工具呼叫 API 端點或其他代理程式,存取最新資訊。工具可能只提供一項功能,也可能提供多項相關功能。
例如 grep 和檔案讀取/寫入等內建工具、本機或遠端模型內容通訊協定 (MCP) 伺服器及其可執行函式,以及 RESTful API 呼叫。
在代理程式模式下,Gemini 可以存取內建的系統工具。選取 IDE,即可查看 Gemini 在代理程式模式中可用的內建工具清單。
IntelliJ
read_file
- 使用檔案的絕對路徑擷取文字內容。
write_file
- 將指定文字寫入指定檔案,如果檔案不存在,則會建立檔案。
analyze_current_file
- 分析編輯器中開啟的檔案,找出錯誤和警告。
find_files
- 根據檔案名稱或部分路徑尋找檔案的絕對路徑
grep
- 找出專案中含有特定文字模式或規則運算式的所有檔案。
list_files
- 列出指定絕對路徑中的所有檔案和目錄。
resolve_symbol
- 將特定符號參照解析為原始宣告。
find_usages
- 在專案中搜尋指定符號宣告的所有參照。
git
- 執行 Git 指令列介面 (CLI) 指令,並傳回結果。
list_vcs_roots
- 傳回目前專案中的所有版本管控系統 (VCS) 根目錄,例如 Git 存放區。
限制
標準 Gemini Code Assist 對話的部分功能可能無法在代理程式模式中使用,或運作方式與標準對話不同。
朗讀功能不適用於代理模式。在代理程式模式下,Gemini 不會引用來源,且無法停用與引用來源相符的程式碼建議。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[],[],null,["# Agent mode\n\n| **Preview**\n|\n| \u003cbr /\u003e\n|\n| This product or feature is in preview. Products and features that are\n| in preview are available \"as is\".\n|\n| \u003cbr /\u003e\n|\n\u003cbr /\u003e\n\nThis document describes agent mode in Gemini Code Assist.\n\nAgent mode is available in the VS Code and IntelliJ integrated development\nenvironments (IDEs). To start using agent mode, see\n[Use agentic chat as a pair programmer](/gemini-code-assist/docs/use-agentic-chat-pair-programmer).\n\nAgent mode in VS Code is powered by [Gemini CLI](/gemini-code-assist/docs/gemini-cli).\n\nAgent mode in IntelliJ doesn't use the Gemini CLI.\n\nWith agent mode, you can do any of the following and more:\n\n- Ask questions about your code.\n- Use context and built-in tools to improve generated content.\n- Configure MCP servers to extend the agent's abilities.\n- Get solutions to complex tasks with multiple steps.\n- Generate code from design documents, issues, and `TODO` comments.\n- Control the agent behavior by commenting on, editing, and approving plans and tool use during execution.\n\nHow agent mode works\n--------------------\n\nIn agent mode, your prompt is sent to the Gemini API with a list of\n[tools](#tools) that are available. The Gemini\nAPI processes the prompt and returns a response. The response might be a direct\nanswer or a request to use an available tool.\n\nWhen a tool is requested, the agent prepares to use the tool and checks to see\nif it is allowed to use the tool with or without explicit permission:\n\n- For tool requests that modify the file system, or perform mutating operations on any resources, Gemini will ask you to allow the operation unless you have configured Gemini to always allow the tool or tools.\n- Tool requests that are read-only might not ask for permission before completing the task.\n\nWhen asked to allow the use of a tool, you can choose to allow or deny the\noperation. The agent might also give you options to always allow a tool or\nserver or allow similar operations. For more information, see\n[Always allow agent actions](/gemini-code-assist/docs/use-agentic-chat-pair-programmer#yolo-mode).\n\nOnce permission to use the tool is given or self-granted, the agent uses the\ntool to complete the required action, and the result of that action is sent\nback to the Gemini API. Gemini processes the result of the\ntool action and generates another response. This cycle of action and\nevaluation continues until the task is complete.\n\nFor complex tasks, Gemini might show a high-level plan for your\napproval. You can fine tune the plan and ask questions in chat before beginning\nthe process. Once you are happy with the plan, you can approve it. After you\napprove the plan, the agent starts working on the first task, and will ask you\nfor clarifications or permissions as needed as it executes the plan.\n\nAgent mode context\n------------------\n\nContext allows an agent to generate better responses for a given prompt. Context\ncan be taken from files in your IDE, files in your local system folders, tool\nresponses, and your prompt details.\n\nDepending on your IDE and settings, different contexts might be available to the\nagent.\n\nThe following tabs detail how context is gathered for different IDEs. \n\n### VS Code\n\nThe following methods of getting context are usually available to\nGemini Code Assist in agent mode:\n\n- Information in your IDE workspace.\n- Tool responses from built-in tools like grep, terminal, file read, or file write.\n- Google Search responses.\n- Content from a given URL provided in a prompt or by a tool.\n- Context files you create in Markdown.\n\n### Agent memory\n\nGemini Code Assist agent mode in VS Code leverages the\nGemini CLI memory discovery service to find and load `GEMINI.md`\nfiles that provide context for the agent. The memory discovery service\nsearches for these files hierarchically, starting from the current working\ndirectory and moving up to the project root and your home directory. It also\nsearches in subdirectories.\n\nYou can create context files that are global,\nproject-level, and component-level, which are all combined to provide the\nmodel with the most relevant information.\n\nYou can use the `/memory show` command to see the combined content of all\nloaded `GEMINI.md` files, and the `/memory refresh` command to reload them.\n\n### IntelliJ\n\nThe following methods of getting context are usually available to\nGemini Code Assist in agent mode:\n\n- Information in your IDE project including your files, indexed symbols and usage of symbols in your project.\n- Tool responses from built-in tools like grep, file read, or file write.\n- IntelliJ [version control](https://www.jetbrains.com/help/idea/version-control-integration.html).\n- Configured MCP servers and tools\n- Context files you create in Markdown.\n\nYou can see the context available to the agent in the context drawer in the\nagent mode chat prompt area. \n\nTools\n-----\n\n*Tools* are a broad category of services that an agent can use for context and\nactions in its response to your prompt. Tools allow agents to access up-to-date\ninformation through function calling to API endpoints or to other agents. Tools\nmight only offer one function, or they might offer multiple related functions.\n\nSome example tools are built-in tools like grep and file read or write, local or\nremote Model Context Protocol (MCP) servers and their executable functions, and\nRESTful API calls.\n\n### Built-in tools\n\nIn agent mode, Gemini has access to your built-in system tools. Select\nyour IDE to view a list of built-in tools available to Gemini in\nagent mode. \n\n### VS Code\n\nAll of the\n[Gemini CLI built-in tools](https://github.com/google-gemini/gemini-cli/blob/main/docs/core/tools-api.md#built-in-tools) are\navailable to agent mode in Gemini Code Assist.\n\n### IntelliJ\n\n`read_file`\n: Retrieves the text content of a file using its absolute path.\n\n`write_file`\n: Writes the given text to a specified file, creating the file if it doesn't exist.\n\n`analyze_current_file`\n: Analyzes the open file in the editor for errors and warnings.\n\n`find_files`\n: Finds the absolute path to files given a filename or a part of the path\n\n`grep`\n: Finds all files inside the project that contain a given text pattern or regular expression.\n\n`list_files`\n: Lists all files and directories in a given absolute path.\n\n`resolve_symbol`\n: Resolves a specific symbol reference to its original declaration.\n\n`find_usages`\n: Searches the project for all references to a given symbol declaration.\n\n`git`\n: Runs a Git command-line interface (CLI) command and returns the result.\n\n`list_vcs_roots`\n: Returns all Version Control System (VCS) roots, such as Git repositories, in the current project.\n\nLimitations\n-----------\n\nSome features of [standard Gemini Code Assist chat](/gemini-code-assist/docs/chat-overview)\nmight not be available in agent mode or might work differently than they do in\nstandard chat.\n\nRecitation is not available in agent mode. While in agent mode, Gemini\ndoesn't [cite sources](/gemini-code-assist/docs/works#how-when-gemini-cites-sources) and you can't\n[disable code suggestions that match cited sources](/gemini-code-assist/docs/write-code-gemini#disable_code_suggestions_that_match_cited_sources).\n\nWhat's next\n-----------\n\n- [Use agentic chat as a pair programmer](/gemini-code-assist/docs/use-agentic-chat-pair-programmer).\n- Read the [Gemini CLI documentation](https://github.com/google-gemini/gemini-cli/blob/main/docs/index.md)."]]