Skip to content

Commit 2504373

Browse files
richard134Richard Thomra
andauthored
Add Grok CLI backend support (#68)
* Add a CLI interface for Grok AI code backend * Fix: Add missing declare-function for claude-code--do-send-command Add missing function declarations to suppress byte-compiler warnings for claude-code--do-send-command which is called in all three CLI files. --------- Co-authored-by: Richard Thomra <richard.thomra@integrio.se>
1 parent bd2ec81 commit 2504373

File tree

6 files changed

+92
-4
lines changed

6 files changed

+92
-4
lines changed

README.org

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
1010
- [[https://github.com/openai/codex][OpenAI Codex]]
1111
- [[https://docs.github.com/en/copilot/how-tos/use-copilot-agents/use-copilot-cli][GitHub Copilot CLI]]
1212
- [[https://opencode.ai/][Opencode]]
13+
- [[https://github.com/xai-org/grok-cli][Grok CLI]]
1314

1415
- I switch across different CLI based AI tool in emacs: Claude Code / Gemini CLI / Aider / OpenAI Codex. If you also use different AI tools inside emacs, but want to keep same user interface and experience, this package is for you.
1516

@@ -74,6 +75,14 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
7475
- [[https://github.com/openai/codex][OpenAI codex CLI]] (`[[./ai-code-codex-cli.el][ai-code-codex-cli.el]]`)
7576
- [[https://docs.github.com/en/copilot/how-tos/use-copilot-agents/use-copilot-cli][GitHub Copilot CLI]] (`[[./ai-code-github-copilot-cli.el][ai-code-github-copilot-cli.el]]`)
7677
- [[https://opencode.ai/][Opencode]] (`[[./ai-code-opencode.el][ai-code-opencode.el]]`)
78+
- [[https://github.com/xai-org/grok-cli][Grok CLI]] (`[[./ai-code-grok-cli.el][ai-code-grok-cli.el]]`)
79+
80+
**** Grok CLI setup
81+
Install [[https://github.com/xai-org/grok-cli][grok-cli]] and ensure the `grok` executable is on your PATH.
82+
Customize `grok-cli-program` or `grok-cli-program-switches` if you want to
83+
point at a different binary or pass additional flags (for example,
84+
selecting a profile). After that, select the backend through
85+
`ai-code-select-backend` or bind a helper in your config.
7786

7887
You can add other backends by customizing the `ai-code-backends` variable.
7988

@@ -93,9 +102,9 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
93102
- *Implementing a TODO*: Write a comment in your code, like `;; TODO: Implement caching for this function`. Place your cursor on that line and press `C-c a`, then `i` (`ai-code-implement-todo`). The AI will generate the implementation based on the comment.
94103
- *Asking a Question*: Place your cursor within a function, press `C-c a`, then `q` (`ai-code-ask-question`), type your question, and press Enter. The question, along with context, will be sent to the AI.
95104
- *Refactoring a Function*: With the cursor in a function, press `C-c a`, then `r` (`ai-code-refactor-book-method`). Select a refactoring technique from the list, provide any required input (e.g., a new method name), and the prompt will be generated.
96-
- *Reviewing a Pull Request*: Press `C-c a`, then `v` (`ai-code-pull-or-review-diff-file`). Choose to generate a diff between two branches. The diff will be created in a new buffer, and you'll be prompted to start a review.
105+
- *Reviewing a Pull Request*: Press `C-c a`, then `v` (`ai-code-pull-or-review-diff-file`). Choose to generate a diff between two branches. The diff will be created in a new buffer, and you'll be prompted to start a review.
97106

98-
** Add a new AI coding CLI backend
107+
** Add a new AI coding CLI backend
99108

100109
- [[https://github.com/tninja/ai-code-interface.el/pull/2][This PR]] add the github-copilot-cli (require claude-code.el). It can be an example to add basic support for other AI coding CLI.
101110

@@ -111,7 +120,7 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
111120
{
112121
"$schema": "https://opencode.ai/config.json",
113122
"theme": "system"
114-
}
123+
}
115124
#+end_src
116125

117126
** Related emacs packages

ai-code-backends.el

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,17 @@ When called from Lisp code, sends CMD directly without prompting."
9898
:resume opencode-resume
9999
:config "~/.opencode/config.json"
100100
:upgrade nil
101-
:cli "opencode"))
101+
:cli "opencode")
102+
(grok
103+
:label "Grok CLI"
104+
:require ai-code-grok-cli
105+
:start grok-cli
106+
:switch grok-cli-switch-to-buffer
107+
:send grok-cli-send-command
108+
:resume grok-cli-resume
109+
:config "~/.config/grok/config.json"
110+
:upgrade nil
111+
:cli "grok"))
102112
"Available AI backends and how to integrate with them.
103113
Each entry is (KEY :label STRING :require FEATURE :start FN :switch FN :send FN :resume FN-or-nil :upgrade STRING-or-nil :cli STRING).
104114
The :upgrade property can be either a string shell command or nil."

ai-code-codex-cli.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
(declare-function claude-code--start "claude-code" (arg extra-switches &optional force-prompt force-switch-to-buffer))
1414
(declare-function claude-code--term-send-string "claude-code" (backend string))
15+
(declare-function claude-code--do-send-command "claude-code" (cmd))
1516
(defvar claude-code-terminal-backend)
1617

1718

ai-code-github-copilot-cli.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
(declare-function claude-code-switch-to-buffer "claude-code" (&optional arg))
1919
(declare-function claude-code--start "claude-code" (arg extra-switches &optional force-prompt force-switch-to-buffer))
2020
(declare-function claude-code--term-send-string "claude-code" (backend string))
21+
(declare-function claude-code--do-send-command "claude-code" (cmd))
2122

2223

2324
(defgroup ai-code-github-copilot-cli nil

ai-code-grok-cli.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
;;; ai-code-grok-cli.el --- Thin wrapper for Grok CLI -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
;; Provide Grok CLI integration by reusing `claude-code'.
5+
6+
;;; Code:
7+
8+
(require 'claude-code)
9+
(require 'ai-code-backends)
10+
11+
(declare-function claude-code--start "claude-code" (arg extra-switches &optional force-prompt force-switch-to-buffer))
12+
(declare-function claude-code--term-send-string "claude-code" (backend string))
13+
(declare-function claude-code--do-send-command "claude-code" (cmd))
14+
(defvar claude-code-terminal-backend)
15+
16+
(defgroup ai-code-grok-cli nil
17+
"Grok CLI integration via `claude-code'."
18+
:group 'tools
19+
:prefix "grok-cli-")
20+
21+
(defcustom grok-cli-program "grok"
22+
"Path to the Grok CLI executable."
23+
:type 'string
24+
:group 'ai-code-grok-cli)
25+
26+
(defcustom grok-cli-program-switches nil
27+
"Command line switches to pass to Grok CLI on startup."
28+
:type '(repeat string)
29+
:group 'ai-code-grok-cli)
30+
31+
;;;###autoload
32+
(defun grok-cli (&optional arg)
33+
"Start Grok CLI by leveraging `claude-code'."
34+
(interactive "P")
35+
(let ((claude-code-program grok-cli-program)
36+
(claude-code-program-switches grok-cli-program-switches))
37+
(claude-code arg)))
38+
39+
;;;###autoload
40+
(defun grok-cli-switch-to-buffer ()
41+
"Switch to the Grok CLI buffer."
42+
(interactive)
43+
(claude-code-switch-to-buffer))
44+
45+
;;;###autoload
46+
(defun grok-cli-send-command (line)
47+
"Send LINE to Grok CLI programmatically or interactively.
48+
When called interactively, prompts for the command.
49+
When called from Lisp code, sends LINE directly without prompting."
50+
(interactive "sGrok> ")
51+
(claude-code--do-send-command line))
52+
53+
;;;###autoload
54+
(defun grok-cli-resume (&optional arg)
55+
"Resume the previous Grok CLI session, when supported."
56+
(interactive "P")
57+
(let ((claude-code-program grok-cli-program)
58+
(claude-code-program-switches grok-cli-program-switches))
59+
(claude-code--start arg '("resume") nil t)
60+
(claude-code--term-send-string claude-code-terminal-backend "")
61+
(with-current-buffer claude-code-terminal-backend
62+
(goto-char (point-min)))))
63+
64+
(provide 'ai-code-grok-cli)
65+
66+
;;; ai-code-grok-cli.el ends here

ai-code-interface.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
(require 'ai-code-codex-cli)
2828
(require 'ai-code-github-copilot-cli)
2929
(require 'ai-code-opencode)
30+
(require 'ai-code-grok-cli)
3031
(require 'ai-code-file)
3132
(require 'ai-code-ai)
3233

0 commit comments

Comments
 (0)