Git Ask via Gemini CLI

背景

之前跟 LLM 对话过程一直有个痛点,虽然我可以使用例如 FireCrawl/Tavis 这样的工具扩展 LLM 的能力,但是涉及到具体 某个项目的 Code Understanding 相关任务 的时候,不管是自建还是封闭 LLM 对话服务基本都无能为力,一般做法就是乖乖找台电脑,拉取代码到本地再使用例如 Cursor 等工具辅助分析,但一是麻烦,二是过度依赖完整桌面环境。

我自己目前正在开发的 AI Paper Assistant 应用,面对有开源实现的论文,虽然我添加了 MCP 的支持,但LLM 回答的范围也仅局限于论文本身和搜索引擎结果,想研究代码实现依旧还是得走前面的老路子,糟心。

Gemini CLI 的出现算是半解决了我的痛点(以及 Gemini 2.5 Flash 几乎可以无限用也是利好我这个穷人),不再依赖于桌面系统和图形化界面,但是跟现有 LLM 对话应用的集成依旧是个问题,其次在移动端 SSH 连接使用 Gemini CLI 的体验也十分差劲。

好消息是 Gemini CLI 提供非交互模式,可以自定义 Prompt、模型和上下文策略(但是不能调温度、max_tokens 等参数),这意味着我们可以在不修改源码的基础上,直接调用本地的 Gemini CLI 来封装 API。

1
2
3
# ihainan @ debian-dev in ~/extend/projects/Research/gemini-cli on git:main x .venv [15:04:18]
$ echo "What is the main purpose of this repository?" | gemini --model=gemini-2.5-flash
This repository contains the Gemini CLI, a command-line AI workflow tool that connects to your tools, understands your code, and accelerates your workflows. It allows users to query and edit large codebases, generate new applications, automate operational tasks, and integrate with other capabilities like media generation and Google Search.

解决痛点的第一步,先把 Gemini CLI 基于任意 Git/GitHub 实现问答的功能实现了,所有便有了 Gemini-CLI-Git-Ask 这个项目。

RESTful API

实现的功能很简单,首先是 RESTful API 调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ihainan @ debian-dev in ~ .venv [14:51:30]
$ curl -X POST http://192.168.100.101:18080/api/v1/ask \
-H "Content-Type: application/json" \
-d '{
"repository_url": "https://github.com/octocat/Hello-World",
"question": "What is the main purpose of this repository?"
}' | jq
{
"status": "success",
"answer": "This repository is a basic \"Hello World!\" project, likely used for introductory programming or as a placeholder.",
"repository": {
"url": "https://github.com/octocat/Hello-World",
"branch": "master",
"commit_hash": "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d"
},
"execution_time": 17542
}

上线一个 Live Demo,提供 Git/GitHub Repo URL 和问题,点击回答,稍等一会即可获得解答。后端对接的是 Gemini-2.5-Flash。

live demo

MCP Server

在 RESTful API 的基础上继续构建 MCP Server,目前只提供了一个 Tool ask-repository,接受下面三个参数:

  • repository_url
  • question
  • branch (optional)

已经上传 NPM,直接 NPX 安装/运行即可:env SERVICE_URL=http://<your_IP_address>:18080 npx -y git-ask-mcp-server

然后就可以在 LLM 对话应用里面对接 MCP Server 了。

llm_mcp

其他

这次 Vibe Coding 稍微让整个流程更为可控一些,每个模块下面都有一个单独的设计文档,单元/集成测试也有完整的状态追踪文档,Git commit message 也设置了规范并通过 Cursor Rules 要求 LLM 遵循。后续开发的过程每实现一个功能就会更新相应的文档。

此外也尝试把用到的重要提示词也做了记录,Cursor 并没有 Chat History 导出功能,所以先暂时手工维护放到了 Chat History 目录下面。