代理模式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本文档介绍了 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 和文件读取或写入等内置工具、本地或远程 Model Context Protocol (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 不会引用来源,并且您无法停用与引用来源内容匹配的代码建议。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):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)."]]