aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2026-05-10 02:43:48 -0500
committerCraig Jennings <c@cjennings.net>2026-05-10 02:43:48 -0500
commit0248afe222a0722ec336e8c09269612eb773702b (patch)
tree0108507c9b92616dca51b6c575137785f2f8d4dc
parentc78574ab7a7bd0f9a6e4a61c6cdcd196257cff8e (diff)
downloaddotemacs-0248afe222a0722ec336e8c09269612eb773702b.tar.gz
dotemacs-0248afe222a0722ec336e8c09269612eb773702b.zip
Move GPTel tool loading into AI config
Move the local GPTel tool wiring out of init.el and into ai-config. The tools directory and feature list are now configurable, missing optional tools are non-fatal, and focused tests cover the loading behavior.
-rw-r--r--init.el11
-rw-r--r--modules/ai-config.el49
-rw-r--r--tests/test-ai-config-gptel-local-tools.el57
-rw-r--r--tests/testutil-ai-config.el6
4 files changed, 109 insertions, 14 deletions
diff --git a/init.el b/init.el
index a070dd79..3d079fc7 100644
--- a/init.el
+++ b/init.el
@@ -135,17 +135,6 @@
(require 'ai-config) ;; LLM integration with GPTel and friends
(require 'restclient-config) ;; REST API client for API exploration
-(with-eval-after-load 'gptel
- (add-to-list 'load-path "~/.emacs.d/gptel-tools")
- ;; Buffer Tools
- (require 'read_buffer)
- ;; Filesystem Tools
- (require 'read_text_file)
- (require 'write_text_file)
- ;; (require 'update_text_file) ;; BUG: issues with this tool
- (require 'list_directory_files)
- (require 'move_to_trash))
-
;; ------------------------- Personal Workflow Related -------------------------
(require 'calendar-sync) ;; sync calendars, must come after org-agenda
diff --git a/modules/ai-config.el b/modules/ai-config.el
index 3b0b6e20..98e738ea 100644
--- a/modules/ai-config.el
+++ b/modules/ai-config.el
@@ -34,9 +34,6 @@
(autoload 'cj/gptel-load-conversation "ai-conversations" "Load a saved AI conversation." t)
(autoload 'cj/gptel-delete-conversation "ai-conversations" "Delete a saved AI conversation." t)
-(with-eval-after-load 'gptel
- (require 'ai-conversations))
-
;;; ------------------------- AI Config Helper Functions ------------------------
;; Define variables upfront
@@ -45,6 +42,52 @@
(defvar gptel-claude-backend nil "Claude backend, lazy-initialized.")
(defvar gptel-chatgpt-backend nil "ChatGPT backend, lazy-initialized.")
+(defcustom cj/gptel-tools-directory
+ (expand-file-name "gptel-tools/" user-emacs-directory)
+ "Directory containing optional local GPTel tool modules."
+ :type 'directory
+ :group 'cj)
+
+(defcustom cj/gptel-local-tool-features
+ '(read_buffer
+ read_text_file
+ write_text_file
+ list_directory_files
+ move_to_trash)
+ "Feature symbols for optional local GPTel tool modules."
+ :type '(repeat symbol)
+ :group 'cj)
+
+(defun cj/gptel-load-local-tools
+ (&optional tools-directory tool-features)
+ "Load optional GPTel tools from TOOLS-DIRECTORY.
+TOOL-FEATURES defaults to `cj/gptel-local-tool-features'. Return a list
+of loaded feature symbols. Missing directories or individual optional
+tools are reported with `message' and do not signal."
+ (let ((dir (file-name-as-directory
+ (expand-file-name (or tools-directory cj/gptel-tools-directory))))
+ (features (or tool-features cj/gptel-local-tool-features))
+ (loaded nil))
+ (cond
+ ((not (file-directory-p dir))
+ (message "GPTel tools directory not found: %s" dir)
+ nil)
+ (t
+ (add-to-list 'load-path dir)
+ (dolist (feature features)
+ (condition-case err
+ (if (require feature nil 'noerror)
+ (push feature loaded)
+ (message "Optional GPTel tool not found: %s" feature))
+ (error
+ (message "Failed to load GPTel tool %s: %s"
+ feature
+ (error-message-string err)))))
+ (nreverse loaded)))))
+
+(with-eval-after-load 'gptel
+ (require 'ai-conversations)
+ (cj/gptel-load-local-tools))
(defun cj/auth-source-secret (host user)
"Fetch a secret from auth-source for HOST and USER.
diff --git a/tests/test-ai-config-gptel-local-tools.el b/tests/test-ai-config-gptel-local-tools.el
new file mode 100644
index 00000000..8d3a45ac
--- /dev/null
+++ b/tests/test-ai-config-gptel-local-tools.el
@@ -0,0 +1,57 @@
+;;; test-ai-config-gptel-local-tools.el --- Tests for local GPTel tool loading -*- lexical-binding: t; -*-
+
+;;; Commentary:
+
+;; Tests for optional local GPTel tool loading from ai-config.el.
+
+;;; Code:
+
+(require 'ert)
+
+(add-to-list 'load-path (expand-file-name "tests" user-emacs-directory))
+(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory))
+(setq load-prefer-newer t)
+(require 'testutil-ai-config)
+(require 'ai-config)
+
+(defun test-ai-config-gptel-local-tools--write-tool (dir feature)
+ "Write a temporary tool module named FEATURE into DIR."
+ (let ((file (expand-file-name (format "%s.el" feature) dir)))
+ (write-region
+ (format ";;; %s.el --- test tool -*- lexical-binding: t; -*-\n(provide '%s)\n"
+ feature feature)
+ nil
+ file
+ nil
+ 'silent)))
+
+(ert-deftest test-ai-config-gptel-local-tools-missing-directory-is-non-fatal ()
+ "Missing optional tool directory should not signal or load anything."
+ (let ((dir (expand-file-name "missing-gptel-tools/"
+ (make-temp-file "gptel-tools-home-" t))))
+ (should-not (cj/gptel-load-local-tools dir '(test_missing_tool)))))
+
+(ert-deftest test-ai-config-gptel-local-tools-loads-present-tools ()
+ "Present tool modules should be loaded and returned in request order."
+ (let ((dir (make-temp-file "gptel-tools-" t))
+ (features '(test_gptel_tool_one test_gptel_tool_two)))
+ (dolist (feature features)
+ (test-ai-config-gptel-local-tools--write-tool dir feature))
+ (should (equal (cj/gptel-load-local-tools dir features)
+ features))
+ (dolist (feature features)
+ (should (featurep feature)))))
+
+(ert-deftest test-ai-config-gptel-local-tools-skips-missing-tool-files ()
+ "Missing optional tool files should not prevent present tools from loading."
+ (let ((dir (make-temp-file "gptel-tools-" t))
+ (present 'test_gptel_present_tool)
+ (missing 'test_gptel_missing_tool))
+ (test-ai-config-gptel-local-tools--write-tool dir present)
+ (should (equal (cj/gptel-load-local-tools dir (list present missing))
+ (list present)))
+ (should (featurep present))
+ (should-not (featurep missing))))
+
+(provide 'test-ai-config-gptel-local-tools)
+;;; test-ai-config-gptel-local-tools.el ends here
diff --git a/tests/testutil-ai-config.el b/tests/testutil-ai-config.el
index e8953389..c7486222 100644
--- a/tests/testutil-ai-config.el
+++ b/tests/testutil-ai-config.el
@@ -7,6 +7,12 @@
;;; Code:
+(setq load-prefer-newer t)
+
+;; Keep ai-config tests isolated from personal optional GPTel tool files.
+(defvar cj/gptel-tools-directory (make-temp-file "gptel-tools-empty-" t))
+(defvar cj/gptel-local-tool-features nil)
+
;; Pre-cache API keys so auth-source is never consulted
(defvar cj/anthropic-api-key-cached "test-anthropic-key")
(defvar cj/openai-api-key-cached "test-openai-key")