Commit 8f9d62cf91af

Vincent Demeester <vincent@sbr.pm>
2026-06-23 15:15:55
feat(emacs): add project-aware DWIM launchers for agents
Added vde/agent-shell-dwim and vde/pi-coding-agent-dwim that toggle the current project's session, prompt with completion when several exist, and create one when none are present. Prefix arg forces a new instance. Rebound C-c a a and C-c a p to the new commands and exposed explicit new-session bindings to streamline staying within Emacs.
1 parent f1e4f84
Changed files (1)
dots
config
emacs
dots/config/emacs/init.el
@@ -3162,13 +3162,41 @@ the appropriate file in ~/.local/share/imapfilter-rules/"
   :defer t)
 
 (use-package agent-shell
-  :commands (agent-shell agent-shell-toggle)
-  :bind (("C-c a a" . agent-shell)
+  :commands (agent-shell agent-shell-toggle agent-shell-new-shell)
+  :bind (("C-c a a" . vde/agent-shell-dwim)
+	 ("C-c a n" . agent-shell-new-shell)
 	 ("C-c a A" . agent-shell-toggle)
 	 ("C-c a ?" . agent-shell-help-menu))
   :init
   (declare-function agent-shell-google-make-authentication "agent-shell")
   (declare-function agent-shell-make-environment-variables "agent-shell")
+  (declare-function agent-shell-project-buffers "agent-shell")
+  (declare-function agent-shell--read-shell-buffer "agent-shell")
+  (declare-function agent-shell--display-buffer "agent-shell")
+  (defun vde/agent-shell-dwim (&optional arg)
+    "Project-aware agent-shell launcher.
+
+No prefix ARG:
+- 0 shells in this project -> start a new one
+- 1 shell  -> toggle its window (show/hide)
+- N shells -> `completing-read' to pick one, then display.
+
+With \\[universal-argument]: always force a brand-new shell."
+    (interactive "P")
+    (if arg
+	(agent-shell '(4))
+      (let ((buffers (agent-shell-project-buffers)))
+	(pcase (length buffers)
+	  (0 (agent-shell))
+	  (1 (let* ((buf (car buffers))
+		    (win (get-buffer-window buf)))
+	       (if win
+		   (quit-restore-window win 'bury)
+		 (agent-shell--display-buffer buf))))
+	  (_ (agent-shell--display-buffer
+	      (agent-shell--read-shell-buffer
+	       :prompt "Project agent shell: "
+	       :buffers buffers)))))))
   :config
   (setq agent-shell-google-authentication
 	(agent-shell-google-make-authentication :api-key (passage-get "redhat/google/osp/vdeemest-api-key")))
@@ -3192,9 +3220,37 @@ the appropriate file in ~/.local/share/imapfilter-rules/"
   :defer t)
 
 (use-package pi-coding-agent
-  :commands (pi-coding-agent)
-  :init (defalias 'pi #'pi-coding-agent)
-  :bind (("C-c a p" . pi-coding-agent))
+  :commands (pi-coding-agent pi-coding-agent-toggle)
+  :bind (("C-c a p" . vde/pi-coding-agent-dwim)
+         ("C-c a P" . pi-coding-agent))
+  :init
+  (defalias 'pi #'pi-coding-agent)
+  (declare-function pi-coding-agent-project-buffers "pi-coding-agent-ui")
+  (declare-function pi-coding-agent-toggle "pi-coding-agent")
+  (declare-function pi-coding-agent--show-session-buffers "pi-coding-agent")
+  (defun vde/pi-coding-agent-dwim (&optional arg)
+    "Project-aware pi-coding-agent launcher.
+
+No prefix ARG:
+- 0 sessions in this project -> start the default one
+- 1 session  -> toggle its windows (show/hide)
+- N sessions -> `completing-read' to pick one, then show.
+
+With \\[universal-argument]: prompt for a session name (new named session)."
+    (interactive "P")
+    (if arg
+	(let ((current-prefix-arg '(4)))
+	  (call-interactively #'pi-coding-agent))
+      (let ((buffers (pi-coding-agent-project-buffers)))
+	(pcase (length buffers)
+	  (0 (pi-coding-agent))
+	  (1 (pi-coding-agent-toggle))
+	  (_ (let* ((alist (mapcar (lambda (b) (cons (buffer-name b) b)) buffers))
+		    (choice (completing-read "Pi session: " alist nil t))
+		    (chat-buf (cdr (assoc choice alist)))
+		    (input-buf (buffer-local-value 'pi-coding-agent--input-buffer
+						   chat-buf)))
+	       (pi-coding-agent--show-session-buffers chat-buf input-buf)))))))
   :custom
   (pi-coding-agent-input-window-height 10)
   (pi-coding-agent-tool-preview-lines 10)