Chore: Route prompts to visible AI session buffers#350
Conversation
There was a problem hiding this comment.
Pull request overview
Adds session-aware routing to ai-code--send-prompt so that when an AI session buffer is visible in the current frame, prompts can be sent directly to it (with optional disambiguation when the visible session belongs to a different project than the current buffer). This enables workflows like taking org-roam notes in another repo while still targeting a specific running AI session.
Changes:
- New helpers
ai-code--find-visible-session-buffer,ai-code--find-project-session-buffers,ai-code--prompt-choose-target-session, andai-code--send-prompt-to-session-bufferinai-code-prompt-mode.el. ai-code--send-promptnow dispatches to a chosen visible session buffer (sending the string + return through the terminal helpers) or falls back to the previousai-code-cli-send-commandpath.- New ERT tests covering visible-session detection, project-session matching, target selection (single, multi, none), and the two
ai-code--send-promptbranches.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| ai-code-prompt-mode.el | Adds visible/project session lookup and routes prompts to a chosen session buffer when appropriate. |
| test/test_ai-code-prompt-mode.el | Adds ERT tests for the new helpers and the routing logic in ai-code--send-prompt. |
| ;; DONE: right now it can only sent prompt to buffer associated ai coding session, which belong to the same git repo. If there is already a AI coding session buffer opened in side panel in same window, it should allow the prompt send to it. If there is already a ai coding session opened for the triggered buffer, but current side panel shows a different session, it should ask user to choose which session it want to send to. | ||
| ;; DONE: if the current side panel ai coding session is in the same git repo of the triggered buffer file, it should go through the same code path as before, no need to ask user to select session |
| (defun ai-code--send-prompt-to-session-buffer (prompt buffer) | ||
| "Send PROMPT directly to session BUFFER and display it." | ||
| (with-current-buffer buffer | ||
| (ai-code-backends-infra--terminal-send-string prompt) | ||
| (sit-for 0.5) | ||
| (ai-code-backends-infra--terminal-send-return)) | ||
| (if-let ((window (get-buffer-window buffer))) | ||
| (select-window window) | ||
| (ai-code-backends-infra--display-buffer-in-side-window buffer))) |
| (defun ai-code--prompt-choose-target-session () | ||
| "Choose AI session buffer to send prompt to. | ||
| Return session buffer when a non-default target is chosen, nil for default." | ||
| (when-let ((visible-session (ai-code--find-visible-session-buffer))) | ||
| (let* ((project-sessions (ai-code--find-project-session-buffers)) | ||
| (visible-is-project-session (memq visible-session project-sessions)) | ||
| (competing-sessions | ||
| (cl-remove visible-session project-sessions))) | ||
| (cond | ||
| (visible-is-project-session nil) | ||
| ((null competing-sessions) | ||
| visible-session) | ||
| (t | ||
| (let* ((choice-alist (mapcar (lambda (buf) | ||
| (cons (buffer-name buf) buf)) | ||
| (cons visible-session competing-sessions))) | ||
| (selection (completing-read | ||
| "Multiple AI sessions available. Send to: " | ||
| (mapcar #'car choice-alist) | ||
| nil t nil nil (buffer-name visible-session)))) | ||
| (cdr (assoc selection choice-alist)))))))) |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 80e09e4099
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| (when (and (buffer-live-p buf) | ||
| (ai-code-backends-infra--session-buffer-p buf)) |
There was a problem hiding this comment.
Guard session routing when infra helpers are unavailable
ai-code--send-prompt now always enters ai-code--prompt-choose-target-session, and this path unconditionally calls ai-code-backends-infra helpers. In entrypoints that load ai-code-prompt-mode without loading ai-code-backends-infra first (for example, module-level usage paths that don't go through ai-code.el), this can raise void-function ai-code-backends-infra--session-buffer-p before the normal ai-code-cli-send-command fallback runs. Please gate this routing behind fboundp/require so prompt sending still works when infra hasn't been loaded yet.
Useful? React with 👍 / 👎.
|
@copilot double check the feedbacks and fix reasonable ones |
Agent-Logs-Url: https://github.com/tninja/ai-code-interface.el/sessions/7be38823-4f8c-4e10-bed9-3867b32e0651 Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Agent-Logs-Url: https://github.com/tninja/ai-code-interface.el/sessions/7be38823-4f8c-4e10-bed9-3867b32e0651 Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Agent-Logs-Url: https://github.com/tninja/ai-code-interface.el/sessions/7be38823-4f8c-4e10-bed9-3867b32e0651 Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Agent-Logs-Url: https://github.com/tninja/ai-code-interface.el/sessions/7be38823-4f8c-4e10-bed9-3867b32e0651 Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Agent-Logs-Url: https://github.com/tninja/ai-code-interface.el/sessions/7be38823-4f8c-4e10-bed9-3867b32e0651 Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Agent-Logs-Url: https://github.com/tninja/ai-code-interface.el/sessions/7be38823-4f8c-4e10-bed9-3867b32e0651 Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Agent-Logs-Url: https://github.com/tninja/ai-code-interface.el/sessions/7be38823-4f8c-4e10-bed9-3867b32e0651 Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Fixed the substantive review items in c3c2062: removed the leftover planning notes, clarified the selection docstring, and gated direct prompt injection to terminal-managed session buffers so non-terminal AI buffers fall back to normal backend dispatch. I also updated the prompt-mode tests to cover the terminal-session guard. The remaining automated comments are style-only nits around the external buffer-local variable declaration. |
This is about to make it possible to do things like take org-roam note on other repo