branch: elpa/gptel
commit e8523ec5b8c4a6eabdc5f1aefcff239968a98183
Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>

    gptel: Update tool arg types preprocesing
    
    * gptel.el (gptel--preprocess-tool-args): This (pre-existing)
    function is used to convert the :type of tool args from symbols to
    strings before running json-serialize.  Update it so that it also
    handles the conversion when :type is specified inside a vector,
    such as in this example.
    
      (:name "switch"
       :description "Whether to switch to the new database"
       :allOf [(:type string) (:type string :enum ["true" "false"])]
       :default "false")
    
    Add a TODO item to use the same preprocessor to convert :enum
    specifications that are lists instead of arrays.
---
 gptel.el | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/gptel.el b/gptel.el
index f32f00312b..4042a59369 100644
--- a/gptel.el
+++ b/gptel.el
@@ -1554,20 +1554,29 @@ feed the LLM the results.  You can add tools via
 
 (defun gptel--preprocess-tool-args (spec)
   "Convert symbol :type values in tool SPEC to strings destructively."
-  (cond ((not (listp spec)) spec)
+  ;; NOTE: Do not use `sequencep' here, as that covers strings too and breaks
+  ;; things.
+  (cond ((not (or (listp spec) (vectorp spec))) spec)
+        ((vectorp spec)
+         (cl-loop for element across spec
+                  for idx upfrom 0
+                  do (aset spec idx (gptel--preprocess-tool-args element)))
+         spec)
         ((keywordp (car spec))
          (let ((tail spec))
            (while tail
              (when (and (eq (car tail) :type) (symbolp (cadr tail)))
                (setcar (cdr tail) (symbol-name (cadr tail))))
-             (when (listp (cadr tail))
+             ;; TODO: Handle :enum ("provided" "as" "list") here, convert to
+             ;; :enum ["provided" "as" "array"]
+             (when (or (listp (cadr tail)) (vectorp (cadr tail)))
                (gptel--preprocess-tool-args (cadr tail)))
              (setq tail (cddr tail)))
            spec))
-        (t (dolist (element spec)
-             (when (listp element)
-               (gptel--preprocess-tool-args element)))
-           spec)))
+        ((listp spec) (dolist (element spec)
+                        (when (listp element)
+                          (gptel--preprocess-tool-args element)))
+         spec)))
 
 (defvar gptel--known-tools nil
   "Alist of gptel tools arranged by category.

Reply via email to