claude-use-gemini-acp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Claude → Gemini via ACP

通过ACP从Claude调用Gemini

Spawn a Gemini CLI subprocess in ACP mode, send a prompt, stream the response to stdout, and terminate cleanly. No persistent session — one prompt, one response.
以ACP模式启动Gemini CLI子进程,发送提示,将响应流式传输到标准输出,并干净地终止进程。无持久会话——一个提示对应一个响应。

When to use

使用场景

  • The task requires more context than fits in Claude's window
  • You want real-time Google Search grounding
  • You want a second opinion from a different model
  • The user says "ask Gemini", "use Gemini for this", etc.
  • 任务所需上下文超出Claude的窗口容量
  • 你需要实时基于谷歌搜索的支持
  • 你希望从不同模型获取第二意见
  • 用户要求“询问Gemini”“用Gemini处理这个”等

Setup (first use only)

首次使用设置

bash
cd ~/.claude/skills/claude-use-gemini-acp/scripts
pnpm install
This installs
@agentclientprotocol/sdk
into the local
node_modules/
. Subsequent runs skip this step — pnpm caches the result.
bash
cd ~/.claude/skills/claude-use-gemini-acp/scripts
pnpm install
这会将
@agentclientprotocol/sdk
安装到本地
node_modules/
目录。后续运行可跳过此步骤——pnpm会缓存结果。

Usage

使用方法

bash
GEMINI_MODEL=gemini-2.5-pro \
TARGET_CWD=/path/to/project \
node ~/.claude/skills/claude-use-gemini-acp/scripts/gemini-delegate.mjs \
  "Summarize the architecture in ARCHITECTURE.md and list the top 3 risks"
The response streams to stdout as Gemini generates it. Capture it for use in your response:
bash
response=$(node ~/.claude/skills/claude-use-gemini-acp/scripts/gemini-delegate.mjs "your prompt")
bash
GEMINI_MODEL=gemini-2.5-pro \
TARGET_CWD=/path/to/project \
node ~/.claude/skills/claude-use-gemini-acp/scripts/gemini-delegate.mjs \
  "总结ARCHITECTURE.md中的架构并列出前3个风险"
Gemini生成响应时会将其流式传输到标准输出。你可以捕获响应用于后续处理:
bash
response=$(node ~/.claude/skills/claude-use-gemini-acp/scripts/gemini-delegate.mjs "你的提示内容")

Environment variables

环境变量

VariableDefaultDescription
GEMINI_MODEL
gemini-2.5-pro
Model to use
GEMINI_CLI_VERSION
nightly
gemini-cli package version (
nightly
or a specific semver)
TARGET_CWD
current working dirWorking directory passed to the Gemini session
变量名默认值描述
GEMINI_MODEL
gemini-2.5-pro
使用的模型
GEMINI_CLI_VERSION
nightly
gemini-cli包版本(
nightly
或特定语义化版本)
TARGET_CWD
当前工作目录传递给Gemini会话的工作目录

How it works

工作原理

  1. Spawns
    pnpm dlx @google/gemini-cli@<version> --acp --yolo --model <model>
  2. Connects via
    @agentclientprotocol/sdk
    's
    ClientSideConnection
    +
    ndJsonStream
  3. Sends
    initialize
    newSession
    prompt
    over the ACP session
  4. Streams
    agent_message_chunk
    deltas to stdout as they arrive
  5. Terminates the subprocess once the prompt response completes
The
--yolo
flag (
-y
) auto-approves all tool calls inside Gemini's session.
  1. 启动
    pnpm dlx @google/gemini-cli@<version> --acp --yolo --model <model>
    子进程
  2. 通过
    @agentclientprotocol/sdk
    ClientSideConnection
    +
    ndJsonStream
    建立连接
  3. 在ACP会话上依次发送
    initialize
    newSession
    prompt
    指令
  4. agent_message_chunk
    增量到达时,将其流式传输到标准输出
  5. 提示响应完成后终止子进程
--yolo
标志(
-y
)会自动批准Gemini会话内的所有工具调用。

Prompt construction tips

提示构建技巧

  • Include all relevant context in the prompt string — this is a fresh session with no prior history
  • Reference files by absolute path if you want Gemini to read them (it can use its filesystem tools)
  • Keep prompts focused: one clear task per invocation
  • For multi-step work, do multiple sequential invocations rather than one sprawling prompt
  • 在提示字符串中包含所有相关上下文——这是一个无历史记录的全新会话
  • 如果希望Gemini读取文件,请使用绝对路径引用(它可以使用文件系统工具)
  • 提示要聚焦:每次调用对应一个明确任务
  • 对于多步骤工作,多次连续调用而非使用一个冗长的提示

Error handling

错误处理

If the script exits non-zero, check stderr for:
  • pnpm dlx
    download failures (network, auth)
  • Gemini auth errors (
    gemini auth login
    may be needed)
  • ACP initialization failures (version mismatch)
The script does not retry — if something fails, surface the error to the user and suggest re-running after fixing the root cause.
如果脚本非零退出,请检查标准错误输出以排查:
  • pnpm dlx
    下载失败(网络、权限问题)
  • Gemini认证错误(可能需要执行
    gemini auth login
  • ACP初始化失败(版本不匹配)
脚本不会重试——如果出现故障,请向用户显示错误并建议修复根本原因后重新运行。