Skip to content

Commit 807dfec

Browse files
Copilottninja
andauthored
Add Opencode support as a new backend (#20)
* Initial plan * Add opencode support to ai-code-interface.el Co-authored-by: tninja <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: tninja <[email protected]>
1 parent 46867d4 commit 807dfec

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

README.org

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
77
- [[https://github.com/google-gemini/gemini-cli][Gemini CLI]]
88
- [[https://github.com/openai/codex][OpenAI Codex]]
99
- [[https://docs.github.com/en/copilot/how-tos/use-copilot-agents/use-copilot-cli][GitHub Copilot CLI]]
10+
- [[https://opencode.ai/][Opencode]]
1011

1112
- 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.
1213

@@ -64,6 +65,7 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
6465
- Gemini CLI (`[[https://github.com/linchen2chris/gemini-cli.el][gemini-cli.el]]`)
6566
- [[https://github.com/openai/codex][OpenAI codex CLI]] (`[[./ai-code-codex-cli.el][ai-code-codex-cli.el]]`)
6667
- [[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]]`)
68+
- [[https://opencode.ai/][Opencode]] (`[[./ai-code-opencode.el][ai-code-opencode.el]]`)
6769

6870
You can add other backends by customizing the `ai-code-backends` variable.
6971

ai-code-backends.el

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,17 @@ When called from Lisp code, sends CMD directly without prompting."
8888
:resume codex-cli-resume
8989
:config "~/.codex/config.toml"
9090
:upgrade "npm install -g @openai/codex@latest"
91-
:cli "codex"))
91+
:cli "codex")
92+
(opencode
93+
:label "ai-code-opencode.el"
94+
:require ai-code-opencode
95+
:start opencode
96+
:switch opencode-switch-to-buffer
97+
:send opencode-send-command
98+
:resume opencode-resume
99+
:config "~/.opencode/config.json"
100+
:upgrade nil
101+
:cli "opencode"))
92102
"Available AI backends and how to integrate with them.
93103
Each entry is (KEY :label STRING :require FEATURE :start FN :switch FN :send FN :resume FN-or-nil :upgrade STRING-or-nil :cli STRING).
94104
The :upgrade property can be either a string shell command or nil."

ai-code-interface.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
(require 'ai-code-discussion)
2727
(require 'ai-code-codex-cli)
2828
(require 'ai-code-github-copilot-cli)
29+
(require 'ai-code-opencode)
2930
(require 'ai-code-file)
3031
(require 'ai-code-ai)
3132

ai-code-opencode.el

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
;;; ai-code-opencode.el --- Thin wrapper for Opencode -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
;;
5+
;; Thin wrapper that reuses `claude-code' to run Opencode.
6+
;; Provides interactive commands and aliases for the AI Code suite.
7+
;;
8+
;; Opencode is an open-source alternative to Claude Code that provides
9+
;; HTTP server APIs and customization features (LSP, custom LLM providers, etc.)
10+
;; See: https://opencode.ai/
11+
;;
12+
;;; Code:
13+
14+
(require 'claude-code)
15+
16+
(declare-function claude-code--start "claude-code" (arg extra-switches &optional force-prompt force-switch-to-buffer))
17+
(declare-function claude-code--term-send-string "claude-code" (backend string))
18+
(defvar claude-code-terminal-backend)
19+
20+
21+
(defgroup ai-code-opencode nil
22+
"Opencode integration via `claude-code'."
23+
:group 'tools
24+
:prefix "opencode-")
25+
26+
(defcustom opencode-program "opencode"
27+
"Path to the Opencode executable."
28+
:type 'string
29+
:group 'ai-code-opencode)
30+
31+
;;;###autoload
32+
(defun opencode (&optional arg)
33+
"Start Opencode (reuses `claude-code' startup logic)."
34+
(interactive "P")
35+
(let ((claude-code-program opencode-program) ; override dynamically
36+
(claude-code-program-switches nil)) ; optional e.g.: '("exec" "--non-interactive")
37+
(claude-code arg)))
38+
39+
;;;###autoload
40+
(defun opencode-switch-to-buffer ()
41+
(interactive)
42+
(claude-code-switch-to-buffer))
43+
44+
;;;###autoload
45+
(defun opencode-send-command (line)
46+
(interactive "sOpencode> ")
47+
(claude-code-send-command line))
48+
49+
;;;###autoload
50+
(defun opencode-resume (&optional arg)
51+
"Resume a previous Opencode session.
52+
53+
This command starts Opencode with the --resume flag to resume
54+
a specific past session. The CLI will present an interactive list of past
55+
sessions to choose from.
56+
57+
If current buffer belongs to a project, start in the project's root
58+
directory. Otherwise start in the directory of the current buffer file,
59+
or the current value of `default-directory' if no project and no buffer file.
60+
61+
With double prefix ARG (\\[universal-argument] \\[universal-argument]),
62+
prompt for the project directory."
63+
(interactive "P")
64+
(let ((claude-code-program opencode-program)
65+
(claude-code-program-switches nil))
66+
(claude-code--start arg '("resume") nil t)
67+
(claude-code--term-send-string claude-code-terminal-backend "")
68+
(with-current-buffer claude-code-terminal-backend
69+
(goto-char (point-min)))))
70+
71+
(provide 'ai-code-opencode)
72+
73+
;;; ai-code-opencode.el ends here

0 commit comments

Comments
 (0)