Grok
将编码任务委托给 xAI Grok Build CLI(功能开发、PR)。
技能元数据
| 来源 | 可选 — 使用 hermes skills install official/autonomous-ai-agents/grok 安装 |
| 路径 | optional-skills/autonomous-ai-agents/grok |
| 版本 | 0.1.0 |
| 作者 | Matt Maximo (MattMaximo), Hermes Agent |
| 许可证 | MIT |
| 平台 | linux, macos, windows |
| 标签 | Coding-Agent, Grok, xAI, Code-Review, Refactoring, Automation |
| 相关技能 | codex, claude-code, hermes-agent |
参考:完整 SKILL.md
以下是 Hermes 在触发此技能时加载的完整技能定义。这是技能激活时代理看到的指令。
Grok Build CLI — Hermes 编排指南
通过 Hermes 终端将编码任务委托给 Grok Build(xAI 的自主编码代理 CLI,即 grok 命令)。Grok 可以读取文件、编写代码、运行 shell 命令、生成子代理以及管理 git 工作流。它有三种运行方式:交互式 TUI、无头模式(-p)以及作为基于 JSON-RPC 的 ACP 代理。
这是继 codex 和 claude-code 之后的第三个同类技能。编排模式几乎相同——对于一次性任务,首选无头模式 -p,对于交互式会话则使用 PTY。
何时使用
- 构建功能
- 重构
- PR 审查
- 批量问题修复
- 任何你原本会使用 Codex / Claude Code 但希望使用 Grok 的任务
先决条件
- 安装(首选):
npm install -g @xai-official/grok- 官方安装程序
curl -fsSL https://x.ai/cli/install.sh | bash也可行,但在某些环境中x.ai主机受到 Cloudflare 的限制。npm 路径完全避免了这种依赖。
- 官方安装程序
- 认证 — SuperGrok / X Premium+ 订阅(主要路径):
- 运行一次
grok login→ 打开浏览器进行 OAuth → 令牌缓存在~/.grok/auth.json中。这使用你的 SuperGrok 或 X Premium+ 订阅(无需按令牌 API 计费)。 - 通过检查是否存在
~/.grok/auth.json来查看登录状态,或者运行一个廉价的无头冒烟测试:grok --no-auto-update -p "Say ok." - 在 TUI 中,
/logout用于注销,/login(或重新启动)用于重新登录。
- 运行一次
- 不需要 git 仓库 — 与 Codex 不同,Grok 在 git 目录之外也能正常运行(适用于临时/一次性任务)。
- 零配置兼容 Claude Code / AGENTS.md — Grok 会自动读取
CLAUDE.md、.claude/(技能、代理、MCPs、钩子、规则)以及AGENTS.md系列文件。现有的项目上下文可直接使用。
API 密钥回退方案(非此用户的默认设置): Grok 还支持设置
XAI_API_KEY环境变量,以便通过api.x.ai进行按量付费计费。仅在grok login/ SuperGrok 认证不可用时使用此方案。订阅路径(grok login)是此处预期的设置。
两种编排模式
模式 1:无头模式 (-p) — 非交互式(首选)
运行一次性任务,打印结果并退出。无需 PTY,无需导航交互式对话框。这是最干净的集成路径——类似于 claude -p 和 codex exec。
terminal(command="grok --no-auto-update -p 'Add a dark mode toggle to settings'", workdir="/path/to/project", timeout=180)
在自动化中始终传递 --no-auto-update 以跳过后台更新检查。
何时使用无头模式:
- 一次性编码任务(修复错误、添加功能、重构)
- CI/CD 自动化和脚本编写
- 使用
--output-format json进行结构化输出解析 - 任何不需要多轮对话的任务
模式 2:交互式 PTY — 多轮 TUI 会话
TUI 是一个全屏、支持鼠标交互的应用程序。使用 pty=true 驱动它。为了进行稳健的监控/输入,请使用 tmux(与 claude-code 技能相同的模式)。
# Launch in a tmux session for capture-pane monitoring
terminal(command="tmux new-session -d -s grok-work -x 140 -y 40")
terminal(command="tmux send-keys -t grok-work 'cd /path/to/project && grok' Enter")
# Wait for startup, then send a task
terminal(command="sleep 5 && tmux send-keys -t grok-work 'Refactor the auth module to use JWT' Enter")
# Monitor progress
terminal(command="sleep 15 && tmux capture-pane -t grok-work -p -S -50")
# Exit when done
terminal(command="tmux send-keys -t grok-work '/quit' Enter && sleep 1 && tmux kill-session -t grok-work")
无头但内联输出的提示: 如果你希望获得类似 TUI 的输出而不占用全屏备用屏幕(例如,为了更清晰的日志),请添加 --no-alt-screen。对于纯自动化,无头模式 -p 仍然比 TUI 更干净。
无头模式深入解析
常用标志
| 标志 | 效果 |
|---|---|
-p, --single <PROMPT> | 发送一个提示,以无头模式运行,然后退出 |
-m, --model <MODEL> | 选择模型 |
-s, --session-id <ID> | 创建或恢复命名的无头会话 |
-r, --resume <ID> | 恢复现有会话 |
-c, --continue | 继续当前目录中最近的会话 |
--cwd <PATH> | 设置工作目录 |
--output-format <FMT> | plain(默认)、json 或 streaming-json |
--always-approve | 自动批准所有工具执行(相当于 --full-auto / --yolo) |
--no-alt-screen | 内联运行,不占用全屏 TUI |
--no-auto-update | 跳过后台更新检查(在所有自动化中使用) |
输出格式
plain— 人类可读文本(默认)json— 运行结束时输出一个 JSON 对象(便于干净地解析结果)streaming-json— 随着事件到达,输出换行符分隔的 JSON 事件
# Structured result for parsing
terminal(command="grok --no-auto-update -p 'List all TODO comments in src/' --output-format json", workdir="/project", timeout=120)
# Auto-approve for autonomous building
terminal(command="grok --no-auto-update --always-approve -p 'Refactor the database layer and run the tests'", workdir="/project", timeout=300)
后台模式(长任务)
# Start headless in background
terminal(command="grok --no-auto-update --always-approve -p 'Refactor the auth module'", workdir="/project", background=true, notify_on_complete=true)
# Returns session_id
# Monitor
process(action="poll", session_id="<id>")
process(action="log", session_id="<id>")
# Kill if needed
process(action="kill", session_id="<id>")
对于交互式(TUI)后台会话,使用 pty=true + tmux,并通过 tmux capture-pane 进行监控,这与 claude-code / codex skills 完全相同。
会话续接
# Start a named session
terminal(command="grok --no-auto-update -s refactor-db -p 'Start refactoring the database layer' --always-approve", workdir="/project", timeout=240)
# Resume it later
terminal(command="grok --no-auto-update -r refactor-db -p 'Now add connection pooling' --always-approve", workdir="/project", timeout=180)
# Or continue the most recent session in this directory
terminal(command="grok --no-auto-update -c -p 'What did you change last time?'", workdir="/project", timeout=60)
只读审计 → Markdown 笔记模式
要让 Grok 审查本地产物并返回一份干净的 markdown 笔记(用于 Obsidian 或代码仓库),且不修改任何内容:
- 首先使用 Hermes 工具(
read_file、write_file)准备稳定的输入文件。仅将相关上下文快照保存到临时文件中,而不是转储原始路径。 - 在不使用
--always-approve的情况下无头运行 Grok,使其无法自动写入,并要求输出格式为markdown only, no preamble(仅 markdown,无前导说明)。 - 使用
write_file()将 Grok 的标准输出直接保存到目标笔记中。
grok --no-auto-update -p "Read /tmp/current.md and /tmp/inventory.md. Produce markdown only, no preamble. Output a clean note titled 'Cleanup Review'." --output-format plain
陷阱(与 Claude Code 相同): 对于文档重写,宽松的“rewrite this”(重写这个)提示可能会返回更改摘要,而不是完整文件。相反:通过管道传入文件,并要求 Return ONLY the full revised markdown document. No intro, no explanation, no code fences. Start immediately with '# Title'.(仅返回完整的修订版 markdown 文档。无引言,无解释,无代码围栏。立即以 '# Title' 开头。)在覆盖目标文件之前,使用 read_file() 验证前几行。
PR 审查模式
快速审查(无头模式)
terminal(command="cd /path/to/repo && git diff main...feature-branch | grok --no-auto-update -p 'Review this diff for bugs, security issues, and style problems. Be thorough.'", timeout=120)
克隆到临时目录审查(安全,不修改仓库)
terminal(command="REVIEW=$(mktemp -d) && git clone https://github.com/user/repo.git $REVIEW && cd $REVIEW && gh pr checkout 42 && grok --no-auto-update -p 'Review the changes vs origin/main. Check bugs, security, race conditions, missing tests.'", pty=true, timeout=300)
发布审查意见
terminal(command="gh pr comment 42 --body '<review text>'", workdir="/path/to/repo")
使用 Worktrees 并行修复问题
# Create worktrees
terminal(command="git worktree add -b fix/issue-78 /tmp/issue-78 main", workdir="~/project")
terminal(command="git worktree add -b fix/issue-99 /tmp/issue-99 main", workdir="~/project")
# Launch Grok headless in each (background)
terminal(command="grok --no-auto-update --always-approve -p 'Fix issue #78: <description>. Commit when done.'", workdir="/tmp/issue-78", background=true, notify_on_complete=true)
terminal(command="grok --no-auto-update --always-approve -p 'Fix issue #99: <description>. Commit when done.'", workdir="/tmp/issue-99", background=true, notify_on_complete=true)
# Monitor
process(action="list")
# After completion: push and open PRs
terminal(command="cd /tmp/issue-78 && git push -u origin fix/issue-78")
terminal(command="gh pr create --repo user/repo --head fix/issue-78 --title 'fix: ...' --body '...'")
# Cleanup
terminal(command="git worktree remove /tmp/issue-78", workdir="~/project")
有用的子命令与 TUI 命令
| 命令 | 用途 |
|---|---|
grok | 启动交互式 TUI |
grok -p "query" | 无头一次性执行 |
grok login / grok logout | 登录 / 登出(SuperGrok / X Premium+ OAuth) |
grok inspect | 显示 Grok 在当前工作目录中发现的内容:配置源、指令、skills、插件、钩子、MCP 服务器 |
grok agent stdio | 作为 ACP agent 通过 JSON-RPC 运行(用于 IDE/工具集成) |
grok update | 更新 CLI(需要 x.ai 主机;在自动化中跳过) |
TUI 斜杠命令(仅限交互式):/model <name>、/always-approve、/plan、/context、/compact、/resume、/sessions、/fork、/usage、/quit。Shift+Tab 循环切换会话模式(包括 Plan 模式,该模式除会话计划文件外,阻止所有写入工具)。
配置 (~/.grok/config.toml)
[cli]
auto_update = false # skip background update checks persistently
[ui]
permission_mode = "ask" # or "always-approve" to skip tool prompts by default
[models]
default = "grok-build-0.1"
将全局偏好设置放在 ~/.grok/config.toml 中(而非项目范围的 .grok/config.toml)。permission_mode 取代了旧版的 approval_mode / yolo = true 键。
陷阱与注意事项
- 认证受订阅限制。
grok login需要 SuperGrok 或 X Premium+ 订阅。如果登录失败或不存在~/.grok/auth.json,请在回退到XAI_API_KEY之前确认订阅处于活动状态。 - 不要混淆 Hermes 的 xAI 认证与
grokCLI 的认证。 Hermes 的x_search运行在其自身的 xAI OAuth 上;独立的grokCLI 在~/.grok/auth.json中有单独的令牌。x_search正常工作并不意味着grok已登录。 - 在自动化中始终传递
--no-auto-update— 否则 Grok 会联网检查更新(且x.ai/storage.googleapis.com可能不可达)。 - 优先使用 npm install 而非 curl 安装程序 —
npm install -g @xai-official/grok避免了被 Cloudflare 屏蔽的x.ai主机。 --always-approve是自主构建开关。 如果没有它,无头运行可能会因等待工具批准提示而停滞。对于只读审查/审计工作,故意省略它,以便 Grok 无法修改文件。- 无头
-p跳过 TUI 对话框;TUI 需要pty=true(+ tmux 用于监控),就像 Claude Code 一样。 - 如果使用内联 TUI 且全屏 alt-screen 接管导致捕获的输出混乱,请使用
--no-alt-screen。 - 不需要 git 仓库,但对于 PR/提交工作流,你仍然需要一个 — 使用
mktemp -d && git init进行临时提交任务。 - 完成后使用
tmux kill-session -t <name>清理 tmux 会话。
Hermes Agents 规则
- 对于单个任务,优先使用无头
-p— 集成最干净,通过--output-format json输出结构化结果。 - 始终设置
workdir(或--cwd),以便 Grok 定位正确的项目。 - 在每次自动化调用中传递
--no-auto-update。 - 仅当 Grok 应自主写入时使用
--always-approve;对于只读审查和审计,请省略它。 - 使用
background=true, notify_on_complete=true后台运行长任务,并通过process工具进行监控。 - 对于多轮交互式工作使用 tmux,并使用
tmux capture-pane -t <session> -p -S -50进行监控。 - 在依赖认证之前先验证认证 — 检查
~/.grok/auth.json或运行一个简单的grok -p "Say ok."烟雾测试;不要假设 Hermes 的 xAI 认证会自动延续。 - 向用户报告结果 — 总结 Grok 更改了什么以及剩余什么。