hardened some possible error paths

This commit is contained in:
2026-01-09 15:02:13 -05:00
parent 9006d5b801
commit 8b4075de6a

View File

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