branch: externals/minuet
commit 5fc65bb88d9a20ea3542492e08968d65081cbff4
Author: Milan Glacier <d...@milanglacier.com>
Commit: Milan Glacier <d...@milanglacier.com>

    feat: add `minuet-set-nested-plist` for convenient nested plist 
manipulation.
---
 README.md  |  2 +-
 minuet.el  | 38 +++++++++++++++++++++++++++++---------
 recipes.md | 21 +++++++++++----------
 3 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/README.md b/README.md
index 0e1889048e..2300c2565a 100644
--- a/README.md
+++ b/README.md
@@ -223,7 +223,7 @@ llama-server \
     ;; Llama.cpp does not support the `suffix` option in FIM completion.
     ;; Therefore, we must disable it and manually populate the special
     ;; tokens required for FIM completion.
-    (minuet-set-optional-options minuet-openai-fim-compatible-options :suffix 
nil :template)
+    (minuet-set-nested-plist minuet-openai-fim-compatible-options nil 
:template :suffix)
     (minuet-set-optional-options
      minuet-openai-fim-compatible-options
      :prompt
diff --git a/minuet.el b/minuet.el
index e07193eb82..bd05e1f0fe 100644
--- a/minuet.el
+++ b/minuet.el
@@ -370,17 +370,37 @@ fib(5)")
        (not (or (evil-insert-state-p)
                 (evil-emacs-state-p)))))
 
-(defun minuet-set-optional-options (options key val &optional field)
-  "Set the value of KEY in the FIELD of OPTIONS to VAL.
-If FIELD is not provided, it defaults to :optional.  If VAL is nil,
+(defmacro minuet-set-nested-plist (place val &rest attributes)
+  "Set or delete a PLIST's nested ATTRIBUTES.
+PLACE is the plist to set.
+If VAL is non-nil, set the nested attribute to VAL.
+If VAL is nil, delete the final attribute from its parent plist.
+Example usage:
+\(minuet-set-nested-plist `minuet-openai-options' 256 :optional :max-tokens)
+;; delete :max-tokens field
+\(minuet-set-nested-plist `minuet-openai-options' nil :optional :max-tokens)"
+  (if (null attributes)
+      (error "mg--setf-nested-plist requires at least one attribute key"))
+  (if val
+      (let ((access-form place))
+        (dolist (attr attributes)
+          (setq access-form `(plist-get ,access-form ,attr)))
+        `(setf ,access-form ,val))
+    ;; If val is nil, delete the key from its parent plist.
+    (let* ((all-but-last-attributes (butlast attributes))
+           (last-attribute (car (last attributes)))
+           (parent-plist-accessor place))
+      (dolist (attr all-but-last-attributes)
+        (setq parent-plist-accessor `(plist-get ,parent-plist-accessor ,attr)))
+      `(setf ,parent-plist-accessor (map-delete ,parent-plist-accessor 
,last-attribute)))))
+
+(defun minuet-set-optional-options (options key val &optional parent-key)
+  "Set the value of KEY in the PARENT-KEY of OPTIONS to VAL.
+If PARENT-KEY is not provided, it defaults to :optional.  If VAL is nil,
 then remove KEY from OPTIONS.  This helper function simplifies setting
 values in a two-level nested plist structure."
-  (let ((field (or field :optional)))
-    (if val
-        (setf (plist-get options field)
-              (plist-put (plist-get options field) key val))
-      (setf (plist-get options field)
-            (map-delete (plist-get options field) key)))))
+  (let ((parent-key (or parent-key :optional)))
+    (minuet-set-nested-plist options val parent-key key)))
 
 (defun minuet--eval-value (value)
   "Eval a VALUE for minuet.
diff --git a/recipes.md b/recipes.md
index 330b846684..6d28831d5e 100644
--- a/recipes.md
+++ b/recipes.md
@@ -54,7 +54,7 @@ llama-server \
     ;; Llama.cpp does not support the `suffix` option in FIM completion.
     ;; Therefore, we must disable it and manually populate the special
     ;; tokens required for FIM completion.
-    (minuet-set-optional-options minuet-openai-fim-compatible-options :suffix 
nil :template)
+    (minuet-set-nested-plist minuet-openai-fim-compatible-options nil 
:template :suffix)
     (minuet-set-optional-options
      minuet-openai-fim-compatible-options
      :prompt
@@ -116,17 +116,18 @@ backend with the DeepInfra FIM API and 
Qwen-2.5-Coder-32B-Instruct model.
   ;; DeepInfra FIM does not support the `suffix` option in FIM
   ;; completion.  Therefore, we must disable it and manually
   ;; populate the special tokens required for FIM completion.
-  (minuet-set-optional-options minuet-openai-fim-compatible-options :suffix 
nil :template)
+  (minuet-set-nested-plist minuet-openai-fim-compatible-options nil :template 
:suffix)
 
   ;; Custom prompt formatting for Qwen model
-  (minuet-set-optional-options minuet-openai-fim-compatible-options
-                               :prompt
-                               (defun 
minuet-deepinfra-fim-qwen-prompt-function (ctx)
-                                 (format 
"<|fim_prefix|>%s\n%s<|fim_suffix|>%s<|fim_middle|>"
-                                         (plist-get ctx :language-and-tab)
-                                         (plist-get ctx :before-cursor)
-                                         (plist-get ctx :after-cursor)))
-                               :template)
+  (minuet-set-nested-plist
+   minuet-openai-fim-compatible-options
+   (defun minuet-deepinfra-fim-qwen-prompt-function (ctx)
+     (format "<|fim_prefix|>%s\n%s<|fim_suffix|>%s<|fim_middle|>"
+             (plist-get ctx :language-and-tab)
+             (plist-get ctx :before-cursor)
+             (plist-get ctx :after-cursor)))
+   :template
+   :prompt)
 
   ;; Function to transform requests data according to DeepInfra's API format.
   (defun minuet-deepinfra-fim-transform (data)

Reply via email to