Function: gptel--display-tool-results

gptel--display-tool-results is a natively compiled function defined in gptel.el.

Signature

(gptel--display-tool-results TOOL-RESULTS INFO)

Documentation

Insert TOOL-RESULTS into buffer.

TOOL-RESULTS is

 ((tool args result) ...)

for tool call results. INFO contains the state of the request.

Source Code

;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel.el
(defun gptel--display-tool-results (tool-results info)
  "Insert TOOL-RESULTS into buffer.

TOOL-RESULTS is

 ((tool args result) ...)

for tool call results.  INFO contains the state of the request."
  (let* ((start-marker (plist-get info :position))
         (tool-marker (plist-get info :tool-marker))
         (tracking-marker (plist-get info :tracking-marker)))
    ;; Insert tool results
    (with-current-buffer (marker-buffer start-marker)
      (when gptel-include-tool-results
        (cl-loop
         for (tool args result) in tool-results
         with include-names =
         (mapcar #'gptel-tool-name
                 (cl-remove-if-not #'gptel-tool-include (plist-get info :tools)))
         if (or (memq gptel-include-tool-results '(t call))
                (member (gptel-tool-name tool) include-names))
         do (funcall
             (plist-get info :callback)
             (let* ((name (gptel-tool-name tool))
                    (separator        ;Separate from response prefix if required
                     (cond ((not tracking-marker)
                            (and gptel-mode
                                 (not (string-suffix-p
                                       "\n" (gptel-response-prefix-string)))
                                 "\n")) ;start of response
                           ((not tool-marker) gptel-response-separator)
                           ((and (not (= tracking-marker tool-marker))
                                 (not (eq (char-before tracking-marker) ?\n)))
                            gptel-response-separator)))
                    (tool-use
                     ;; TODO(tool) also check args since there may be more than
                     ;; one call/result for the same tool
                     (cl-find-if
                      (lambda (tu) (equal (plist-get tu :name) name))
                      (plist-get info :tool-use)))
                    (id (plist-get tool-use :id))
                    (display-call (format "(%s %s)" name
                                          (string-trim (prin1-to-string args) "(" ")")))
                    (call (prin1-to-string `(:name ,name :args ,args)))
                    (truncated-call
                     (string-replace "\n" " "
                                     (truncate-string-to-width
                                      display-call
                                      (floor (* (window-width) 0.6)) 0 nil " ...)")))
                    (result-final       ;Check if the results should be excluded
                     (if (or (eq gptel-include-tool-results 'call)
                             (and (eq gptel-include-tool-results 'auto)
                                  (eq (gptel-tool-include tool) 'call)))
                         "(Cached tool result — available during original generation but not replayed)"
                       result)))
               (if (derived-mode-p 'org-mode)
                   (concat
                    separator
                    "#+begin_tool "
                    truncated-call
                    (propertize
                     (org-escape-code-in-string (concat "\n" call "\n\n" result-final))
                     'gptel `(tool . ,id))
                    "\n#+end_tool\n")
                 ;; TODO(tool) else branch is handling all front-ends as markdown.
                 ;; At least escape markdown.
                 (concat
                  separator
                  ;; TODO(tool) remove properties and strip instead of ignoring
                  (propertize (format "``` tool %s" truncated-call)
                              'gptel 'ignore 'keymap gptel--markdown-block-map)
                  (propertize
                   ;; TODO(tool) escape markdown in result
                   (concat "\n" call "\n\n" result-final)
                   'gptel `(tool . ,id))
                  ;; TODO(tool) remove properties and strip instead of ignoring
                  (propertize "\n```\n" 'gptel 'ignore
                              'keymap gptel--markdown-block-map))))
             info
             'raw)
         ;; tool-result insertion has updated the tracking marker
         (unless tracking-marker
           (setq tracking-marker (plist-get info :tracking-marker)))
         (if tool-marker
             (move-marker tool-marker tracking-marker)
           (setq tool-marker (copy-marker tracking-marker nil))
           (plist-put info :tool-marker tool-marker))
         (ignore-errors                 ;fold drawer
           (save-excursion
             (goto-char tracking-marker)
             (forward-line -1)
             (if (derived-mode-p 'org-mode)
                 (when (looking-at-p "^#\\+end_tool") (org-cycle))
               (when (looking-at-p "^```") (gptel-markdown-cycle-block))))))))))