Skip to content

Commit bdb6152

Browse files
committed
gptel-org: Read config from Org properties (#141)
* gptel.el: Load gptel-org after Org is loaded. * gptel-org.el (gptel-org--send-with-props): Advise `gptel-send`, `gptel-request` 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 without inserting explicit indirection or stuffing `derived-mode-p`-based dispatch code in many more places in gptel.
1 parent f427054 commit bdb6152

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

gptel-org.el

+21
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,27 @@ value of `gptel-org-branching-context', which see."
170170
;; Create prompt the usual way
171171
(gptel--parse-buffer gptel-backend max-entries))))
172172

173+
(defun gptel-org--send-with-props (send-fun &rest args)
174+
"Conditionally modify SEND-FUN's calling environment.
175+
176+
If in an Org buffer under a heading containing a stored gptel
177+
configuration, use that for requests instead. This includes the
178+
system message, model and provider (backend), among other
179+
parameters."
180+
(if (derived-mode-p 'org-mode)
181+
(pcase-let ((`(,gptel--system-message ,gptel-backend ,gptel-model
182+
,gptel-temperature ,gptel-max-tokens)
183+
(seq-mapn (lambda (a b) (or a b))
184+
(gptel-org--entry-properties)
185+
(list gptel--system-message gptel-backend gptel-model
186+
gptel-temperature gptel-max-tokens))))
187+
(apply send-fun args))
188+
(apply send-fun args)))
189+
190+
(advice-add 'gptel-send :around #'gptel-org--send-with-props)
191+
(advice-add 'gptel-request :around #'gptel-org--send-with-props)
192+
(advice-add 'gptel--suffix-send :around #'gptel-org--send-with-props)
193+
173194

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

gptel.el

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
(require 'cl-generic)
130130
(require 'gptel-openai)
131131

132+
(with-eval-after-load 'org
133+
(require 'gptel-org))
134+
132135

133136
;; User options
134137

0 commit comments

Comments
 (0)