Skip to content

Commit 8efcb4b

Browse files
committed
gptel-org: Read config from Org properties (karthink#141)
* gptel.el: Load gptel-org after Org is loaded. * gptel-org.el (gptel-org--send-with-props): Advise `gptel-send` and `gptel--suffix-send` to use gptel's local config (stored under the current Org heading) when applicable. Advice is a hacky way to do it, but this is the simplest option without explicit indirection via `derived-mode-p`-based dispatch code in many more places in gptel.
1 parent 998fb30 commit 8efcb4b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

gptel-org.el

+25
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,31 @@ value of `gptel-org-branching-context', which see."
174174
;; Create prompt the usual way
175175
(gptel--parse-buffer gptel-backend max-entries))))
176176

177+
(defun gptel-org--send-with-props (send-fun &rest args)
178+
"Conditionally modify SEND-FUN's calling environment.
179+
180+
If in an Org buffer under a heading containing a stored gptel
181+
configuration, use that for requests instead. This includes the
182+
system message, model and provider (backend), among other
183+
parameters."
184+
(if (derived-mode-p 'org-mode)
185+
(pcase-let ((`(,gptel--system-message ,gptel-backend ,gptel-model
186+
,gptel-temperature ,gptel-max-tokens)
187+
(seq-mapn (lambda (a b) (or a b))
188+
(gptel-org--entry-properties)
189+
(list gptel--system-message gptel-backend gptel-model
190+
gptel-temperature gptel-max-tokens))))
191+
(apply send-fun args))
192+
(apply send-fun args)))
193+
194+
(advice-add 'gptel-send :around #'gptel-org--send-with-props)
195+
(advice-add 'gptel--suffix-send :around #'gptel-org--send-with-props)
196+
197+
;; ;; NOTE: Basic uses in org-mode are covered by advising gptel-send and
198+
;; ;; gptel--suffix-send. For custom commands it might be necessary to advise
199+
;; ;; gptel-request instead.
200+
;; (advice-add 'gptel-request :around #'gptel-org--send-with-props)
201+
177202

178203
;;; Saving and restoring state
179204
(defun gptel-org--entry-properties (&optional pt)

gptel.el

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@
133133
(require 'cl-generic)
134134
(require 'gptel-openai)
135135

136+
(with-eval-after-load 'org
137+
(require 'gptel-org))
138+
136139

137140
;; User options
138141

0 commit comments

Comments
 (0)