Fix org-ql-select-rifle

This commit is contained in:
Phil Bajsicki 2025-04-15 21:17:23 +02:00
parent f7b3de3789
commit 44d56bdefd
2 changed files with 451 additions and 432 deletions

View file

@ -120,6 +120,12 @@ Stuff, headers, etc.
;;; Code:)
#+end_src
** gptel-org-tools
Collects into =gptel-org-tools= list, distinct from =gptel-tools=
#+begin_src elisp
(defvar gptel-org-tools '())
#+end_src
** The tools
*** Emacs
These tools are primarily concerned with Emacs, Emacs Lisp, and files-or-buffers.
@ -133,8 +139,8 @@ E.g. can't find the right ~org-ql-select~ query? Let's use an eldritch abominati
Highly not recommended, but sometimes an LLM can pull a rabbit out of pure entropy.
#+begin_src elisp :tangle no
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (elisp)
(unless (stringp elisp) (error "elisp code must be a string"))
(with-temp-buffer
@ -145,7 +151,8 @@ Highly not recommended, but sometimes an LLM can pull a rabbit out of pure entro
:args (list '(:name "eval"
:type string
:description "The Emacs Lisp code to evaluate."))
:category "emacs"))
:category "emacs"
:confirm t))
#+end_src
**** list-buffers
@ -155,8 +162,8 @@ The rationale behind using ~ibuffer~ is the same as with dired. They both displa
Seems to be one of the most reliable tools in the basket... mostly because
#+begin_src elisp
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (arg)
(with-temp-buffer
(ibuffer)
@ -181,8 +188,8 @@ Be sure to customize the function to point to your org directory, if you wish. I
#+end_comment
#+begin_src elisp
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (dir)
(with-temp-buffer
(dired (or dir "~"))
@ -200,8 +207,8 @@ Be sure to customize the function to point to your org directory, if you wish. I
**** find-buffer-visiting
#+begin_src elisp
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (filename)
(bufferp (find-buffer-visiting (expand-file-name filename))))
:name "find-buffer-visiting"
@ -211,24 +218,27 @@ Be sure to customize the function to point to your org directory, if you wish. I
:description "The filename to compare to open buffers."))
:category "org-mode"))
#+end_src
**** find-file-noselect
Continuation from above. Open a file into a buffer for processing. Onec it's found by dired-list.
**** open-file-inactive
Continuation from above. Open a file into a buffer for processing, once it's found by dired-list.
#+begin_src elisp
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (file)
(find-file-noselect file))
:name "find-file-noselect"
:description "Open the file in a buffer. This doesn't interfere with the user."
(with-current-buffer (get-buffer-create file)
(insert-file-contents file)
(current-buffer)))
:name "open-file-inactive"
:description "Open the file in a background buffer. This doesn't interfere with the user."
:args (list '(:name "file"
:type string
:description "Path to file.."))
:category "filesystem"))
#+end_src
**** read-file-contents
This reads file contents,
#+begin_src elisp
(add-to-list 'gptel-tools
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (filename)
(with-temp-buffer
@ -243,7 +253,7 @@ This reads file contents,
#+end_src
**** describe-variable
#+begin_src elisp
(add-to-list 'gptel-tools
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (var)
(let ((symbol (intern var)))
@ -260,7 +270,7 @@ This reads file contents,
**** describe-function
#+begin_src elisp
(add-to-list 'gptel-tools
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (fun)
(let ((symbol (intern fun)))
@ -307,7 +317,6 @@ Pretty simple, does what it says on the tin. It gets all the tags from the =buff
This is not, by any means, sufficient, but I do tag people and specific events frequently enough that it helps save on the context window.
#+begin_src elisp
(defun gptel-org-tools--org-extract-tags (buffer)
(interactive "bBuffer: ")
(with-current-buffer buffer
(let ((tags '()))
(org-map-entries
@ -319,8 +328,8 @@ This is not, by any means, sufficient, but I do tag people and specific events f
(push tag tags))))))
(sort (-uniq tags) #'string<))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-extract-tags
:name "org-extract-tags"
:description "Extract all unique tags from an org-mode buffer"
@ -337,7 +346,6 @@ Then we need to pull /some/ information from the buffer, without dragging the en
Therefore, headings. A reasonable amount of information, and still keeping the signal-to-noise ratio pretty decent.
#+begin_src elisp
(defun gptel-org-tools--org-extract-headings (buffer)
(interactive "bBuffer: ")
(with-current-buffer buffer
(org-map-entries
#'(buffer-substring-no-properties
@ -346,8 +354,8 @@ Therefore, headings. A reasonable amount of information, and still keeping the s
t
'file)))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-extract-headings
:name "org-extract-headings"
:description "Extract all headings from an org-mode buffer"
@ -366,6 +374,11 @@ My current goal is to replace this monstrosity with individual functions for eac
But in the interim, this works. Kind of.
#+begin_comment
Currently *not* tangled, as I'm testing breaking out each type of query into its own individual tool.
#+end_comment
#+begin_src elisp
(defun gptel-org-tools--org-ql-select (buf query)
(org-ql-select
@ -381,8 +394,8 @@ But in the interim, this works. Kind of.
(outline-next-heading)
(line-beginning-position)))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select
:name "org-ql-select"
:description "Run org-ql-select against buffer with query. Using filename fails."
@ -417,7 +430,6 @@ But, any customizations to tweak this is left to the user, as everyone has their
#+begin_src elisp
(defun gptel-org-tools--org-ql-select-dates (buf date)
(interactive "fBuffer: \nsDate (YYYY or YYYY-MM): ")
(org-ql-select
(get-buffer buf)
`(heading ,date)
@ -427,8 +439,8 @@ But, any customizations to tweak this is left to the user, as everyone has their
(org-end-of-subtree)))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-dates
:name "org-ql-select-dates"
:description "Extract org subtree by date in YYYY or YYYY-MM format"
@ -445,8 +457,8 @@ This is still work in progress, the idea is to have the LLM check my calendar an
It works, in principle, but I haven't been able to find a use for it yet. The real challenge is in building a context where the tools integrate with each-other in a way that makes sense. For now, this exists.
#+begin_src elisp
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (days)
(with-temp-buffer
(org-agenda-list (or days 14))
@ -479,8 +491,8 @@ Retrieve the headings where the heading matches query..
(line-end-position))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-headings
:name "org-ql-select-headings"
:description "Retreive matching headings from buffer using org-ql-select. Matches only against heading. Using filename fails."
@ -500,15 +512,15 @@ Retrieve all the headings where either heading or content matches query.
(org-ql-select
(get-buffer buf)
`(rifle ,query)
:action :action #'(lambda ()
:action #'(lambda ()
(concat
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-headings-rifle
:name "org-ql-select-headings-rifle"
:description "Retreive headings from buffer using org-ql-select. Matches against both heading and content. Using filename fails."
@ -537,8 +549,8 @@ This pulls all the headings (and their contents) when they match tags (without i
(line-beginning-position)))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-tags-local
:name "org-ql-select-tags-local"
:description "Run org-ql-select-tags-local against buffer with query. No tag inheritance."
@ -567,8 +579,8 @@ This pulls all the headings (and their contents) when they match tags (with inhe
(line-beginning-position)))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-tags
:name "org-ql-select-tags"
:description "Run org-ql-select-tags against buffer with query. Supports tag inheritance."
@ -585,8 +597,10 @@ This pulls all the headings (and their contents) when they match tags (with inhe
And, the "grab everything that matches" tool.
#+begin_src elisp
(defun gptel-org-tools--org-ql-select-rifle (buf query)
(let ((buffer (get-buffer buf)))
(if buffer
(org-ql-select
(get-buffer buf)
buffer
`(rifle ,query)
:action #'(lambda ()
(concat
@ -594,10 +608,11 @@ And, the "grab everything that matches" tool.
(line-beginning-position)
(progn
(outline-next-heading)
(line-beginning-position)))))))
(line-beginning-position))))))
(message "Buffer '%s' does not exist." buf))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-rifle
:name "org-ql-select-rifle"
:description "Run org-ql-select-rifle against buffer with query."

View file

@ -34,8 +34,10 @@
;;; Code:)
(add-to-list 'gptel-tools
(gptel-make-tool
(defvar gptel-org-tools '())
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (arg)
(with-temp-buffer
(ibuffer)
@ -50,8 +52,8 @@
:optional t))
:category "emacs"))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (dir)
(with-temp-buffer
(dired (or dir "~"))
@ -66,8 +68,8 @@
:optional t))
:category "filesystem"))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (filename)
(bufferp (find-buffer-visiting (expand-file-name filename))))
:name "find-buffer-visiting"
@ -77,18 +79,20 @@
:description "The filename to compare to open buffers."))
:category "org-mode"))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (file)
(find-file-noselect file))
:name "find-file-noselect"
:description "Open the file in a buffer. This doesn't interfere with the user."
(with-current-buffer (get-buffer-create file)
(insert-file-contents file)
(current-buffer)))
:name "open-file-inactive"
:description "Open the file in a background buffer. This doesn't interfere with the user."
:args (list '(:name "file"
:type string
:description "Path to file.."))
:category "filesystem"))
(add-to-list 'gptel-tools
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (filename)
(with-temp-buffer
@ -101,7 +105,7 @@
:description "The filename to read."))
:category "org-mode"))
(add-to-list 'gptel-tools
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (var)
(let ((symbol (intern var)))
@ -115,7 +119,7 @@
:description "Variable name"))
:category "emacs"))
(add-to-list 'gptel-tools
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (fun)
(let ((symbol (intern fun)))
@ -131,7 +135,6 @@
:category "emacs"))
(defun gptel-org-tools--org-extract-tags (buffer)
(interactive "bBuffer: ")
(with-current-buffer buffer
(let ((tags '()))
(org-map-entries
@ -143,8 +146,8 @@
(push tag tags))))))
(sort (-uniq tags) #'string<))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-extract-tags
:name "org-extract-tags"
:description "Extract all unique tags from an org-mode buffer"
@ -154,7 +157,6 @@
:category "org-mode"))
(defun gptel-org-tools--org-extract-headings (buffer)
(interactive "bBuffer: ")
(with-current-buffer buffer
(org-map-entries
#'(buffer-substring-no-properties
@ -163,8 +165,8 @@
t
'file)))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-extract-headings
:name "org-extract-headings"
:description "Extract all headings from an org-mode buffer"
@ -187,8 +189,8 @@
(outline-next-heading)
(line-beginning-position)))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select
:name "org-ql-select"
:description "Run org-ql-select against buffer with query. Using filename fails."
@ -201,7 +203,6 @@
:category "org"))
(defun gptel-org-tools--org-ql-select-dates (buf date)
(interactive "fBuffer: \nsDate (YYYY or YYYY-MM): ")
(org-ql-select
(get-buffer buf)
`(heading ,date)
@ -211,8 +212,8 @@
(org-end-of-subtree)))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-dates
:name "org-ql-select-dates"
:description "Extract org subtree by date in YYYY or YYYY-MM format"
@ -224,8 +225,8 @@
:description "Date in YYYY or YYYY-MM format. Can add multiple like so: \"YYYY-MM\" \"YYYY-MM\""))
:category "org"))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function (lambda (days)
(with-temp-buffer
(org-agenda-list (or days 14))
@ -250,8 +251,8 @@
(line-end-position))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-headings
:name "org-ql-select-headings"
:description "Retreive matching headings from buffer using org-ql-select. Matches only against heading. Using filename fails."
@ -267,15 +268,15 @@
(org-ql-select
(get-buffer buf)
`(rifle ,query)
:action :action #'(lambda ()
:action #'(lambda ()
(concat
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-headings-rifle
:name "org-ql-select-headings-rifle"
:description "Retreive headings from buffer using org-ql-select. Matches against both heading and content. Using filename fails."
@ -300,8 +301,8 @@
(line-beginning-position)))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-tags-local
:name "org-ql-select-tags-local"
:description "Run org-ql-select-tags-local against buffer with query. No tag inheritance."
@ -326,8 +327,8 @@
(line-beginning-position)))))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-tags
:name "org-ql-select-tags"
:description "Run org-ql-select-tags against buffer with query. Supports tag inheritance."
@ -340,8 +341,10 @@
:category "org-ql"))
(defun gptel-org-tools--org-ql-select-rifle (buf query)
(let ((buffer (get-buffer buf)))
(if buffer
(org-ql-select
(get-buffer buf)
buffer
`(rifle ,query)
:action #'(lambda ()
(concat
@ -349,10 +352,11 @@
(line-beginning-position)
(progn
(outline-next-heading)
(line-beginning-position)))))))
(line-beginning-position))))))
(message "Buffer '%s' does not exist." buf))))
(add-to-list 'gptel-tools
(gptel-make-tool
(add-to-list 'gptel-org-tools
(gptel-make-tool
:function #'gptel-org-tools--org-ql-select-rifle
:name "org-ql-select-rifle"
:description "Run org-ql-select-rifle against buffer with query."