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

    gptel: Make :append actions on strings idempotent
    
    * gptel.el (gptel--modify-value): A common use of the :append and
    :prepend keys when specifying string-valued preset keys is to add
    to the system message.  If a preset is applied twice, this
    concatenation happens twice as well, which is undesirable more
    than it is intentional.  Make this action idempotent, so presets
    can be applied multiple times without repeatedly appending
    strings.
    
    In general, list values are still appended as specified without
    any checks.  (When specifying :tools in particular, the list is
    deduplicated before sending a request.)
    
    This change is experimental.
---
 gptel.el | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/gptel.el b/gptel.el
index ef8637ff72a..d20bd289db3 100644
--- a/gptel.el
+++ b/gptel.el
@@ -323,6 +323,7 @@ NEW-SPEC is either a declarative action spec (plist) of the 
form
 form it is returned as is.
 
 - :append and :prepend will append/prepend val (a list or string) to ORIGINAL.
+  Actions on strings are idempotent, they will only be appended/prepended once.
 - :eval will evaluate val and return the result, and
 - :function will call val with ORIGINAL as its argument, and return the result.
 - :merge will treat ORIGINAL and NEW-SPEC as plists and return a merged plist,
@@ -334,10 +335,14 @@ form it is returned as is.
         (let ((key (pop tail)) (form (pop tail)))
           (setq current
                 (pcase key
-                  (:append (funcall (if (stringp form) #'concat #'append)
-                                    current form))
-                  (:prepend (funcall (if (stringp form) #'concat #'append)
-                                     form current))
+                  (:append (if (stringp form)
+                               (if (string-suffix-p form current t)
+                                   current (concat current form))
+                             (append current form)))
+                  (:prepend (if (stringp form)
+                                (if (string-prefix-p form current t)
+                                    current (concat form current))
+                              (append form current)))
                   (:eval (eval form t))
                   (:function (funcall form current))
                   (:merge (gptel--merge-plists (copy-sequence current) form))

Reply via email to