rtk icon indicating copy to clipboard operation
rtk copied to clipboard

Support for the new Grok CLI

Open frankgreco opened this issue 6 months ago โ€ข 3 comments

Thanks!

frankgreco avatar May 15 '26 05:05 frankgreco

wshm ยท Automated triage by AI

๐Ÿ” Automated Triage

โœจ Category feature
๐ŸŸข Priority low
๐ŸŽฏ Confidence 72%

Summary

Feature request to add RTK wrapper/support for the Grok CLI; body is nearly empty so details on commands or behavior needed are unknown.


Triaged automatically by wshm ยท This is an automated analysis, not a human review.

pszymkowiak avatar May 27 '26 12:05 pszymkowiak

This is needed, thanks

lovrozagar avatar Aug 25 '26 12:08 lovrozagar

Adding the contract details this issue is missing (needs-info), gathered from grok CLI v1.0.0 on macOS (its own bundled docs plus direct probing of rtk's hook processor).

Good news: grok's hook system is already Claude-Code-compatible on the output side. Its bundled docs (~/.grok/docs/user-guide/10-hooks.md) specify, for PreToolUse:

  • Config location: ~/.grok/hooks/*.json (global) or project-scoped, same shape as Claude Code:
    { "hooks": { "PreToolUse": [ { "matcher": "...", "hooks": [ { "type": "command", "command": "...", "timeout": 10 } ] } ] } }
    
  • Output for rewriting: {"hookSpecificOutput": {"hookEventName": "PreToolUse", "updatedInput": {"command": "..."}}} โ€” byte-identical to what rtk hook claude already emits.
  • The docs also state grok accepts Cursor-style event names (preToolUse, beforeShellExecution) as aliases for PreToolUse.
  • Failures are fail-open; only an explicit deny blocks a call.

The only incompatibility is the input side โ€” casing:

Claude Code (what rtk hook claude parses) grok
tool name tool_name toolName
tool input tool_input toolInput
shell tool value Bash run_terminal_command (docs: "Bash โ†’ run_terminal_command", and a matcher on Bash matches both)

Verified locally:

$ echo '{"toolName":"run_terminal_command","toolInput":{"command":"git status"}}' | rtk hook claude
                      # (empty output โ€” not recognized)

$ echo '{"tool_name":"Bash","tool_input":{"command":"git status"}}' | rtk hook claude
{"hookSpecificOutput":{...,"updatedInput":{"command":"rtk git status"}}}

Suggested fix โ€” likely a handful of lines, no new handler needed: in the rtk hook claude input parser, accept toolName/toolInput as fallbacks for tool_name/tool_input, and treat run_terminal_command as an alias of Bash. Then rtk init --agent grok only needs to drop a JSON file into ~/.grok/hooks/.

I'm bridging it locally with a small shim that normalizes the casing, shells out to rtk hook claude, and passes the output through unchanged. Scope of what I verified: the shim's own transform (both casings in, correct updatedInput out, fail-open on bad/empty/non-shell input, no double-prefix when the command is already rtk-prefixed). End-to-end is also confirmed: grok inspect lists the handler as command matcher=Bash|run_terminal_command user (native grok layer, not the Claude-compat layer), and a headless run (grok -p "Run exactly one shell command: git status --short") landed in rtk's own history DB as git status --short -> rtk git status --short, with the row count going 110 -> 111. So the rewrite genuinely applies inside a live grok session. Happy to open a PR if a maintainer confirms the approach.

Two notes for whoever picks this up:

  1. Guard the rewrite when the command is already rtk-prefixed. rtk hook claude already returns empty in that case, so passing its output through verbatim is safe, but a hand-rolled handler could double-prefix.
  2. grok applies updatedInput only after all handlers finish, last rewrite wins โ€” so ordering matters if another hook also rewrites.

NashCC1550 avatar Aug 29 '26 07:08 NashCC1550