branch: externals/llm
commit 76f2ce2326ecfeec7f91805b3626cc53af429882
Author: Andrew Hyatt <[email protected]>
Commit: GitHub <[email protected]>

    Run tools in the original buffer (#233)
    
    This should fix https://github.com/ahyatt/llm/issues/231
---
 NEWS.org              |  1 +
 llm-provider-utils.el | 50 +++++++++++++++++++++++++++-----------------------
 2 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/NEWS.org b/NEWS.org
index b0e51ebc7b..21a6795a72 100644
--- a/NEWS.org
+++ b/NEWS.org
@@ -2,6 +2,7 @@
 - Removed bad interactions made in Ollama tool calls
 - Fixed Ollama tool calling requests
 - Fixed Ollama reasoning, whose API has changed
+- Run tools in the original buffer
 * Version 0.28.3
 - Fixed breakage in Ollama streaming tool calling
 - Fixed incorrect Ollama streaming tool use capability reporting
diff --git a/llm-provider-utils.el b/llm-provider-utils.el
index ed039b72a2..07dbe208a2 100644
--- a/llm-provider-utils.el
+++ b/llm-provider-utils.el
@@ -360,17 +360,20 @@ return a list of `llm-chat-prompt-tool-use' structs.")
      :headers (llm-provider-headers provider)
      :data (llm-provider-chat-request provider prompt nil)
      :on-success (lambda (data)
-                   (if-let ((err-msg (llm-provider-chat-extract-error provider 
data)))
-                       (llm-provider-utils-callback-in-buffer
-                        buf error-callback 'error
-                        err-msg)
-                     (llm-provider-utils-process-result
-                      provider prompt
-                      (llm-provider-utils-extract-all provider data)
-                      multi-output
-                      (lambda (result)
-                        (llm-provider-utils-callback-in-buffer
-                         buf success-callback result)))))
+                   (with-current-buffer (if (buffer-live-p buf)
+                                            buf
+                                          (generate-new-buffer " *llm-temp*" 
t))
+                     (if-let ((err-msg (llm-provider-chat-extract-error 
provider data)))
+                         (llm-provider-utils-callback-in-buffer
+                          buf error-callback 'error
+                          err-msg)
+                       (llm-provider-utils-process-result
+                        provider prompt
+                        (llm-provider-utils-extract-all provider data)
+                        multi-output
+                        (lambda (result)
+                          (llm-provider-utils-callback-in-buffer
+                           buf success-callback result))))))
      :on-error (lambda (_ data)
                  (llm-provider-utils-callback-in-buffer
                   buf error-callback 'error
@@ -443,18 +446,19 @@ Any strings will be concatenated, integers will be added, 
etc."
      :on-success
      (lambda (_)
        ;; We don't need the data at the end of streaming, so we can ignore it.
-       (llm-provider-utils-process-result
-        provider prompt
-        (llm-provider-utils-streaming-accumulate
-         current-result
-         (when-let ((tool-uses-raw (plist-get current-result
-                                              :tool-uses-raw)))
-           `(:tool-uses ,(llm-provider-collect-streaming-tool-uses
-                          provider tool-uses-raw))))
-        multi-output
-        (lambda (result)
-          (llm-provider-utils-callback-in-buffer
-           buf response-callback result))))
+       (with-current-buffer (if (buffer-live-p buf)
+                                (generate-new-buffer " *llm-temp*" t))
+         (llm-provider-utils-process-result
+          provider prompt
+          (llm-provider-utils-streaming-accumulate
+           current-result
+           (when-let ((tool-uses-raw (plist-get current-result
+                                                :tool-uses-raw)))
+             `(:tool-uses ,(llm-provider-collect-streaming-tool-uses
+                            provider tool-uses-raw))))
+          multi-output
+          (lambda (result)
+            (llm-provider-utils-callback-in-buffer buf response-callback 
result)))))
      :on-error (lambda (_ data)
                  (llm-provider-utils-callback-in-buffer
                   buf error-callback 'error

Reply via email to