diff --git a/codex.el b/codex.el index 0c07c9e..323af7c 100644 --- a/codex.el +++ b/codex.el @@ -18,6 +18,7 @@ (require 'json) (require 'cl-lib) +(require 'subr-x) (defgroup codex nil "Chat with Codex." @@ -75,9 +76,6 @@ (let ((map (make-sparse-keymap))) (define-key map (kbd "C-c C-c") #'codex-send) (define-key map (kbd "C-c C-s") #'codex-switch-sessions) - (define-key map (kbd "C-c C-r") #'codex-reset) - (define-key map (kbd "M-p") #'codex-previous-prompt) - (define-key map (kbd "M-n") #'codex-next-prompt) map) "Keymap for `codex-mode'.") @@ -180,12 +178,14 @@ item (string-equal type "item.completed") (string-equal (gethash "type" item) "agent_message")))) - jsons)) - (item (gethash "item" msg)) - (text (gethash "text" item))) + jsons))) + (unless msg + (error "No proper agent_message" received)) + (let* ((item (gethash "item" msg)) + (text (gethash "text" item))) ;; We should make sure we get one and only one message here, otherwise bail out - (setf codex--session-id (codex--parse-session-id jsons)) - text)) + (setf codex--session-id (codex--parse-session-id jsons)) + text))) (defun codex--ensure-session-in-sessions-file (prompt) (let ((sessions (codex--read-sessions-file))) @@ -195,7 +195,7 @@ (defun codex--format-response (msg) (with-temp-buffer (insert msg) - (fill-paragraph) + (fill-region (point-min) (point-max)) (buffer-string))) (defun codex--send (prompt) @@ -204,22 +204,23 @@ (proc (make-process :name "codex" :buffer buf - :command (if (and codex--session-id (not (string-empty-p codex--session-id))) - (progn - (message "Using session %s" codex--session-id) - (list "codex" "exec" "resume" codex--session-id "--json" prompt)) - (progn - (message "Creating new session") - (list "codex" "exec" "--json" prompt)))))) + :command + (let ((resume (when (and codex--session-id (not (string-empty-p codex--session-id))) + (progn + (message "Using session %s" codex--session-id) + (list "resume" codex--session-id)))) + (skip (and codex-skip-git-repo-check (list "--skip-git-repo-check")))) + (append (list codex-command "exec") resume skip (list "--json" prompt)))))) (set-process-sentinel proc (lambda (p event) (when (string= event "finished\n") (with-current-buffer (process-buffer p) - (-> (buffer-string) - codex--parse-msg-from-response - codex--format-response - codex--write-to-chat) + (thread-first + (buffer-string) + codex--parse-msg-from-response + codex--format-response + codex--write-to-chat) (codex--ensure-session-in-sessions-file prompt) (kill-buffer))))))) @@ -262,12 +263,15 @@ (goto-char (point-max))))) (defun codex--read-sessions-file () - (with-temp-buffer - (insert-file-contents codex-sessions-file) - (goto-char (point-min)) - (condition-case nil - (read (current-buffer)) - (end-of-file nil)))) + (if (file-exists-p codex-sessions-file) + (with-temp-buffer + (insert-file-contents codex-sessions-file) + (goto-char (point-min)) + (condition-case nil + (read (current-buffer)) + (end-of-file nil))) + (codex--write-sessions-file '()) + nil)) (defun codex--write-sessions-file (list-of-session-ids) (let ((filename codex-sessions-file))