Initialize the Khoj Transient menu on first run instead of load

This prevents Khoj from polling the Khoj server until explicitly
invoked via `khoj' entrypoint function.

Previously it'd make a request to the khoj server every time Emacs or
khoj.el was loaded

Closes #243
This commit is contained in:
Debanjum Singh Solanky 2023-10-13 18:48:26 -07:00
parent b669aa2395
commit f64fa06e22

View file

@ -1092,17 +1092,20 @@ Paragraph only starts at first text after blank line."
;; Khoj Menu ;; Khoj Menu
;; --------- ;; ---------
(transient-define-argument khoj--content-type-switch () (defun khoj--setup-and-show-menu ()
:class 'transient-switches "Create Transient menu for khoj and show it."
:argument-format "--content-type=%s" ;; Create the Khoj Transient menu
:argument-regexp ".+" (transient-define-argument khoj--content-type-switch ()
;; set content type to: last used > based on current buffer > default type :class 'transient-switches
:init-value (lambda (obj) (oset obj value (format "--content-type=%s" (or khoj--content-type (khoj--buffer-name-to-content-type (buffer-name)))))) :argument-format "--content-type=%s"
;; dynamically set choices to content types enabled on khoj backend :argument-regexp ".+"
:choices (or (ignore-errors (mapcar #'symbol-name (khoj--get-enabled-content-types))) '("all" "org" "markdown" "pdf" "image"))) ;; set content type to: last used > based on current buffer > default type
:init-value (lambda (obj) (oset obj value (format "--content-type=%s" (or khoj--content-type (khoj--buffer-name-to-content-type (buffer-name))))))
;; dynamically set choices to content types enabled on khoj backend
:choices (or (ignore-errors (mapcar #'symbol-name (khoj--get-enabled-content-types))) '("all" "org" "markdown" "pdf" "image")))
(transient-define-suffix khoj--search-command (&optional args) (transient-define-suffix khoj--search-command (&optional args)
(interactive (list (transient-args transient-current-command))) (interactive (list (transient-args transient-current-command)))
(progn (progn
;; set content type to: specified > last used > based on current buffer > default type ;; set content type to: specified > last used > based on current buffer > default type
(setq khoj--content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name)))) (setq khoj--content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
@ -1111,9 +1114,9 @@ Paragraph only starts at first text after blank line."
;; trigger incremental search ;; trigger incremental search
(call-interactively #'khoj-incremental))) (call-interactively #'khoj-incremental)))
(transient-define-suffix khoj--find-similar-command (&optional args) (transient-define-suffix khoj--find-similar-command (&optional args)
"Find items similar to current item at point." "Find items similar to current item at point."
(interactive (list (transient-args transient-current-command))) (interactive (list (transient-args transient-current-command)))
(progn (progn
;; set content type to: specified > last used > based on current buffer > default type ;; set content type to: specified > last used > based on current buffer > default type
(setq khoj--content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name)))) (setq khoj--content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
@ -1121,37 +1124,40 @@ Paragraph only starts at first text after blank line."
(setq khoj-results-count (or (transient-arg-value "--results-count=" args) khoj-results-count)) (setq khoj-results-count (or (transient-arg-value "--results-count=" args) khoj-results-count))
(khoj--find-similar khoj--content-type))) (khoj--find-similar khoj--content-type)))
(transient-define-suffix khoj--update-command (&optional args) (transient-define-suffix khoj--update-command (&optional args)
"Call khoj API to update index of specified content type." "Call khoj API to update index of specified content type."
(interactive (list (transient-args transient-current-command))) (interactive (list (transient-args transient-current-command)))
(let* ((force-update (if (member "--force-update" args) "true" "false")) (let* ((force-update (if (member "--force-update" args) "true" "false"))
;; set content type to: specified > last used > based on current buffer > default type ;; set content type to: specified > last used > based on current buffer > default type
(content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name)))) (content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
(type-query (if (equal content-type "all") "" (format "t=%s" content-type))) (type-query (if (equal content-type "all") "" (format "t=%s" content-type)))
(update-url (format "%s/api/update?%s&force=%s&client=emacs" khoj-server-url type-query force-update)) (update-url (format "%s/api/update?%s&force=%s&client=emacs" khoj-server-url type-query force-update))
(url-request-method "GET")) (url-request-method "GET"))
(progn (progn
(setq khoj--content-type content-type) (setq khoj--content-type content-type)
(url-retrieve update-url (lambda (_) (message "khoj.el: %s index %supdated!" content-type (if (member "--force-update" args) "force " ""))))))) (url-retrieve update-url (lambda (_) (message "khoj.el: %s index %supdated!" content-type (if (member "--force-update" args) "force " "")))))))
(transient-define-suffix khoj--chat-command (&optional _) (transient-define-suffix khoj--chat-command (&optional _)
"Command to Chat with Khoj." "Command to Chat with Khoj."
(interactive (list (transient-args transient-current-command))) (interactive (list (transient-args transient-current-command)))
(khoj--chat)) (khoj--chat))
(transient-define-prefix khoj--menu () (transient-define-prefix khoj--menu ()
"Create Khoj Menu to Configure and Execute Commands." "Create Khoj Menu to Configure and Execute Commands."
[["Configure Search" [["Configure Search"
("n" "Results Count" "--results-count=" :init-value (lambda (obj) (oset obj value (format "%s" khoj-results-count)))) ("n" "Results Count" "--results-count=" :init-value (lambda (obj) (oset obj value (format "%s" khoj-results-count))))
("t" "Content Type" khoj--content-type-switch)] ("t" "Content Type" khoj--content-type-switch)]
["Configure Update" ["Configure Update"
("-f" "Force Update" "--force-update")]] ("-f" "Force Update" "--force-update")]]
[["Act" [["Act"
("c" "Chat" khoj--chat-command) ("c" "Chat" khoj--chat-command)
("s" "Search" khoj--search-command) ("s" "Search" khoj--search-command)
("f" "Find Similar" khoj--find-similar-command) ("f" "Find Similar" khoj--find-similar-command)
("u" "Update" khoj--update-command) ("u" "Update" khoj--update-command)
("q" "Quit" transient-quit-one)]]) ("q" "Quit" transient-quit-one)]])
;; Show the Khoj Transient menu
(khoj--menu))
;; ---------- ;; ----------
@ -1164,7 +1170,7 @@ Paragraph only starts at first text after blank line."
(interactive) (interactive)
(when khoj-auto-setup (when khoj-auto-setup
(khoj-setup t)) (khoj-setup t))
(khoj--menu)) (khoj--setup-and-show-menu))
(provide 'khoj) (provide 'khoj)