From 66ef378009bc8279009f1052e0a86fae4f74ab7a Mon Sep 17 00:00:00 2001 From: kj Date: Thu, 6 Aug 2026 00:01:51 -0300 Subject: [PATCH] fix(gptel-magit): handle reasoning cells in callbacks --- configs/init-ai.el | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/configs/init-ai.el b/configs/init-ai.el index 5082982..b684e2c 100644 --- a/configs/init-ai.el +++ b/configs/init-ai.el @@ -101,7 +101,12 @@ ) (add-hook 'gptel-post-response-functions 'gptel-end-of-response) - (setopt gptel-include-reasoning nil) ;; Mantener hasta resolver: https://github.com/ragnard/gptel-magit/issues/8 + ;; El "thinking" de los modelos se muestra en los buffers gptel. + ;; `ignore' (valor por defecto de gptel) lo muestra pero NO lo reenvía + ;; al modelo en turnos posteriores (recomendado: evita llenar el + ;; contexto, ver NEWS de gptel). Usar `t' si además se quiere + ;; reenviarlo y `nil' para deshabilitarlo (útil para respuestas mas rápidas). + (setopt gptel-include-reasoning 'ignore) (defun gptel-switch+model () "Switch to gptel backend and model in a single completion prompt." @@ -142,7 +147,41 @@ (use-package gptel-magit :ensure t - :hook (magit-mode . gptel-magit-install)) + :hook (magit-mode . gptel-magit-install) + :config + ;; gptel-magit no sabe manejar el contenido de "reasoning": gptel entrega + ;; el thinking al callback como (reasoning . TEXTO) y gptel-magit lo + ;; inserta como si fuera la respuesta final, fallando con + ;; Wrong type argument: char-or-string-p, (reasoning . "...") + ;; (https://github.com/ragnard/gptel-magit/issues/8). Esto ocurre + ;; aunque `gptel-include-reasoning' sea nil, porque gptel entrega las + ;; celdas (reasoning . ...) al callback de forma incondicional (solo + ;; controla si se insertan en el buffer gptel y si se reenvían). + ;; En vez de deshabilitar el thinking globalmente, sanitizamos la + ;; respuesta en el callback: se descartan las celdas de reasoning y solo + ;; se deja pasar la respuesta final (string). Así el thinking sigue + ;; activo en las sesiones gptel normales. + (defun kj-gptel-magit--string-response-callback (callback) + "Return a callback that forwards only string responses to CALLBACK. +Descarta las celdas (reasoning . TEXTO) con las que gptel entrega el +contenido de thinking al callback (ver `gptel-request')." + (lambda (response &rest args) + (when (stringp response) + (apply callback response args)))) + + (defun kj-gptel-magit--sanitize-callback (orig-fn &rest args) + "Around advice for `gptel-magit--request'. +Envuelve el argumento :callback de ARGS para que las respuestas con +contenido de reasoning no rompan gptel-magit." + (let ((pos (cl-position :callback args))) + (when (and pos (functionp (nth (1+ pos) args))) + (setf (nth (1+ pos) args) + (kj-gptel-magit--string-response-callback + (nth (1+ pos) args))))) + (apply orig-fn args)) + + (advice-add 'gptel-magit--request :around + #'kj-gptel-magit--sanitize-callback)) (use-package gptel-autocomplete :defer nil