Skip to content

Commit

Permalink
org unescape only the results, not the call
Browse files Browse the repository at this point in the history
The call and results are in the tool_call block.  The call is a read compatible
structure.  The result is just a big string.
  • Loading branch information
psionic-k committed Feb 12, 2025
1 parent 11c329b commit b408dd6
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions gptel-org.el
Original file line number Diff line number Diff line change
Expand Up @@ -638,16 +638,28 @@ cleaning up after."
(delete-region (match-beginning 0) (match-end 0))))

(defun gptel--org-unescape-tool-results ()
"Undo escapes done to keep results from escaping blocks."
(cl-labels ((tool-result-p (_value property)
(and (listp property)
(eq (car property) 'tool-result))))
(save-excursion
(goto-char (point-min))
;; TODO `next-single-char-property-change'
(while-let ((found (text-property-search-forward 'gptel nil #'tool-result-p)))
(org-unescape-code-in-region (prop-match-beginning found)
(prop-match-end found))))))
"Undo escapes done to keep results from escaping blocks.
Scans backward for gptel tool text property, reads the arguments, then
unescapes the remainder."
(save-excursion
(goto-char (point-max))
(let ((prev-pt (point)))
(while (> prev-pt (point-min))
(goto-char
(previous-single-char-property-change (point) 'gptel))
(let ((prop (get-text-property (point) 'gptel))
(backward-progress (point)))
(when (eq (car-safe prop) 'tool)
;; User edits to clean up can potentially insert a tool-call header
;; that is propertized. Tool call headers should not be
;; propertized.
(when (looking-at "[[:space:]]*#\\+begin_tool_call")
(goto-char (match-end 0)))
(condition-case _err
(read (current-buffer))
(((end-of-file invalid-read-syntax))))
(org-unescape-code-in-region (point) prev-pt))
(goto-char (setq prev-pt backward-progress)))))))

(provide 'gptel-org)
;;; gptel-org.el ends here

0 comments on commit b408dd6

Please sign in to comment.