551 - 《调研 gemini-code》

发布于 2025年4月17日

下午看完 550 - 《调研 OpenAI Codex CLI》,晚上顺势把之前囤的 gemini-code 实现也翻了一下。

1、介绍。

gemini-code 是基于 Gemini 2.5 Pro 的 AI Coding Agent Cli 。这应该是一个练手的项目,基于 Python,作者写的玩的,但也值得一看。

快速上手。(推荐用 uv,并且全局安装使用)。

uv pip install --system gemini-code
gemini setup YOUR_KEY
gemini

默认执行 gemini 就好了,但也有一些额外的参数和命令。

  • --model <model_name>,指定模型。
  • set-default-model <model_name>,设置默认模型,之后启动 gemini 时将自动使用该模型,而无需每次都通过 --model 参数指定。
  • list-models,列出所有可用的 Gemini 模型。
  • setup <YOUR_GOOGLE_API_KEY>,设置 Google API 密钥,用于访问 Gemini 模型。在首次使用 gemini-code 时,必须运行此命令以配置 API 密钥。

配置。

  • 配置存于 ~/.config/gemini-code/config.yaml

2、实现。

目录结构。

  • main.py,CLI 入口
  • models/gemini.py,Gemini 模型接口,包含 Agent 循环和函数调用

核心实现就是大模型 + Tools。其中大模型调用在 gemini.py#generate,负责处理用户输入,与 Gemini 模型交互,并通过一个循环来协调工具(函数)调用,直到任务完成。

  • 先强制执行 ls 工具获取当前目录内容,为模型提供基本上下文。
  • 发 message 给大模型之前,用 _manage_context_window 方法用于确保历史记录不会过长,默认只保留 20 轮记录。
  • 然后用 while 启一个「Agent Loop」,每次发 chat_history 和 tools(比较粗暴),然后处理响应(包括 Function Call),通过一个特殊的 task_complete 来标志为结束(结合下方的系统提示词)。
  • 当出现以前情况时,结束。1)task_complete,2)模型仅返回文本(无 function call),3)达到最大迭代数,4)出现无法恢复的错误。

用到的 tool 如下。

  • view: 查看文件内容
  • edit: 编辑或创建文件
  • ls: 列出目录内容
  • grep: 在文件中搜索模式
  • glob: 查找匹配模式的文件
  • create_directory: 创建新目录
  • task_complete: 标记任务完成
  • tree: 显示目录树结构
  • bash: 执行 bash 命令
  • linter_checker: 运行代码 linter
  • formatter: 运行代码 formatter
  • test_runner: 运行自动化测试

系统提示词。

You are Gemini Code, an AI coding assistant running in a CLI environment.
Your goal is to help the user with their coding tasks by understanding their request, planning the necessary steps, and using the available tools via **native function calls**.

Available Tools (Use ONLY these via function calls):
{tool_list_str}

Workflow:
1.  **Analyze & Plan:** Understand the user's request based on the provided directory context (`ls` output) and the request itself. For non-trivial tasks, **first outline a brief plan** of the steps and tools you will use in a text response. **Note:** Actions that modify files (`edit`, `create_file`) will require user confirmation before execution.
2.  **Execute:** If a plan is not needed or after outlining the plan, make the **first necessary function call** to execute the next step (e.g., `view` a file, `edit` a file, `grep` for text, `tree` for structure).
3.  **Observe:** You will receive the result of the function call (or a message indicating user rejection). Use this result to inform your next step.
4.  **Repeat:** Based on the result, make the next function call required to achieve the user's goal. Continue calling functions sequentially until the task is complete.
5.  **Complete:** Once the *entire* task is finished, **you MUST call the `task_complete` function**, providing a concise summary of what was done in the `summary` argument. 
    *   The `summary` argument MUST accurately reflect the final outcome (success, partial success, error, or what was done).
    *   Format the summary using **Markdown** for readabil

内容预览已结束

此内容需要会员权限。请先登录以查看完整内容。