From 25401cf0eb3d537f61e6de28cfed72fe90ef25f2 Mon Sep 17 00:00:00 2001 From: Vibrys Date: Tue, 18 Jun 2024 21:53:48 +0200 Subject: [PATCH 1/2] gptel-org-force-branching-context-with-topic introduced --- gptel-org.el | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gptel-org.el b/gptel-org.el index eceb491c..a21a7806 100644 --- a/gptel-org.el +++ b/gptel-org.el @@ -114,6 +114,12 @@ This makes it feasible to have multiple conversation branches." :type 'boolean :group 'gptel) +(defcustom gptel-org-force-branching-context-with-topic nil + "force the existence both the topic and branching context" + :local t + :type 'boolean + :group 'gptel) + ;;; Setting context and creating queries (defun gptel-org--get-topic-start () @@ -158,9 +164,11 @@ 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 gptel-org-force-branching-context-with-topic + (error "topic forced but non-existent"))) (if gptel-org-branching-context ;; Create prompt from direct ancestors of point (if (fboundp 'org-element-lineage-map) @@ -203,6 +211,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 gptel-org-force-branching-context-with-topic + (error "branching context forced but non-existent")) ;; Create prompt the usual way (gptel--parse-buffer gptel-backend max-entries)))) From ea86418c298284eb87fe43d99aa341fbd528d141 Mon Sep 17 00:00:00 2001 From: Vibrys Date: Wed, 19 Jun 2024 16:27:45 +0200 Subject: [PATCH 2/2] more generic flag introduced: gptel-force-prompt-context-be-explicit it replaces less generic flag (ie org-mode specific): gptel-org-force-branching-context-with-topic --- gptel-org.el | 15 +++++---------- gptel.el | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/gptel-org.el b/gptel-org.el index a21a7806..7df20121 100644 --- a/gptel-org.el +++ b/gptel-org.el @@ -114,12 +114,6 @@ This makes it feasible to have multiple conversation branches." :type 'boolean :group 'gptel) -(defcustom gptel-org-force-branching-context-with-topic nil - "force the existence both the topic and branching context" - :local t - :type 'boolean - :group 'gptel) - ;;; Setting context and creating queries (defun gptel-org--get-topic-start () @@ -167,8 +161,9 @@ value of `gptel-org-branching-context', which see." (if topic-start ;; narrow to GPTEL_TOPIC property scope (narrow-to-region topic-start prompt-end) - (when gptel-org-force-branching-context-with-topic - (error "topic forced but non-existent"))) + (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) @@ -211,8 +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 gptel-org-force-branching-context-with-topic - (error "branching context forced but non-existent")) + (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.