跳到主要内容

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 代理

这是继 codexclaude-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 -pcodex 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(默认)、jsonstreaming-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 或代码仓库),且不修改任何内容:

  1. 首先使用 Hermes 工具(read_filewrite_file)准备稳定的输入文件。仅将相关上下文快照保存到临时文件中,而不是转储原始路径。
  2. 使用 --always-approve 的情况下无头运行 Grok,使其无法自动写入,并要求输出格式为 markdown only, no preamble(仅 markdown,无前导说明)。
  3. 使用 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/quitShift+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 键。

陷阱与注意事项

  1. 认证受订阅限制。 grok login 需要 SuperGrok 或 X Premium+ 订阅。如果登录失败或不存在 ~/.grok/auth.json,请在回退到 XAI_API_KEY 之前确认订阅处于活动状态。
  2. 不要混淆 Hermes 的 xAI 认证与 grok CLI 的认证。 Hermes 的 x_search 运行在其自身的 xAI OAuth 上;独立的 grok CLI 在 ~/.grok/auth.json 中有单独的令牌。x_search 正常工作并不意味着 grok 已登录。
  3. 在自动化中始终传递 --no-auto-update — 否则 Grok 会联网检查更新(且 x.ai/storage.googleapis.com 可能不可达)。
  4. 优先使用 npm install 而非 curl 安装程序npm install -g @xai-official/grok 避免了被 Cloudflare 屏蔽的 x.ai 主机。
  5. --always-approve 是自主构建开关。 如果没有它,无头运行可能会因等待工具批准提示而停滞。对于只读审查/审计工作,故意省略它,以便 Grok 无法修改文件。
  6. 无头 -p 跳过 TUI 对话框;TUI 需要 pty=true(+ tmux 用于监控),就像 Claude Code 一样。
  7. 如果使用内联 TUI 且全屏 alt-screen 接管导致捕获的输出混乱,请使用 --no-alt-screen
  8. 不需要 git 仓库,但对于 PR/提交工作流,你仍然需要一个 — 使用 mktemp -d && git init 进行临时提交任务。
  9. 完成后使用 tmux kill-session -t <name> 清理 tmux 会话。

Hermes Agents 规则

  1. 对于单个任务,优先使用无头 -p — 集成最干净,通过 --output-format json 输出结构化结果。
  2. 始终设置 workdir(或 --cwd),以便 Grok 定位正确的项目。
  3. 在每次自动化调用中传递 --no-auto-update
  4. 仅当 Grok 应自主写入时使用 --always-approve;对于只读审查和审计,请省略它。
  5. 使用 background=true, notify_on_complete=true 后台运行长任务,并通过 process 工具进行监控。
  6. 对于多轮交互式工作使用 tmux,并使用 tmux capture-pane -t <session> -p -S -50 进行监控。
  7. 在依赖认证之前先验证认证 — 检查 ~/.grok/auth.json 或运行一个简单的 grok -p "Say ok." 烟雾测试;不要假设 Hermes 的 xAI 认证会自动延续。
  8. 向用户报告结果 — 总结 Grok 更改了什么以及剩余什么。