branch: elpa/gptel
commit 129032fca88f29c20343e973c98fa17db3000405
Author: Karthik Chikmagalur <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>

    gptel-request: Evaluate system messages in request buffer
    
    * gptel-request.el (gptel-request): Parse the system message,
    evaluating function-valued system messages in the process, in the
    request buffer instead of the temporary prompt buffer used to
    construct the prompt.  (#1095, #1153)
    
    This is necessary for correctness, since the system message
    function is intended by the user to be evaluated in their current
    environment.
    
    The parsed system message (as returned by gptel--parse-directive)
    is always a list, even when the original directive is a string.
    This can cause issues when applying presets in the prompt that
    modify the system message by appending to it.  To avoid this,
    ensure that string-valued system prompts remain string-valued
    until the query is finally realized in `gptel--realize-query'.
    
    This latter fix is ugly, but the alternative is to make
    `gptel--parse-directive' a polymorphic, sum-type function, which
    appears to be uglier on the whole considering its many uses in
    gptel's request pipeline and UI.
    
    * NEWS (New features and UI changes): Mention bug fix.
---
 NEWS             | 8 ++++++++
 gptel-request.el | 7 ++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 7a35d0273a9..29059a76242 100644
--- a/NEWS
+++ b/NEWS
@@ -38,6 +38,14 @@
   ring redirection now correctly captures the full response from the
   LLM, including pre- and post-tool-call text.
 
+** Notable bug fixes
+
+- Function-valued system messages/directives are now evaluated in the
+  buffer from which the gptel request is sent, so they can use the
+  context of the current buffer correctly.  (Previously they were
+  evaluated in a temporary buffer used to construct the query, leading
+  to unexpected behavior.)
+
 * 0.9.9.3
 
 ** Breaking changes
diff --git a/gptel-request.el b/gptel-request.el
index 2b6bf179995..ff7cd3ce8a3 100644
--- a/gptel-request.el
+++ b/gptel-request.el
@@ -1999,11 +1999,16 @@ be used to rerun or continue the request at a later 
time."
               ;; TEMP Decide on the annoated prompt-list format
               (gptel--parse-list-and-insert prompt)
               (current-buffer)))))
+         (system-list (gptel--parse-directive system 'raw)) ;eval 
function-valued system prompts
          (info (list :data prompt-buffer
                      :buffer buffer
                      :position start-marker)))
     (when transforms (plist-put info :transforms transforms))
-    (with-current-buffer prompt-buffer (setq gptel--system-message system))
+    (with-current-buffer prompt-buffer
+      (setq gptel--system-message       ;guaranteed to be buffer-local
+            ;; Retain single-part system messages as strings to avoid surprises
+            ;; when applying presets
+            (if (cdr system-list) system-list (car system-list))))
     (when stream (plist-put info :stream stream))
     ;; This context should not be confused with the context aggregation 
context!
     (when callback (plist-put info :callback callback))

Reply via email to