branch: externals/minuet
commit 8f98e3db92069dd63a3c903f36e50f436e3ba997
Author: Milan Glacier <[email protected]>
Commit: Milan Glacier <[email protected]>

    refactor: extract transform request into function.
---
 minuet.el | 46 +++++++++++++++-------------------------------
 1 file changed, 15 insertions(+), 31 deletions(-)

diff --git a/minuet.el b/minuet.el
index 4f564e9043..cbc3487da2 100644
--- a/minuet.el
+++ b/minuet.el
@@ -1044,6 +1044,16 @@ If using ollama you can just set it to 'TERM'." api-key)
           ;; replace placeholders that are not replaced
           tmpl (replace-regexp-in-string "{{{.*}}}" "" tmpl))))
 
+(defun minuet--apply-request-transform (options end-point headers body)
+  "Apply OPTIONS transform functions to END-POINT, HEADERS, and BODY.
+Return a plist with :end-point, :headers, and :body."
+  (let ((transformed (list :end-point end-point
+                           :headers headers
+                           :body body)))
+    (dolist (fn (plist-get options :transform))
+      (setq transformed (or (funcall fn transformed) transformed)))
+    transformed))
+
 (defun minuet--openai-fim-complete-base (options get-text-fn context callback)
   "The base function to complete code with openai fim API.
 OPTIONS are the provider options.  GET-TEXT-FN are the function to get
@@ -1054,7 +1064,6 @@ arrive."
          ;; Initialize input components
          (name (plist-get options :name))
          (end-point (plist-get options :end-point))
-         (transform-functions (plist-get options :transform))
          (body `(,@(plist-get options :optional)
                  :stream t
                  :model ,(plist-get options :model)
@@ -1070,14 +1079,7 @@ arrive."
                     ("Accept" . "application/json")
                     ("Authorization" .
                      ,(concat "Bearer " (minuet--get-api-key (plist-get 
options :api-key))))))
-         ;; Apply transformations
-         (transformed `(:end-point ,end-point
-                        :headers ,headers
-                        :body ,body))
-         (transformed (progn (dolist (fn transform-functions)
-                               (setq transformed (or (funcall fn transformed) 
transformed)))
-                             transformed))
-         ;; Extract transformed components
+         (transformed (minuet--apply-request-transform options end-point 
headers body))
          (end-point (plist-get transformed :end-point))
          (headers (plist-get transformed :headers))
          (body (plist-get transformed :body))
@@ -1170,8 +1172,7 @@ and :content keys."
 OPTIONS are the provider options.  the completion items from json.
 CONTEXT is to be used to build the prompt.  CALLBACK is the function
 to be called when completion items arrive."
-  (let* ((transform-functions (plist-get options :transform))
-         (end-point (plist-get options :end-point))
+  (let* ((end-point (plist-get options :end-point))
          (headers `(("Content-Type" . "application/json")
                     ("Accept" . "application/json")
                     ("Authorization" .
@@ -1185,12 +1186,7 @@ to be called when completion items arrive."
                              (minuet--eval-value (plist-get options :fewshots))
                              (--> (minuet--make-chat-llm-shot context options)
                                   minuet--create-chat-messages-from-list))))
-         (transformed `(:end-point ,end-point
-                        :headers ,headers
-                        :body ,body))
-         (transformed (progn (dolist (fn transform-functions)
-                               (setq transformed (or (funcall fn transformed) 
transformed)))
-                             transformed))
+         (transformed (minuet--apply-request-transform options end-point 
headers body))
          (end-point (plist-get transformed :end-point))
          (headers (plist-get transformed :headers))
          (body-json (json-serialize (plist-get transformed :body))))
@@ -1239,7 +1235,6 @@ CONTEXT and CALLBACK will be passed to the base function."
 CONTEXT is to be used to build the prompt.  CALLBACK is the function
 to be called when completion items arrive."
   (let* ((options minuet-claude-options)
-         (transform-functions (plist-get options :transform))
          (end-point (plist-get options :end-point))
          (headers `(("Content-Type" . "application/json")
                     ("Accept" . "application/json")
@@ -1254,12 +1249,7 @@ to be called when completion items arrive."
                              (minuet--eval-value (plist-get options :fewshots))
                              (--> (minuet--make-chat-llm-shot context options)
                                   minuet--create-chat-messages-from-list))))
-         (transformed `(:end-point ,end-point
-                        :headers ,headers
-                        :body ,body))
-         (transformed (progn (dolist (fn transform-functions)
-                               (setq transformed (or (funcall fn transformed) 
transformed)))
-                             transformed))
+         (transformed (minuet--apply-request-transform options end-point 
headers body))
          (end-point (plist-get transformed :end-point))
          (headers (plist-get transformed :headers))
          (body-json (json-serialize (plist-get transformed :body))))
@@ -1316,7 +1306,6 @@ CHAT is a list of plists with :role and :content keys"
 CONTEXT is to be used to build the prompt.  CALLBACK is the function
 to be called when completion items arrive."
   (let* ((options minuet-gemini-options)
-         (transform-functions (plist-get options :transform))
          (end-point (format "%s/%s:streamGenerateContent?alt=sse"
                             (plist-get options :end-point)
                             (plist-get options :model)))
@@ -1332,12 +1321,7 @@ to be called when completion items arrive."
                              (--> (minuet--make-chat-llm-shot context options)
                                   minuet--create-chat-messages-from-list
                                   
minuet--transform-openai-chat-to-gemini-chat))))
-         (transformed `(:end-point ,end-point
-                        :headers ,headers
-                        :body ,body))
-         (transformed (progn (dolist (fn transform-functions)
-                               (setq transformed (or (funcall fn transformed) 
transformed)))
-                             transformed))
+         (transformed (minuet--apply-request-transform options end-point 
headers body))
          (end-point (plist-get transformed :end-point))
          (headers (plist-get transformed :headers))
          (body-json (json-serialize (plist-get transformed :body))))

Reply via email to