diff --git a/gptel-org.el b/gptel-org.el index eceb491c..7df20121 100644 --- a/gptel-org.el +++ b/gptel-org.el @@ -158,9 +158,12 @@ value of `gptel-org-branching-context', which see." (let ((max-entries (and gptel--num-messages-to-send (* 2 gptel--num-messages-to-send))) (topic-start (gptel-org--get-topic-start))) - (when topic-start - ;; narrow to GPTEL_TOPIC property scope - (narrow-to-region topic-start prompt-end)) + (if topic-start + ;; narrow to GPTEL_TOPIC property scope + (narrow-to-region topic-start prompt-end) + (when (and gptel-force-prompt-context-be-explicit (buffer-file-name)) + (error "explicit prompt context forced but org's topic not present + along hierarchical lineage of the current Org heading"))) (if gptel-org-branching-context ;; Create prompt from direct ancestors of point (if (fboundp 'org-element-lineage-map) @@ -203,6 +206,8 @@ value of `gptel-org-branching-context', which see." '(gptel org) "Using `gptel-org-branching-context' requires Org version 9.6.7 or higher, it will be ignored.") (gptel--parse-buffer gptel-backend max-entries)) + (when (and gptel-force-prompt-context-be-explicit (buffer-file-name)) + (error "explicit prompt context forced but org's branching context is not set")) ;; Create prompt the usual way (gptel--parse-buffer gptel-backend max-entries)))) diff --git a/gptel.el b/gptel.el index 5f217e0a..c8599a4c 100644 --- a/gptel.el +++ b/gptel.el @@ -514,6 +514,19 @@ with `gptel-mode' enabled), where user prompts and responses are always handled separately." :type 'boolean) +(defcustom gptel-force-prompt-context-be-explicit nil + "Force the context that is sent along the prompt be explicitly bound. + +If non-nil, then: +For Org mode's FILE buffers: `gptel-org-branching-context' must be non-nil + and conversation topic needs to be present + in hierarchical lineage of the current Org + heading. +For other buffers: region must be marked." + :local t + :type 'boolean + :group 'gptel) + (defvar-local gptel--old-header-line nil) @@ -1050,8 +1063,11 @@ there." ((derived-mode-p 'org-mode) (require 'gptel-org) (gptel-org--create-prompt (or prompt-end (point-max)))) - (t (goto-char (or prompt-end (point-max))) - (gptel--parse-buffer gptel-backend max-entries))))))) + (t + (when gptel-force-prompt-context-be-explicit + (error "explicit prompt context forced but region is NOT marked")) + (goto-char (or prompt-end (point-max))) + (gptel--parse-buffer gptel-backend max-entries))))))) (cl-defgeneric gptel--parse-buffer (backend max-entries) "Parse current buffer backwards from point and return a list of prompts.