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