4141; ; loading it does not change global Markdown file associations.
4242; ;
4343; ; Usage:
44- ; ; M-x pi-coding-agent Start or focus session in current project
45- ; ; C-u M-x pi-coding-agent Start a named session
46- ; ; M-x pi-coding-agent-toggle Hide/show session windows in current frame
44+ ; ; M-x pi-coding-agent Start or focus session in current project
45+ ; ; C-u M-x pi-coding-agent Start a named session
46+ ; ; M-x pi-coding-agent-open-session-file Open a JSONL session file as live session
47+ ; ; M-x pi-coding-agent-toggle Hide/show session windows in current frame
4748; ;
4849; ; Many users define an alias: (defalias 'pi 'pi-coding-agent)
4950; ;
8384(require 'pi-coding-agent-menu )
8485(require 'pi-coding-agent-input )
8586
87+ (declare-function dired-get-filename " dired" (&optional localp no-error-if-not-filep))
88+
8689; ;;; Main Entry Point
8790
8891(defun pi-coding-agent--setup-session (dir &optional session )
@@ -138,6 +141,57 @@ Returns the chat buffer."
138141 (pi-coding-agent--set-chat-buffer chat-buf))
139142 chat-buf))
140143
144+ (defun pi-coding-agent--show-session-buffers (chat-buf input-buf )
145+ " Show CHAT-BUF and INPUT-BUF, focusing input when both are visible."
146+ (if (and (get-buffer-window-list chat-buf nil )
147+ (get-buffer-window-list input-buf nil ))
148+ (pi-coding-agent--focus-input-window chat-buf input-buf)
149+ (pi-coding-agent--display-buffers chat-buf input-buf)))
150+
151+ (defun pi-coding-agent--dired-regular-file-at-point ()
152+ " Return Dired's regular file at point, or nil."
153+ (when (derived-mode-p 'dired-mode )
154+ (when-let* ((file (dired-get-filename nil t )))
155+ (and (file-regular-p file)
156+ (expand-file-name file)))))
157+
158+ (defun pi-coding-agent--regular-jsonl-file-p (file )
159+ " Return non-nil if FILE is a cheap local JSONL file candidate."
160+ (when (stringp file)
161+ (let ((path (expand-file-name file)))
162+ (and (string-suffix-p " .jsonl" path)
163+ (not (file-remote-p path))
164+ (ignore-errors
165+ (and (file-regular-p path)
166+ (file-readable-p path)))))))
167+
168+ (defun pi-coding-agent--visited-jsonl-file-prompt-default ()
169+ " Return the current buffer's visited JSONL file for the prompt, or nil."
170+ (when-let* ((file buffer-file-name)
171+ (path (expand-file-name file)))
172+ (and (pi-coding-agent--regular-jsonl-file-p path)
173+ path)))
174+
175+ (defun pi-coding-agent--session-file-prompt-default ()
176+ " Return an explicit default file for the session-file prompt, or nil."
177+ (if (derived-mode-p 'dired-mode )
178+ (pi-coding-agent--dired-regular-file-at-point)
179+ (pi-coding-agent--visited-jsonl-file-prompt-default)))
180+
181+ (defun pi-coding-agent--read-session-file-name ()
182+ " Read an existing pi session file name from the minibuffer."
183+ (let* ((default-file (pi-coding-agent--session-file-prompt-default))
184+ (default-dir (and default-file (file-name-directory default-file)))
185+ (initial (and default-file (file-name-nondirectory default-file)))
186+ ; ; `read-file-name' otherwise uses the current buffer's visited file
187+ ; ; as a hidden default when DEFAULT-FILENAME and INITIAL are nil.
188+ (buffer-file-name nil ))
189+ (read-file-name " Pi session file: "
190+ default-dir
191+ default-file
192+ t
193+ initial)))
194+
141195;;;### autoload
142196(defun pi-coding-agent (&optional session )
143197 " Start or switch to pi coding agent session in current project.
@@ -159,12 +213,28 @@ frame, keeps layout unchanged and focuses the input window."
159213 (let ((dir (pi-coding-agent--session-directory)))
160214 (setq chat-buf (pi-coding-agent--setup-session dir session))
161215 (setq input-buf (buffer-local-value 'pi-coding-agent--input-buffer chat-buf))))
162- ; ; When both windows are already visible in current frame, just focus
163- ; ; the session input window. Otherwise restore/show the session layout.
164- (if (and (get-buffer-window-list chat-buf nil )
165- (get-buffer-window-list input-buf nil ))
166- (pi-coding-agent--focus-input-window chat-buf input-buf)
167- (pi-coding-agent--display-buffers chat-buf input-buf))))
216+ (pi-coding-agent--show-session-buffers chat-buf input-buf)))
217+
218+ ;;;### autoload
219+ (defun pi-coding-agent-open-session-file (session-file )
220+ " Open pi JSONL SESSION-FILE as a live session.
221+ This uses the normal chat/input UI and switches pi to SESSION-FILE; it is not a
222+ static viewer. The session header must record a non-empty absolute cwd that
223+ names an existing directory. Interactively, prompt for an existing file. In
224+ Dired, default to the regular file at point; otherwise, default to the current
225+ visited local regular readable .jsonl file when there is one."
226+ (interactive (list (pi-coding-agent--read-session-file-name)))
227+ (let* ((session-file (expand-file-name session-file))
228+ (dir (pi-coding-agent--session-file-cwd-or-error session-file)))
229+ (pi-coding-agent--check-dependencies)
230+ (let* ((chat-buf (pi-coding-agent--setup-session dir))
231+ (input-buf (buffer-local-value 'pi-coding-agent--input-buffer
232+ chat-buf))
233+ (proc (buffer-local-value 'pi-coding-agent--process chat-buf)))
234+ (pi-coding-agent--show-session-buffers chat-buf input-buf)
235+ (when (pi-coding-agent--session-transition-ready-p chat-buf " open" )
236+ (pi-coding-agent--resume-selected-session proc chat-buf session-file))
237+ chat-buf)))
168238
169239;;;### autoload
170240(defun pi-coding-agent-toggle ()
0 commit comments