branch: elpa/gptel
commit 13757388ca1f41bf5556201f96ab18b97ad63872
Author: Psionik K <73710933+psioni...@users.noreply.github.com>
Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>

    gptel-gemini: Parse tool results from buffer
    
    * gptel-gemini.el (gptel--parse-buffer):
    Create tool call and result messages from each recorded call.
    
    Parse tool messages in the buffer.  This commit covers the Gemini
    backend.  The strategy is to pcase on the gptel property.  Since
    tool insertions have one distinct gptel text property, the call
    and response message are recovered at the same time.
    
    Introduce support for the `ignore' property in the Gemini prompt
    creator.  Users can make use of it to have non-conversation
    content intermingled with chat.
    
    NOTE: Gemini does not use tool-call ids, so no ids are stored in
    the gptel text property.  This can cause problems if the user
    switches from Gemini to a backend that does use ids.
---
 gptel-gemini.el | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/gptel-gemini.el b/gptel-gemini.el
index e6311b6662..4f474bb2dd 100644
--- a/gptel-gemini.el
+++ b/gptel-gemini.el
@@ -225,7 +225,7 @@ See generic implementation for full documentation."
            else collect (list :role "model" :parts `(:text ,text)) into prompts
            finally return prompts))
 
-(cl-defmethod gptel--parse-buffer ((_backend gptel-gemini) &optional 
max-entries)
+(cl-defmethod gptel--parse-buffer ((backend gptel-gemini) &optional 
max-entries)
   (let ((prompts) (prev-pt (point))
         (include-media (and gptel-track-media (or (gptel--model-capable-p 
'media)
                                                   (gptel--model-capable-p 
'url)))))
@@ -239,6 +239,26 @@ See generic implementation for full documentation."
              (when-let* ((content (gptel--trim-prefixes
                                    (buffer-substring-no-properties (point) 
prev-pt))))
                (push (list :role "model" :parts (list :text content)) 
prompts)))
+            (`(tool . ,_id)
+             (save-excursion
+               (condition-case nil
+                   (let* ((tool-call (read (current-buffer)))
+                          (name (plist-get tool-call :name))
+                          (arguments  (plist-get tool-call :args)))
+                     (plist-put tool-call :result
+                                (string-trim (buffer-substring-no-properties
+                                              (point) prev-pt)))
+                     (push (gptel--parse-tool-results backend (list tool-call))
+                           prompts)
+                     (push (list :role "model"
+                                 :parts
+                                 (vector `(:functionCall ( :name ,name
+                                                           :args ,arguments))))
+                           prompts))
+                 ((end-of-file invalid-read-syntax)
+                  (message (format "Could not parse tool-call on line %s"
+                                   (line-number-at-pos (point))))))))
+            ('ignore)
             ('nil
              (if include-media
                  (when-let* ((content (gptel--gemini-parse-multipart

Reply via email to