Skip to content

Chore: Route prompts to visible AI session buffers#350

Merged
tninja merged 9 commits into
mainfrom
kang_feat_11
May 16, 2026
Merged

Chore: Route prompts to visible AI session buffers#350
tninja merged 9 commits into
mainfrom
kang_feat_11

Conversation

@tninja
Copy link
Copy Markdown
Owner

@tninja tninja commented May 15, 2026

This is about to make it possible to do things like take org-roam note on other repo

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, and ai-code--send-prompt-to-session-buffer in ai-code-prompt-mode.el.
  • ai-code--send-prompt now dispatches to a chosen visible session buffer (sending the string + return through the terminal helpers) or falls back to the previous ai-code-cli-send-command path.
  • New ERT tests covering visible-session detection, project-session matching, target selection (single, multi, none), and the two ai-code--send-prompt branches.

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.

Comment thread ai-code-prompt-mode.el Outdated
Comment on lines +247 to +248
;; 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
Comment thread ai-code-prompt-mode.el
Comment on lines +225 to +233
(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)))
Comment thread ai-code-prompt-mode.el
Comment on lines +203 to +223
(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))))))))
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread ai-code-prompt-mode.el Outdated
Comment on lines +189 to +190
(when (and (buffer-live-p buf)
(ai-code-backends-infra--session-buffer-p buf))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@tninja
Copy link
Copy Markdown
Owner Author

tninja commented May 15, 2026

@copilot double check the feedbacks and fix reasonable ones

Copilot AI and others added 7 commits May 15, 2026 02:08
Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

@copilot double check the feedbacks and fix reasonable ones

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.

Copilot finished work on behalf of tninja May 15, 2026 02:13
@tninja tninja merged commit 100bf2b into main May 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants