branch: elpa/gptel commit 33ee02393bc2bf97cd87dbd9e62b1427653d35ef Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com> Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
gptel: Make response tracking more robust * gptel.el (gptel--inherit-stickiness, gptel-mode): Finally bite the bullet and add an after-change-function to gptel-mode to track response regions more robustly. Now any change to a chat buffer in an LLM response region will be marked as a response, mostly resolving a number of problems and issues (#321, #343 and others). This change is experimental, and will be reverted if there are performance issues. Preliminary testing suggests that it does not cause any. It also affects only chat buffers, so the problem persists if you (i) use multi-turn requests (ii) via gptel-send (iii) in non-chat buffers. (gptel-rewrite and other ways of using gptel don't suffer from the response tracking issue.) --- gptel.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gptel.el b/gptel.el index 1f370d74d9..ff3cda305a 100644 --- a/gptel.el +++ b/gptel.el @@ -1445,6 +1445,16 @@ file." ;; NOTE: It's not clear that this is the best strategy: (add-to-list 'text-property-default-nonsticky '(gptel . t)) +(defun gptel--inherit-stickiness (beg end pre) + "Mark any change to an LLM response region as a response. + +Intended to be added to `after-change-functions' in gptel chat buffers, +which see for BEG, END and PRE." + (and (= pre 0) (< end (point-max)) + (and-let* ((val (get-text-property end 'gptel))) + (add-text-properties + beg end `(gptel ,val front-sticky (gptel)))))) + ;;;###autoload (define-minor-mode gptel-mode "Minor mode for interacting with LLMs." @@ -1459,6 +1469,7 @@ file." (gptel-mode -1) (user-error (format "`gptel-mode' is not supported in `%s'." major-mode))) (add-hook 'before-save-hook #'gptel--save-state nil t) + (add-hook 'after-change-functions 'gptel--inherit-stickiness nil t) (gptel--prettify-preset) (when (derived-mode-p 'org-mode) ;; Work around bug in `org-fontify-extend-region'. @@ -1553,6 +1564,7 @@ file." (buttonize (gptel--model-name gptel-model) (lambda (&rest _) (gptel-menu)))))))) (remove-hook 'before-save-hook #'gptel--save-state t) + (remove-hook 'after-change-functions 'gptel--inherit-stickiness t) (gptel--prettify-preset) (if gptel-use-header-line (setq header-line-format gptel--old-header-line