Skip to content

Commit 6f010c3

Browse files
committed
feat: add resume functionality for CLI sessions across backends
1 parent 1780710 commit 6f010c3

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

ai-code-backends.el

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313

1414
(defvar ai-code-cli)
1515

16+
(defun ai-code--unsupported-resume (&optional _arg)
17+
(interactive "P")
18+
(user-error "Backend '%s' does not support resume" (ai-code-current-backend-label)))
19+
20+
;;;###autoload
21+
(defun ai-code-cli-resume (&optional arg)
22+
"Resume the current backend's CLI session when supported."
23+
(interactive "P")
24+
(ai-code--unsupported-resume arg))
25+
1626
;;;###autoload
1727
(defcustom ai-code-backends
1828
'((claude-code
@@ -21,6 +31,7 @@
2131
:start claude-code
2232
:switch claude-code-switch-to-buffer
2333
:send claude-code-send-command
34+
:resume claude-code-resume
2435
:config "~/.claude.json"
2536
:cli "claude")
2637
(claude-code-ide
@@ -29,6 +40,7 @@
2940
:start claude-code-ide--start-if-no-session
3041
:switch claude-code-ide-switch-to-buffer
3142
:send claude-code-ide-send-prompt
43+
:resume claude-code-ide-resume
3244
:config "~/.claude.json"
3345
:cli "claude")
3446
(gemini
@@ -37,6 +49,7 @@
3749
:start gemini-cli
3850
:switch gemini-cli-switch-to-buffer
3951
:send gemini-cli-send-command
52+
:resume nil
4053
:config "~/.gemini/settings.json"
4154
:cli "gemini")
4255
(codex
@@ -45,16 +58,18 @@
4558
:start codex-cli
4659
:switch codex-cli-switch-to-buffer
4760
:send codex-cli-send-command
61+
:resume codex-cli-resume
4862
:config "~/.codex/config.toml"
4963
:cli "codex"))
5064
"Available AI backends and how to integrate with them.
51-
Each entry is (KEY :label STRING :require FEATURE :start FN :switch FN :send FN :cli STRING)."
65+
Each entry is (KEY :label STRING :require FEATURE :start FN :switch FN :send FN :resume FN-or-nil :cli STRING)."
5266
:type '(repeat (list (symbol :tag "Key")
5367
(const :label) (string :tag "Label")
5468
(const :require) (symbol :tag "Feature to require")
5569
(const :start) (symbol :tag "Start function")
5670
(const :switch) (symbol :tag "Switch function")
5771
(const :send) (symbol :tag "Send function")
72+
(const :resume) (choice (symbol :tag "Resume function") (const :tag "Not supported" nil))
5873
(const :cli) (string :tag "CLI name")))
5974
:group 'ai-code)
6075

@@ -97,6 +112,7 @@ Sets `ai-code-cli-*' defaliases and updates `ai-code-cli'."
97112
(start (plist-get plist :start))
98113
(switch (plist-get plist :switch))
99114
(send (plist-get plist :send))
115+
(resume (plist-get plist :resume))
100116
(cli (plist-get plist :cli)))
101117
;; If the declared feature is not available after require, inform user to install it.
102118
(when (and feature (not (featurep feature)))
@@ -108,6 +124,17 @@ Sets `ai-code-cli-*' defaliases and updates `ai-code-cli'."
108124
(defalias 'ai-code-cli-start start)
109125
(defalias 'ai-code-cli-switch-to-buffer switch)
110126
(defalias 'ai-code-cli-send-command send)
127+
(when (and resume (not (fboundp resume)))
128+
(user-error "Backend '%s' declares resume function '%s' but it is not callable."
129+
label (symbol-name resume)))
130+
(if resume
131+
(let ((resume resume))
132+
(fset 'ai-code-cli-resume
133+
(lambda (&optional arg)
134+
(interactive "P")
135+
(let ((current-prefix-arg arg))
136+
(call-interactively resume)))))
137+
(fset 'ai-code-cli-resume #'ai-code--unsupported-resume))
111138
(setq ai-code-cli cli
112139
ai-code-selected-backend key)
113140
(message "AI Code backend switched to: %s" (plist-get plist :label)))))

ai-code-codex-cli.el

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99

1010
(require 'claude-code)
1111

12+
(declare-function claude-code--start "claude-code" (arg extra-switches &optional force-prompt force-switch-to-buffer))
13+
(declare-function claude-code--term-send-string "claude-code" (backend string))
14+
(defvar claude-code-terminal-backend)
15+
16+
1217
(defgroup ai-code-codex-cli nil
1318
"Codex CLI integration via `claude-code'."
1419
:group 'tools
@@ -38,6 +43,15 @@
3843
(claude-code-send-command line))
3944

4045

46+
;;;###autoload
47+
(defun codex-cli-resume (&optional arg)
48+
"Resume a previous Codex CLI session."
49+
(interactive "P")
50+
(let ((claude-code-program codex-cli-program)
51+
(claude-code-program-switches nil))
52+
(claude-code--start arg '("resume") nil t)
53+
(claude-code--term-send-string claude-code-terminal-backend "")
54+
(goto-char (point-min))))
4155
(provide 'ai-code-codex-cli)
4256

4357
;;; ai-code-codex-cli.el ends here

ai-code-interface.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ Shows the current backend label to the right."
241241
["AI Code Commands"
242242
["AI CLI session"
243243
("a" "Start AI CLI" ai-code-cli-start)
244+
("R" "Resume AI CLI" ai-code-cli-resume)
244245
("z" "Switch to AI CLI" ai-code-cli-switch-to-buffer-or-hide)
245246
("s" ai-code--select-backend-description ai-code-select-backend)
246247
("g" "Open backend config (eg. add mcp)" ai-code-open-backend-config)

0 commit comments

Comments
 (0)