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

    gptel-openai: Adapt OpenAI tool-ids for Openrouter+Anthropic
    
    * gptel-openai.el (gptel--openai-format-tool-id,
    gptel--openai-unformat-tool-id): It is possible to use the
    gptel-openai to access Anthropic backends with Openrouter.  In
    this case, the tool call ids are not in the call_* format, and
    tool calls fail (#747).  Try to work around this by registering
    tool call ids as received when they are not in the call_* format.
    
    This will make the chat log not work when switching backends when
    both of these conditions are met: (i) tool results are included in
    the buffer and (ii) a non-standard tool call id is in effect.  But
    there's not much we can do about it since we no longer tell if the
    gptel-openai backend in effect wants call_*, toolu_* or some other
    format.
    
    * gptel-anthropic.el (gptel--anthropic-unformat-tool-id): Don't
    issue a message if the tool call id is not in the expected form.
---
 gptel-anthropic.el |  4 +---
 gptel-openai.el    | 12 ++++++++----
 2 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/gptel-anthropic.el b/gptel-anthropic.el
index 9446b0237e..307636fcf0 100644
--- a/gptel-anthropic.el
+++ b/gptel-anthropic.el
@@ -300,9 +300,7 @@ TOOL-USE is a list of plists containing tool names, 
arguments and call results."
 (defun gptel--anthropic-unformat-tool-id (tool-id)
   (or (and (string-match "toolu_\\(.+\\)" tool-id)
            (match-string 1 tool-id))
-      (progn
-        (message "Unexpected tool_call_id format: %s" tool-id)
-        tool-id)))
+      tool-id))
 
 (cl-defmethod gptel--parse-list ((_backend gptel-anthropic) prompt-list)
   (cl-loop for text in prompt-list
diff --git a/gptel-openai.el b/gptel-openai.el
index 221f86f511..f5ae38c32e 100644
--- a/gptel-openai.el
+++ b/gptel-openai.el
@@ -328,14 +328,18 @@ Mutate state INFO with response metadata."
    tool-use))
 
 (defun gptel--openai-format-tool-id (tool-id)
-  (format "call_%s" tool-id))
+  "Format TOOL-ID for OpenAI.
+
+If the ID has the format used by a different backend, use as-is."
+  (if (or (string-prefix-p "toolu_" tool-id) ;#747
+          (string-prefix-p "call_"  tool-id))
+      tool-id
+    (format "call_%s" tool-id)))
 
 (defun gptel--openai-unformat-tool-id (tool-id)
   (or (and (string-match "call_\\(.+\\)" tool-id)
            (match-string 1 tool-id))
-      (progn
-        (message "Unexpected tool_call_id format: %s" tool-id)
-        tool-id)))
+      tool-id))
 
 ;; NOTE: No `gptel--inject-prompt' method required for gptel-openai, since this
 ;; is handled by its defgeneric implementation

Reply via email to